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

Regression for typing.Annotated when aliased #9092

Closed
fellhorn opened this issue Sep 26, 2024 · 4 comments
Closed

Regression for typing.Annotated when aliased #9092

fellhorn opened this issue Sep 26, 2024 · 4 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@fellhorn
Copy link

fellhorn commented Sep 26, 2024

Describe the bug

Pyright 1.1.379 seems to have introduced a different behavior for typing.Annotated type forms when:

  • enableExperimentalFeatures: true is set
  • typing.Annotated is aliased in another module

I assume this is due to the provisional support for draft PEP 747 and the TypeForm special form.

We discovered this because it breaks jaxtyping type annotations, which internally aliases typing.Annotated.

Code or Screenshots

The file to type check:

from typing import Annotated

# These two alias typing.Annotated
from .indirection import IndirectAnnotated
from jaxtyping import Int32


def test(
    inp1: Annotated[int, "metadata"],  # This is valid
    inp2: IndirectAnnotated[int, "metadata"], # "metadata" is not defined (reportUndefinedVariable)
    inp3: Int32[int, "metadata"], # "metadata" is not defined (reportUndefinedVariable)
):
    pass

indirection.py

from typing import (
    Annotated as IndirectAnnotated,
)

__all__ = [
    "IndirectAnnotated",
]

pyrightconfig.json

{
    "include": [
        "./*",
    ],
    "enableExperimentalFeatures": true,
}

If your code relies on symbols that are imported from a third-party library, include the associated import statements and specify which versions of those libraries you have installed.

indirection.py provides a minimal example for jaxtyping's internal logic.

VS Code extension or command-line

All pyright versions >=1.1.379 as well as pylance v2024.9.101 (pre release) produce the issue:

> pyright -p pyrightconfig.json 
/workspaces/pyright_test/test.py
  /workspaces/pyright_test/test.py:11:35 - error: "metadata" is not defined (reportUndefinedVariable)
  /workspaces/pyright_test/test.py:12:22 - error: "metadata" is not defined (reportUndefinedVariable)
2 errors, 0 warnings, 0 informations 
@fellhorn fellhorn added the bug Something isn't working label Sep 26, 2024
@fellhorn
Copy link
Author

fellhorn commented Sep 26, 2024

I found a smaller example to reproduce the issue in the pyright playground:

Code sample in pyright playground

from typing import Annotated

IndirectAnnotated = Annotated

def test(
    inp1: Annotated[int, "metadata"],  # This is valid
    inp2: IndirectAnnotated[int, "metadata"], # "metadata" is not defined (reportUndefinedVariable)
):
    pass

@erictraut
Copy link
Collaborator

Thanks for helping shake out the bugs in the provisional implementation of TypeForm.

This will be addressed in the next release.

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Sep 26, 2024
@fellhorn
Copy link
Author

Thanks a lot, Eric! It's always amazing to see how quickly you react to bug reports here in pyright.

@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.383.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants