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

[bug] F401 false positive for imports inside functions #4939

Closed
smackesey opened this issue Jun 7, 2023 · 1 comment · Fixed by #4942
Closed

[bug] F401 false positive for imports inside functions #4939

smackesey opened this issue Jun 7, 2023 · 1 comment · Fixed by #4942
Assignees
Labels
bug Something isn't working

Comments

@smackesey
Copy link

smackesey commented Jun 7, 2023

Contrived example, but ruff 0.0.271 flags the below import inside of fn with F401 (unused import) and auto-deletes it, but this results in a runtime error when you execute the code, because the first argument to cast doesn't get a deferred annotation like a function parameter:

from __future__ import annotations

from typing import TYPE_CHECKING, cast

if TYPE_CHECKING:
    from threading import Thread


def fn(thread: Thread):
    from threading import Thread  # Flagged as F401

    casted_thread = cast(Thread, thread)
    return casted_thread

print(fn(1))

You can work around it by quoting the cast argument, but still a bug.

@smackesey smackesey changed the title F401 false positive for imports inside functions [bug] F401 false positive for imports inside functions Jun 7, 2023
@charliermarsh
Copy link
Member

Oops, thanks. I’ll fix this tonight. I need to cut a new release anyway.

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