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 on F811 "Redefinition of unused" when using contextlib.suppress #12309

Closed
masklinn opened this issue Jul 13, 2024 · 1 comment · Fixed by #12311
Closed

False positive on F811 "Redefinition of unused" when using contextlib.suppress #12309

masklinn opened this issue Jul 13, 2024 · 1 comment · Fixed by #12311
Assignees
Labels
bug Something isn't working

Comments

@masklinn
Copy link

I assume this is a generalised problem with context managers being able to influence control flow but I figure contextlib.suppress could be special-cased maybe?

import contextlib

foo = None
with contextlib.suppress(ImportError):
    from some_module import foo


bar = None
try:
    from some_module import bar
except ImportError:
    pass

print(foo, bar)

the two bits setting "foo" and "bar" are functionally equivalent, but in the first one Ruff reports

test.py:5:29: F811 Redefinition of unused `foo` from line 3
  |
3 | foo = None
4 | with contextlib.suppress(ImportError):
5 |     from some_module import foo
  |                             ^^^ F811
  |
  = help: Remove definition: `foo`

while the second one works fine.

@charliermarsh
Copy link
Member

This is interesting, agree that it's a bug.

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.

2 participants