Skip to content

F821: false negatives when a file has from __future__ import annotations #10340

@AlexWaygood

Description

@AlexWaygood

Ruff currently emits no F821 errors on this .py file, but the lines marked with XXX all fail at runtime due to undefined names:

from __future__ import annotations

from typing import TypeAlias, Optional, Union

MaybeCStr: TypeAlias = Optional[CStr]  # XXX
MaybeCStr2: TypeAlias = Optional["CStr"]  # always okay
CStr: TypeAlias = Union[C, str]  # XXX
CStr2: TypeAlias = Union["C", str]  # always okay

class C: ...

class Leaf: ...
class Tree(list[Tree | Leaf]): ...  # XXX
class Tree2(list["Tree | Leaf"]): ...  # always okay

Note:

  • Ruff correctly emits three errors on this file when from __future__ import annotations is not included
  • Ruff correctly emits no errors on this file if it's a .pyi stub file.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinglinterRelated to the linter

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions