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 F821 on circular reference in TYPE_CHECKING block #11245

Open
ngnpope opened this issue May 2, 2024 · 4 comments · May be fixed by #11262
Open

False positive for F821 on circular reference in TYPE_CHECKING block #11245

ngnpope opened this issue May 2, 2024 · 4 comments · May be fixed by #11262
Assignees
Labels
bug Something isn't working

Comments

@ngnpope
Copy link
Contributor

ngnpope commented May 2, 2024

Keywords searched for before creating issue: F821, TYPE_CHECKING.

Example code that produces the false positive:

from __future__ import annotations

from typing import TYPE_CHECKING, TypeAlias

if TYPE_CHECKING:
    Nested: TypeAlias = dict[str, bool | Nested]

Command line and output:

$ ruff --version
ruff 0.4.2
$ ruff check --isolated --select=F821 bug.py 
bug.py:6:42: F821 Undefined name `Nested`
Found 1 error.
$ mypy --strict bug.py 
Success: no issues found in 1 source file

Expected that F821 would not be raised in a TYPE_CHECKING block for a circular reference when using postponed evaluation of annotations (PEP 563).

@trag1c
Copy link
Contributor

trag1c commented May 2, 2024

nit: indeed, PEP 563 applies to annotations, but dict[str, bool | Nested] isn't one (in fact the __future__ import doesn't make a difference).

I think the issue is still valid though 👍

@charliermarsh charliermarsh added the bug Something isn't working label May 2, 2024
@charliermarsh
Copy link
Member

Not sure... @AlexWaygood would know.

@AlexWaygood
Copy link
Member

AlexWaygood commented May 2, 2024

Yeah I think from __future__ import annotations is irrelevant here, but if it's in an if TYPE_CHECKING block we should treat it as though it has "stub file" semantics (whether or not __future__ annotations are enabled). The rhs of an assignment is evaluated at runtime regardless of whether __future__ annotations are enabled, but nothing in an if TYPE_CHECKING block is ever evaluated at runtime, so it doesn't matter

TL;DR: I agree that this is a false positive

@AlexWaygood
Copy link
Member

If you tried to use Nested in a type annotation anywhere without either quoting it or having __future__ annotations enabled, it would fail, because the alias doesn't exist at runtime. But that's a separate issue

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.

4 participants