microsoft / pyright

Static Type Checker for Python
Other
13.09k stars 1.4k forks source link

`TypeVar` bound to `Annotated[...]` results in downstream false positives. #8454

Closed patrick-kidger closed 1 month ago

patrick-kidger commented 1 month ago

The following:

from typing import Generic, TypeVar, Annotated

_T = TypeVar("_T")

class Foo(Generic[_T]):
    def data(self) -> _T:
        ...

Foo[Annotated[dict, ""]]().data()[""]

raises an error on the final line:

tmp3.py:9:1 - error: "__getitem__" method not defined on type "Annotated" (reportIndexIssue)

Replacing the _T with a direct Annotated[dict, ""] works.

Expected behaviour is for the Annotated to be 'transparent', so that lookup works as if .data() gives a dictionary.

pyright version 1.1.372 ran from the command line.

(PS - thanks for your message on jaxtyping! Looking forward to seeing where this goes :) )

erictraut commented 1 month ago

Thanks for reporting. This will be fixed in the next release.

erictraut commented 1 month ago

This is addressed in pyright 1.1.373.