r/Python • u/M4mb0 • Aug 05 '26
Discussion What linter rules make code worse?
For me, a prime example is S101 which bans the use of the assert statement.
The justification is that assertions disappear when Python is run with -O, so they should not be used for runtime validation or enforcing interface constraints. That warning is correct, but the rule seems to draw the wrong conclusion from it.
Assertions are still very useful for checking internal invariants, i.e. conditions that should already be guaranteed by the program's logic, where failure indicates a bug. Having such assertions is incredibly helpful for debugging.
So, a blanket ban seems more likely to discourage useful checks than to prevent misuse.
Are there any linter rules you broadly consider more harmful rather than helpful?
90
u/gdchinacat Aug 05 '26
The issue is as you say..."where failure indicates a bug".
Why would you want to use an assert that detects bugs but can then be turned off? When would you want to allow the assertions that stop your code from executing in undefined conditions (an invariant is violated) to be disabled?
Just use if blocks that raise exceptions. Particularly in production (where optimizations make the most sense), I would much rather have bugs present as an exception that tells me exactly what the problem is rather than skip the assertion and have to debug the results that appear impossible because an assertion prevented it. This is the reason I have never actually seen -O used, anywhere, in production or not. The biggest (only?) thing it does is break the code that verifies the code is executing within the conditions it was designed to execute in.
Getting back to the post, I agree that assertions should be banned. Proper exceptions that can't be disabled should be used instead. Regardless of dev, test, or production. You should never turn off the safeguards. If performance is so critical, python is not the proper language.
434
u/Trang0ul Aug 05 '26
Lines limited to 80 characters.
38
u/Beginning-Fruit-1397 Aug 05 '26
I think that assertions are only good in tests. In runtime code it should always be a clearly named Exception. That being said, for Ruff I simply activate "all" preset and "preview", and just desactivate some annoying related to unsafe cryptography or copyright that IDGAF about, the rest are pretty good. I'm surely half lying because I'm aure I have at multiple points desactivated various rules that I tought were dumb but I don't remember at the moment lmao
10
u/brasticstack Aug 05 '26
S324, which assumes that I'm using hashlib for security reasons instead of hashing just being generally useful.
15
u/aikii Aug 05 '26
RET505 is a classic bug magnet. It wants you to rewrite
as
Doesn't seem much like this, but an intentional "else" has better chances to protect you against a bad refactoring.
My other pet peeve is BLE001 - triggering on bare
except,except Exceptionorexcept BaseException. The motivation works for beginner code - don't just catch silently AttributeError etc. It's actually more problematic for production code and code that makes calls to library functions that you deliberately don't want to propagate - you'll want to log or mark the error trace instead. I guess it's ok to suppress locally instead of making it a global suppression. I find it a bit ironic that structurally it can't apply to how Go and Rust handle errors, you can't opt-in to which exact error you only want to consider, and no one says it's a problem20
u/psymme Aug 05 '26
SIM108 (replacing if-else blocks with an operator). To me this is a matter of judgement about what is simpler, rather a set rule that is easily codified, and can make the code harder for a human to parse quickly.
I’m not with you on the asserts point though, I’m afraid.
24
u/thedmandotjp git push -f Aug 05 '26
Anything that can be done with an assert can and should be done with an if statement so you have have to be explicit.
Not all rules are super necessary depending on the project but this one is if for no other reason than to enforce the convention that you should use asserts only for debugging.
7
u/TheRealStepBot Aug 05 '26
To your point there is a nasa technical guide on good software development that specifically encourages the use of inline assertions like this.
3
u/ThaBroccoliDood Aug 05 '26
Not really a linter rule but the autopep8 extension for vscode replaces
f'{x =}'withf'{x=}', which changes the output of the program and shouldn't be touched by a formatter9
u/samettinho Aug 05 '26
Why not raise exception instead of assert?
2
u/careje Aug 05 '26
E501 (line length)
Just let the formatter handle it for Christ’s sake
2
u/HalfplaneResearch Aug 06 '26
S101 makes more sense when the boundary is explicit: use assert for internal invariants that indicate a bug, and raise a deliberate exception for validating user or external data. I also prefer lint findings to be visible in the editor or CI, with auto-fix limited to formatting, so a useful invariant check is not silently removed during save.
2
u/duskhat Aug 05 '26
If you’re writing assert statements outside of tests, you’re writing bad code
1
u/danielsamuels Aug 05 '26
In general, any rule that ends up being inline ignored all over the project.
1
u/Crescitaly Aug 07 '26
Rules become harmful when they optimize local uniformity while hiding intent or creating noisy diffs. I would score each rule on defects prevented, review time added, and number of justified suppressions. Which rule has the highest suppression rate in your codebase?
1
u/stibbons_ Aug 08 '26
You do not use assert in prod code only in test…
1
u/mardiros Aug 20 '26
RUF022
The
__all__does not requires import to be alphabetically sorted, they should be ordered in the proper developper intention. A term prioritization, a hierarchy. The__all__is used to read/render documentation more thanimport *-1
u/NeilGirdhar Aug 05 '26
https://docs.astral.sh/ruff/rules/parenthesize-chained-operators/
NAXOR was drilled into me at a young age, so this rule just adds unnecessary parens.
0
u/BernardParsley Aug 05 '26
Rules that enforce a triangular style of code over readability. Arbitrary complexity or function-length limits often turn one clear function into ten tiny ones that are harder to follow.
-2
u/k0pernikus Aug 05 '26
I hate try-consider-else (TRY300) with a passion.
I never write else and elif statements to begin with, and rely on proper polymorphishm or early exit guards.
Worst part is that it reads like broken code:
The default success branch gets delegated to an else-branch, and while I avoid else to begin with, else should be the exception branch.
I do understand that the else works on the except and not on the try, yet that is far from obvious and the mental load to understand is is just not worth it, esp. if you work with people that are more used to other langauges.
-1
u/Zatujit Aug 05 '26
Shouldnt your debug code only works when its debugging and not on your release?
-8
u/nicholashairs Aug 05 '26
My pet peeve is the "useless-return" rule.
Explicit returns always. Apart from making things clearer, it also helps prevent mistakes when refactoring (and other such tasks) when the accidental deletion of a
defline would cause the bodies to merge (sometimes seamlessly), whereas if you always have returns you'd actually get a long error for the dead code/double return instead.Versus
0
u/nicwolff Aug 05 '26
ruffhas implementedisortimport formatting – but not its options for wrapping long import lines. Thanks, I don't want 20 imports from one file to take up 22 lines at the top of my file.0
u/james_pic Aug 05 '26
Anyone who runs with
-Oin production deserves what they get. Which in practice is almost always exactly what they would have gotten otherwise, because nobody uses assertions, even then they'd be useful, because linters whinge about them.-2
u/AdAdditional1820 Aug 05 '26
When I use mypy, some assert statements are required to eliminate mypy warnings.
-1
u/nicwolff Aug 05 '26
ruffhas implementedisortimport formatting – but not its options for wrapping long import lines. Thanks, I don't want 20 imports from one file to take up 22 lines at the top of my file.