r/Python • • 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?

149 Upvotes

213 comments sorted by

View all comments

Show parent comment

1

u/gdchinacat Aug 05 '26

Thanks for clarifying. I don’t have a problem with raising AssertionError because I’m skeptical meaningful recovery handling for an exception that indicates unexpected conditions exist. In cases where an invariant was violated there isn’t anything a higher level of code can do to change that. A retry isn’t going to make an internally generated out of bounds become in bounds, or an invalid configuration value valid. The best an exception handler can do is keep the process from crashing so other work that isn’t impacted can continue.
I don’t consider input validation a good use of assertion errors, those should use exceptions that accurately report the error to the client.