detect compare used instead of an iterable

Bug #1614338 reported by John Vandenberg
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Pyflakes
New
Undecided
Unassigned

Bug Description

The following will always result in
   TypeError: 'bool' object is not iterable

for a in (1 == 2):
    pass

It is fairly easy to detect as it is a compare in a for loop.

Revision history for this message
asmeurer (asmeurer) wrote :

Seems like part of a larger issue of propagating type inference from literals. int.__eq__(int) returns bool; bool.__iter__() raises TypeError. I would love to see pyflakes do this, but it would be better to do it in a generic way, rather than picking and choosing random examples.

Note that == for general objects could return anything because of __eq__ (and there are definitely some libraries that use it as syntactic sugar).

Revision history for this message
asmeurer (asmeurer) wrote :

In particular, if a and b are numpy arrays, then a == b is a numpy array (which is iterable).

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

Ya, sorry this is a specific case. I found it while working on https://github.com/PyCQA/pycodestyle/issues/371#issuecomment-240614113 , and thought I better raise it here before I forgot it.

It is unfortunate that it can only easily be done for literals and built in types, and needs to understand the classes possibly defined in other modules before it could reliably work for anything else.

Im not sure how valuable it is if it only supports literals and built in types.

Revision history for this message
asmeurer (asmeurer) wrote :

I think if it were done generically it would be very useful, because it would catch a lot of typos. Even just spellchecking attribute names on literals would help me out personally quite a bit.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

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