Skip to content

Getting F821 Undefined name "xyz" for typing.Literal values when imported indirectly #1976

@alisaifee

Description

@alisaifee

Description

Ruff complains with F821 Undefined name "xyz" for the values described in arguments typed as typing.Literal. I was however only able to reproduce this when Literal is imported indirectly (i.e. not from typing import Literal).

As an example, the following file passes all checks:

from typing import Literal

a: Literal["fu", "bar"] = "fu"
a = "bar"

But this does not:

  • x.py

    from typing import Literal
    __all__ = ["Literal"]
  • y.py

    from x import Literal
    a: Literal["fu", "bar"] = "fu"
    a = "bar"
  • Output:

    $ ruff y.py
    y.py:2:12: F821 Undefined name `fu`
    y.py:2:18: F821 Undefined name `bar`
    
  • Reproduced without any configuration changes.

  • Ruff version:

    $ ruff --version
    ruff 0.0.225
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions