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

Fix for F811 (Redefinition of unused) removes import needed at runtime #10384

Closed
Hnasar opened this issue Mar 13, 2024 · 4 comments
Closed

Fix for F811 (Redefinition of unused) removes import needed at runtime #10384

Hnasar opened this issue Mar 13, 2024 · 4 comments
Assignees
Labels
bug Something isn't working rule Implementing or modifying a lint rule

Comments

@Hnasar
Copy link
Contributor

Hnasar commented Mar 13, 2024

If you have a module and a symbol of the same name, https://docs.astral.sh/ruff/rules/redefined-while-unused/ may result in broken code.
Using version: 0.3.2

Repro

import datetime
from datetime import datetime
datetime(1, 2, 3)
$ ruff foo.py  --fix

Expected

The first import (shadowed) should be removed.

Observed

--- foo.py
+++ foo.py
@@ -2,6 +2,4 @@
 
 import datetime
 
-from datetime import datetime
-
 print(datetime(1, 2, 3))
$ python3 foo.py 
Traceback (most recent call last):
  File "foo.py", line 5, in <module>
    datetime(1, 2, 3)
TypeError: 'module' object is not callable. Did you mean: 'datetime.datetime(...)'?
@zanieb zanieb added bug Something isn't working rule Implementing or modifying a lint rule labels Mar 13, 2024
@zanieb
Copy link
Member

zanieb commented Mar 13, 2024

Thanks for the clear report! This one looks tricky.

@charliermarsh charliermarsh self-assigned this Mar 13, 2024
@charliermarsh
Copy link
Member

I'll take a look!

charliermarsh added a commit that referenced this issue Mar 13, 2024
This ensures that we don't have incorrect, automated fixes for shadowed
names that actually point to different imports.

See: #10384.
@MichaReiser
Copy link
Member

MichaReiser commented Mar 18, 2024

@charliermarsh is this fixed by your PR or could you explain what's still missing.

@charliermarsh
Copy link
Member

Yeah it’s fixed.

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

No branches or pull requests

4 participants