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 positive for B031 in mutually exclusive branches #3801

Closed
janosh opened this issue Mar 29, 2023 · 5 comments · Fixed by #3844
Closed

False positive for B031 in mutually exclusive branches #3801

janosh opened this issue Mar 29, 2023 · 5 comments · Fixed by #3844
Labels
bug Something isn't working

Comments

@janosh
Copy link

janosh commented Mar 29, 2023

Ruff v0.0.260

# t.py
import itertools

for key, group in itertools.groupby("foobar"):
    if True:
        print(key, len(tuple(group)))
    else:
        print(key, len(tuple(group)))  # B031
ruff t.py --select B

raises false positive flake8-bugbear B031

Using the generator returned from itertools.groupby() more than once will do nothing on the second usage

@charliermarsh
Copy link
Member

Is this incorrect? flake8-bugbear flags line 7 too.

@charliermarsh charliermarsh added the question Asking for support or clarification label Mar 29, 2023
@janosh
Copy link
Author

janosh commented Mar 29, 2023

If the generator uses are in mutually exclusive execution paths, I think this error should not be raised.

@charliermarsh
Copy link
Member

Ahh yeah, I see. It might be difficult to detect this reliably.

@charliermarsh charliermarsh added bug Something isn't working and removed question Asking for support or clarification labels Mar 29, 2023
@charliermarsh
Copy link
Member

(But yes, it's a false positive.)

@charliermarsh charliermarsh changed the title False positive B031 False positive for B031 in mutually exclusive branches Mar 29, 2023
@AA-Turner
Copy link
Contributor

Thank you @dhruvmanila @charliermarsh!

A

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