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?
View all comments
Show parent comment
3
u/Conscious-Ball8373 Aug 05 '26
I'm agreeing with you - assert in production code is not acceptable.
The problem with my "strawman" is that it raises `AssertionError`. In what production code is raising `AssertionError` directly acceptable? None that I ever review. You raise an exception that's actually appropriate to the condition or handle it in some other way. Raising `AssertionError` all over the place just means you'll have a catch-all `except AssertionError` somewhere near the top of the stack, which is now functionally equivalent to `except Exception` which the linter will also - rightly - call out.
So I agree that the fact it can be turned off is a problem. But I'm saying there are problems even if it can't be turned off - it uses too-general an exception type to report errors.