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

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

Closed
alisaifee opened this issue Jan 18, 2023 · 8 comments

Comments

@alisaifee
Copy link

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
    
@alisaifee alisaifee changed the title Getting F821 Undefined name "xyz" for typing.Literal Getting F821 Undefined name "xyz" for typing.Literal values when imported indirectly Jan 18, 2023
@charliermarsh
Copy link
Member

Yeah we don't track that the re-exported Literal in that case is the same as typing.Literal.

We do have something to support this use-case though. If you want to treat every member of x as if it's a re-export of a typing member, you can do:

[tool.ruff]
typing-modules = ["x"]

(Relevant docs are here: https://github.com/charliermarsh/ruff#typing-modules)

@charliermarsh
Copy link
Member

(The canonical use-case for this is projects that have some kind of compatibility shim over typing and typing-extensions.)

@alisaifee
Copy link
Author

Neat, that is exactly the use case (reference).

@alisaifee
Copy link
Author

I'll close this as it is clearly pointed out in the documentation (😅)

@charliermarsh
Copy link
Member

Oh nice! Good to know that there are more examples of this, and glad it solves your problem :)

@henryiii
Copy link
Contributor

Solves my problem too, thank you! Though I use relative imports, so it seems to not be triggering on from .._compat.typing import Literal. If I change it to absolute, then it works.

@charliermarsh
Copy link
Member

Ah good call -- filed as #2005!

@charliermarsh
Copy link
Member

@henryiii - Heads up, I also checked out scikit_build_core and verified that (e.g.) F821 errors aren't being triggered for Literals with typing-modules = ["scikit_build_core._compat.typing"].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants