Detect tautological asserts

Bug #848467 reported by Tristan Seligmann
20
This bug affects 3 people
Affects Status Importance Assigned to Milestone
Pyflakes
Fix Released
Wishlist
Unassigned
pyflakes (Debian)
Fix Released
Unknown

Bug Description

An assert of the form:

assert (x, 'blahblah')

is tautological, because a non-empty tuple always has a truth value of True. However, it is common to mistakenly construct an assert statement of this form when attempting to wrap it over multiple lines. Python 2.6+ already issues a SyntaxWarning about this, but it would be nice if Pyflakes detected it too.

Changed in pyflakes:
importance: Undecided → Wishlist
Changed in pyflakes (Debian):
status: Unknown → New
Changed in pyflakes (Debian):
status: New → Confirmed
Revision history for this message
John Vandenberg (jayvdb) wrote :

Pull request for the specific problem raised here : https://github.com/pyflakes/pyflakes/pull/51

I've also quickly hacked together detection of assertion of a static value. It is just a concept demo, in case anyone thinks it could be useful. It has cross-version issues, and could easily become more complete by adding more call names.
https://github.com/jayvdb/pyflakes/commit/25234773b174f0eb34fca3cd2e734290d7ab3a5e

Revision history for this message
John Vandenberg (jayvdb) wrote :

Expanding on my post, I'm think only `assert False` and `assert True` should be the only static asserts permitted.

While CPython is catching only the absolute worst type of assert, pyflakes can and IMO should report other problematic asserts.

afaik, there is no Python parser that does anything sensible with alternative static asserts. So anything other than `assert False` and `assert True` is obfuscated code, either unintentional or intentional, and the cost/benefit weights heavily in favour of reporting it as if it is an unintentional code error. If it was intentional obfuscated code, the coder will be happy it causes an error.

However, there could be use cases for embedding 'hidden' structured information in the assert ast, which are stripped in optimize mode. If that is a serious concern, I can package up my code as a flake8 plugin instead.

Revision history for this message
Jakub Wilk (jwilk) wrote :

I used `assert 0` a few times, because that's less typing than `assert False`.
Debian Code Search says I'm not the only one who's that lazy:
https://codesearch.debian.net/search?q=%28%3Fm%29^\s*assert\s%2B0\s*%28%2C|%24%29+filetype%3Apython

What's the use case for `assert True`?

In general, I think assertions that are always true are probably mistakes; and assertions that are always false are probably intentional.

Revision history for this message
John Vandenberg (jayvdb) wrote :

Yea, I agree `assert 0` is self-evident enough that it shouldnt be an error.

tbh, I can't think of a use case for `assert True`, and would happily report an error for it also. In its favour, `assert True` is at least quite clear about what it does and doesnt do.

Revision history for this message
Phil Frost (bitglue) wrote :
Changed in pyflakes:
status: New → Fix Committed
Phil Frost (bitglue)
Changed in pyflakes:
status: Fix Committed → Fix Released
Changed in pyflakes (Debian):
status: Confirmed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.