Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
premdas26 opened this issue Apr 20, 2023 · 2 comments · Fixed by #4070
Closed

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

premdas26 opened this issue Apr 20, 2023 · 2 comments · Fixed by #4070
Assignees
Labels
bug Something isn't working

Comments

@premdas26
Copy link

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

@dhruvmanila
Copy link
Member

Interesting! I see the problem. The global count is being increased instead of the branch local count. This leads to updating the global count twice which means that the line list(group) will be where this false positive is being detected at.

Let me take a look at it, I think even comprehensions might be affected.

@charliermarsh charliermarsh added the bug Something isn't working label Apr 21, 2023
@premdas26
Copy link
Author

great thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants