Skip to content

False B031 error when using generator from itertools.groupby in if/else block #4050

@premdas26

Description

@premdas26

After upgrading to v0.0.262, I'm getting a false B031 error for multiple uses of a generator from itertools.groupby:

x = dict()    
for key, group in itertools.groupby(x, lambda x: x):
  if key:
    for n in group:
      print(n)
  else:
      list(group)

This code will result in B031 Using the generator returned from itertools.groupby() more than once will do nothing on the second usage. However, if I do not have a loop in the if block, I do not get the error, so as far as I can tell, it seems to be specific to loops (including comprehensions). This appears to be a case not caught by the fix in #3801

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions