Skip to content

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

@smackesey

Description

@smackesey

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.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions