-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
TypeVar bound to Never when mypy can't infer the appropriate value #18154
Comments
As a workaround, you can try giving _T = TypeVar("_T", covariant=True, default=int)
reveal_type(decorator_factory()) # N: "def [_P] (def (*_P.args, **_P.kwargs) -> builtins.int) -> def (*_P.args, **_P.kwargs) -> builtins.int"
reveal_type(Class.method_one) # N: "def (self: SCRATCH.Class) -> builtins.int"
reveal_type(Class().method_one()) # N: "builtins.int" Using For the record, while pyright doesn't complain, it's just as confused as mypy about how to bind # pyright 1.1.389
reveal_type(decorator_factory()) # Type of "decorator_factory()" is "((**_P@decorator_factory) -> Unknown) -> ((**_P@decorator_factory) -> Unknown)"
reveal_type(Class.method_two) # Type of "Class.method_two" is "(self: Class) -> Unknown"
reveal_type(Class().method_two()) # Type of "Class().method_two()" is "Unknown" |
Pyright is not confused here; it's working correctly. It binds the TypeVar based on the default value |
It's essentially #3737 - consider using a pair of overloads with and without the argument. |
Closing because I agree it's essentially the same issue. |
In the second call to
decorator_factory()
with no arguments,_T
should be bound toint
. I understand that inferring _T=int is maybe too much for mypy, but I believe_T
should then be bound toAny
, or maybeobject
to be extra strict, but definitely notNever
, which is what currently happens.https://mypy-play.net/?mypy=master&python=3.13&gist=4a1aeecb04582945c4fa78f196bace5f
For the record, pyright has no complaints about this code.
The text was updated successfully, but these errors were encountered: