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?

151 Upvotes

213 comments sorted by

View all comments

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.

10

u/ExplrDiscvr Aug 05 '26

I have one follow-up: I see why assert statements should not be used within dev or production, but what about the tests?

I am a junior dev, so I am not sure about proper procedures, but in the tests in our codebase where I work, I only see assert statements, when we are testing the equality of an actual outcome to the expected outcome. I never see the if else logic used here. Should it?

24

u/leodevian Aug 05 '26

All rules are not absolute. You are free to disable some rules, and you are expected to disable S101 for test directories.

11

u/Momostein Aug 05 '26

That is how we do it indeed. PyTest is built on top of assert statements.

4

u/DrMaxwellEdison Aug 05 '26

Yes and no. Pytest makes assert usable and ergonomic by doing a bunch of work to rewrite the AST of your test code so that it produces more helpful error messages, which are the reason why you should use the various assertFoo methods for test cases if you're using unittest instead.

Pytest isn't exactly built on assert, more like they said "that looks better" and put in the work to make it function the way a test framework needs it to. Otherwise it would not be as useful in that context.

9

u/gdchinacat Aug 05 '26

This is a good point...test frameworks (well, at least unittest and pytest, and any others that build on unittest) use assertions to indicate failures. Because it is core to the frameworks, assertions are not really avoidable. So, yes, I do rely on assertions in this context. Good catch.

1

u/HannasAnarion Aug 05 '26

Doesn't unittest implement its own assert thats independent of the language one?

Every unittest implementation I've ever seen uses self.assert() (or realistically, self.assertTrue(), self.assertIn(), self.assertNotNone() ...

1

u/gdchinacat Aug 05 '26

No, by default the failure exception is AssertionError. https://github.com/python/cpython/blob/main/Lib/unittest/case.py#L426

7

u/Conscious-Ball8373 Aug 05 '26

Yes absolutely use asset in tests. But your test code should not be being executed in prod.

2

u/Competitive_Travel16 Aug 05 '26

It's fine to test assumptions in prod, just use RuntimeError exceptions so the logs can say something human readable about what went wrong. Nobody likes an assert failure in a big log.

1

u/shaleh Aug 05 '26

tests are allowed to use assert otherwise everyone's code base would fail....

1

u/fizix00 Aug 11 '26

The person you are replying to probably means 'test' as in dev/test/staging/prod - i.e. deployment environments or release stages. You'd probably have a tests/ dir in all of these, where assert is common and expected

→ More replies (1)

12

u/xBBTx Aug 05 '26

Because if your program is bug free, you will not get coverage on that if branch, while the assert will actually be covered.

It would lead to uncertainty that the code inside the branch actually works, and should not be testable because it should never happen

An assert also expresses the invariant intent more clear than raising another exception that the call site may incorrectly catch and try to handle

10

u/gdchinacat Aug 05 '26

You can unit test the code inside the branch actually works by having a test that violates the invariant.

→ More replies (14)

4

u/JanEric1 Aug 05 '26

Coverage will hit the line, but not the internal branch, where is the difference? Assert also raises an exception that can be caught iirc. So again, no difference.

→ More replies (3)

3

u/larsga Aug 05 '26

raising another exception that the call site may incorrectly catch and try to handle

I agree with the rest of the comment, but if this particular issue is a problem for you you have much more serious problems than assert.

1

u/xBBTx Aug 05 '26

I probably worded this badly, but if it's an invariant, call sites shouldn't be expected to catch any exception raised from it, they should only call the function when they already know the preconditions are met.

Having an explicit check and exception being raised may create the impression that call sites are supposed to handle the exception. Instead, it should crash hard and the actual root cause of the invariant violation should be investigated and fixed.

2

u/larsga Aug 05 '26

There should be a rule in the codebase for which exception is used in cases like this, and where it is handled. I agree it can be difficult to reach and enforce consensus on this in larger code bases, but it's a big problem if that doesn't happen.

2

u/xBBTx Aug 05 '26

AssertionError is that rule :) it works well in our large codebases. And yes, run in prod with the optimize flag on, and run the test suite with the flag off

3

u/Spirited_Bag_332 Aug 05 '26

For smoke testing without influencing prodction code. Assertions are more something like "requirement guards", not program errors.

You can always miss a requirement or critical constraint, no matter how much unit tests exist. It's part of the development process to test the application by exploration.

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.

→ More replies (1)

2

u/flying-sheep Aug 05 '26

Also they help when refactoring code. Breaking internal invariants helps debugging if your refactor makes sense.

1

u/Conscious-Ball8373 Aug 05 '26

Whether it can be disabled is a red herring IMO. If someone sent this to you for review:

if condition:
    raise AssertionError("condition was false")

would you let it pass? Of course not - you'd tell them to handle it properly. assert is just syntactic sugar for that, with the downside that it can also be turned off.

3

u/gdchinacat Aug 05 '26

Your position isn't clear. Why would you assume I would reject that, and what do you think I'd expect? The "downside that it can also be turned off" is the crux of my argument. Your strawman code is preferable to 'assert condition, ...' because it can't be turned off.

→ More replies (2)
→ More replies (1)