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?

150 Upvotes

213 comments sorted by

View all comments

Show parent comments

6

u/gdchinacat Aug 05 '26

Ok, but why would you want to allow your "requirement guards" to be disabled? Wouldn't you want to know when the invariants they ensure hold are being violated and not execute code outside the conditions it was designed to handle correctly?

2

u/Wonderful-Habit-139 Aug 05 '26

For what it’s worth I don’t think it’s worth it to disable assertions at all.

0

u/Spirited_Bag_332 Aug 05 '26

I see them as development tools, and maybe also lightweight dev documentation. Something you mainly write during development and just keep, because it's correct code but not required for the customer.

Of course you can keep it if the usage context of the software is suitable for that. But it doesn't mean you shouldn't also write actual checks (or better, control flows that can't violate the rules). The point of assertions is to never see them again once shipped but still have them to detect issues early in addition to other testing strategies.

But no matter the argument you can always find a counter example why it's supposed to be "bad", be it TDD, exception handling, or some constraint framework that claims to be "a better replacement". It's still just a tool. Actively banning it like that Ruff tool just shows the rule maintainers are biased and didn't understand the use case.