-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Parameterless generic functions are allowed #2885
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
Comments
I don't think mypy should necessarily prohibit something that "makes no sense". Could this cause a false positive or false negative (uncaught runtime error)? If yes, then those are bugs, if no, then there is no problem. |
I disagree. You could say the same about usage of |
These two cases are different. For |
Okay, that's a point. On the other hand, I could see an user with poor understanding of generics writing something like T = TypeVar('T')
U = TypeVar('U')
def f(x: T) -> U: ... but as soon as he would provide some implementation for If no one else considers this a bug, I can close this. |
There actually is a valid reason to only use
(Also, However, I can't think of any real use case where plain |
IMO, identifying every possible non-sensical type annotation in |
While thinking about whether there are possible use cases for returning a plain type variable I discovered that mypy behaves strangely in case of a restricted type variable: from typing import TypeVar
class A: ...
class B: ...
T = TypeVar('T', A, B)
def f() -> T:
if bool():
return A() # E: Incompatible return value type (got "A", expected "B")
else:
return B() # E: Incompatible return value type (got "B", expected "A") Note how two error messages contradict each other. |
Regarding @JukkaL's example of returning |
Raising priority to normal since this appeared couple times again. |
Note that mypy actually complains about an invalid return in such functions, see #7702. So I think it makes sense to instead give a dedicated error message explaining why it is bad, or better just with a links to some docs about this. |
Here is a use case I encountered for the more general notion of generic functions that don't accept generic parameters. Suppose a function calls a specific REST API with the given path and query and returns the result of |
Hmm... I take it all back. You could just return |
This was fixed by #13166. |
Mypy is absolutely OK with this code, but it makes no sense (it is not possible to return
T
without argument).The text was updated successfully, but these errors were encountered: