You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Mypy and Pyright complain when importing new NamedTuple #56
I am trying to use generic NamedTuple from the new typing_extensions, however static checkers can't resolve its import.
Below is the code to reproduce and mypy errors as comments.
fromtypingimportTypeVar, Genericfromtyping_extensionsimportNamedTuple# error: Module "typing_extensions" has no attribute "NamedTuple"T_co=TypeVar('T_co', covariant=True)
classFoo(NamedTuple, Generic[T_co]): # error: Class cannot subclass "NamedTuple" (has type "Any")bar: T_co
Not sure what the problem is. I see that there's a conditional here that checks for Python 3.11. It might be the case that type checkers can't resolve this statically. This is just a hypothesis though.
I forgot to add it to the typeshed stub for typing_extensions -- the stub is all that the type checker sees :) python/typeshed#8295 should fix the issue.
It might be worth noting, however, that mypy doesn't yet support generic NamedTuples (python/mypy#685) -- my typeshed PR will fix the import issue, but mypy will still emit errors if you try to actually use generic NamedTuples. I know pyright supports generic NamedTuples; not sure about pytype and pyre.
You have to wait for the next mypy or pyright release. We can probably get this into mypy 0.970 (expected soon), and pyright releases with updated stubs frequently.
In the meantime, if you're using mypy, one option is to clone typeshed's master branch and use mypy with the --custom-typeshed-dir option. Pyright might have a similar option you can use to override its vendored copy of typeshed, but I'm not sure.
Activity
Add `typing_extensions.NamedTuple`
AlexWaygood commentedon Jul 14, 2022
I forgot to add it to the typeshed stub for
typing_extensions-- the stub is all that the type checker sees :) python/typeshed#8295 should fix the issue.It might be worth noting, however, that mypy doesn't yet support generic
NamedTuples (python/mypy#685) -- my typeshed PR will fix the import issue, but mypy will still emit errors if you try to actually use genericNamedTuples. I know pyright supports genericNamedTuples; not sure about pytype and pyre.gshaikovski commentedon Jul 14, 2022
Great, thanks!
Add `typing_extensions.NamedTuple` (#8295)
gshaikovski commentedon Jul 14, 2022
@AlexWaygood I see the fix was merged. I think the typeshed CI pipeline already ran too since then. How do I get this stub into my env? Thanks.
JelleZijlstra commentedon Jul 14, 2022
You have to wait for the next mypy or pyright release. We can probably get this into mypy 0.970 (expected soon), and pyright releases with updated stubs frequently.
AlexWaygood commentedon Jul 14, 2022
In the meantime, if you're using mypy, one option is to clone typeshed's master branch and use mypy with the
--custom-typeshed-diroption. Pyright might have a similar option you can use to override its vendored copy of typeshed, but I'm not sure.gshaikovski commentedon Jul 14, 2022
Great thanks both!