-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Warn for incorrect TypeVar usage (with Generics) #16113
Comments
I remember hitting cases like this more generally, but just for the record this currently fails: import typing
T = typing.TypeVar("T")
def f() -> T:
... with this output:
... to try to demonstrate that "more general" case, this doesn't fail: import typing
T = typing.TypeVar("T")
def f() -> list[T]:
... |
Yup, this is a check we added in 0.981 For list[T], I believe it was intentional to not have the warning trigger for it, see #13061 (comment). I'm not sure I agree with that, but would be curious to know what @Dreamsorcerer 's real world code looks like. |
Ah, OK. The case I just saw was a custom user object of |
OK, so I think it's all Generic types that don't trigger the warning. I'm not sure I understand the comment from @JukkaL, as that use case appears to be exactly the same as doing Minimal reproducer:
|
Comparison between mypy and pyright: from typing import AnyStr, TypeVar
T = TypeVar("T")
def fun0() -> T: raise ValueError()
def fun1() -> AnyStr: raise ValueError()
def fun2() -> list[T]: raise ValueError()
def fun3() -> list[AnyStr]: raise ValueError()
def fun4() -> AnyStr | None: raise ValueError()
reveal_type(fun0())
reveal_type(fun1())
reveal_type(fun2())
reveal_type(fun3())
reveal_type(fun4())
|
Feature
Sometimes a user less familiar with more advanced typing concepts will use a TypeVar in a way that doesn't add any typing information.
As an example:
def foo() -> T:
I think, basically, anytime a TypeVar appears only 1 time in a function (or class) definition, this should be treated as an error.
Pitch
Code like this seems like an obvious sign the user has misunderstood TypeVar, and a warning should probably be emitted. They either want a normal type, or they missed an annotation somewhere.
The text was updated successfully, but these errors were encountered: