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
0
u/xBBTx Aug 05 '26
The premise is that the check wouldn't be there in the first place. The inline assert is there to make the assumption/invariant expectation explicit/visible.
Adding the assert in this case costs nothing: no uncovered test branch and associated low-value unit test that tests implementation details and hurts refactoring, no performance loss in prod because the asserts are optimized away.
We gain from it by:
The difference with the manual check + raising an error is that it does require additional tests and can't be optimized out (though performance in this case is a bullshit argument, it's Python after all)