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 on type alias with Annotated and multiple type arguments upon import #9273

Closed
luwqz1 opened this issue Oct 20, 2024 · 3 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@luwqz1
Copy link

luwqz1 commented Oct 20, 2024

I made a type alias with Annotated, which has two arguments in the generic. Everything works correctly, but as soon as i import this type alias into another file, some strange behavior occurs. Unfortunately, I couldn't reproduce other examples, so I can't pinpoint the exact issue. Eric Traut, i hope you'll be able to figure it out, though it's possible that Pyright is working as intended. I'm always open to discussion!

File foo.py

import typing

First = typing.TypeVar("First")
Meta = typing.TypeVar("Meta")

type FooType[First, Meta] = typing.Annotated[First, Meta]
Foo: typing.TypeAlias = FooType[First, Meta]

def bar(foo: Foo[str, int]): ...  # Ok

File bar.py

from foo import Foo

def bar(foo: Foo[str, int]): ... # Error: Too many type arguments provided for "type[First@Foo]"; expected 1 but received 2
@luwqz1 luwqz1 added the bug Something isn't working label Oct 20, 2024
@luwqz1 luwqz1 changed the title False positive on type alias with Annotated and multiple type arguments upon import False positive on type alias with Annotated and multiple type arguments upon import Oct 20, 2024
erictraut added a commit that referenced this issue Oct 20, 2024
…-style generic type alias whose type is defined as a new-style type alias that has multiple type parameters, only some of which are used in its type definition. This addresses #9273.
erictraut added a commit that referenced this issue Oct 20, 2024
…-style generic type alias whose type is defined as a new-style type alias that has multiple type parameters, only some of which are used in its type definition. This addresses #9273. (#9274)
@erictraut
Copy link
Collaborator

I agree this is a bug. It will be addressed in the next release.

What you're doing here is pretty suspect though. Type parameters are intended to parameterize types. Here you're using a type parameter that appears only within an Annotated metadata expression, not within the base type expression. Any type argument supplied for the second type parameter will therefore be ignored by static type checkers. I guess this construct might be valid if you intend to use it with a runtime type checker or perform introspection of annotations in your own code.

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Oct 20, 2024
@luwqz1
Copy link
Author

luwqz1 commented Oct 20, 2024

I guess this construct might be valid if you intend to use it with a runtime type checker or perform introspection of annotations in your own code.

Thank you very much for such a quick answer! You are absolutely right, i plan to use the second type parameter at runtime for other libraries that may perform checks/validation.

@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.386

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