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
A function returning TypeVar should receive at least one argument containing the same Typevar #13765
mypy recently (first noticed today: 2022-09-29) started failing my code with the following:
A function returning TypeVar should receive at least one argument containing the same Typevar
I did a cursory search and found no reference to this rule. Personally, I find it unnecessarily restrictive. For example, an object method could return that TypeVar without having any arguments; i.e., just relying on the object's state. Nonetheless, I assume it was added for good reason. Can that reason please be elaborated on somewhere in the documentation and/or mypy's output?
mypy won't complain about the situation you suggested (method of generic class being different from a function):
from typing import *
T = TypeVar("T")
class X(Generic[T]):
def f(self) -> T: ...
The reason for the lint is that a single TypeVar (in all the cases that mypy warns about) will not do anything.
Generics and TypeVars are parts of the typing system users find complicated, so warning when a type hint isn't doing anything helps such users detect misuse.
Reacted by Christopher Harrison and Jonathon Vandezande
class StaticFactoryClassWithError:
@staticmethod
def create() -> T:
doesn't do anything.
In your real code, you want to just return ProtocolShipper, not ProtocolShipperType. I added a suggestion for that which didn't quite make it into release 0.980: #13730
If someone has a concrete suggestion on how to improve the docs or error messages, open a PR.
Seems to me that fully generic functions are not yet supported. We can have generic functions if and only if the function has a generic input, but we miss the case for when a function has no input and wants to return a generic output.
Can someone confirm or correct me? @hauntsaninja
Is the following achievable?
defsome_generic_func(someBool : bool) ->T:
if(someBool):
returnSomeType#someType extends Telse:
returnSomeOtherType#someOtherType also extends T
The advantage is code that returns a concrete instance without requiring to cast. For example, this:
@alelom you can achieve what you want with overloads. If the function simply returns T, there is no way for the type checker to know that someBool being True leads to a particular return type.
Activity
hauntsaninja commentedon Sep 30, 2022
mypy won't complain about the situation you suggested (method of generic class being different from a function):
The reason for the lint is that a single TypeVar (in all the cases that mypy warns about) will not do anything.
Generics and TypeVars are parts of the typing system users find complicated, so warning when a type hint isn't doing anything helps such users detect misuse.
francescayeye commentedon Oct 1, 2022
I have the same failure reason with the above code, for
StaticFactoryClassWithError.create()but not forStaticFactoryClassWithoutError.create().to me being
create()a static method makes sense to not have any argument containing the same TypeVarthis does not fail as well
you can see the real code at https://github.com/elastic/elastic-serverless-forwarder/blob/main/shippers/factory.py
hauntsaninja commentedon Oct 1, 2022
doesn't do anything.
In your real code, you want to just return ProtocolShipper, not ProtocolShipperType. I added a suggestion for that which didn't quite make it into release 0.980: #13730
If someone has a concrete suggestion on how to improve the docs or error messages, open a PR.
francescayeye commentedon Oct 1, 2022
I've hinted that just after sending my comment. but thanks for the quick reply :)
(I have indeed the correct return type in the factory for ProtocolStorage)
francescayeye commentedon Oct 1, 2022
Indeed I remember now why I used the
ProtocolShipperTypeI have somewhere else:
that using
ProtocolShipperinstead produces the following error:pierresouchay commentedon Feb 7, 2023
Any idea how to make code like this (https://stackoverflow.com/a/6798042) compatible?
alelom commentedon Sep 21, 2023
Seems to me that fully generic functions are not yet supported. We can have generic functions if and only if the function has a generic input, but we miss the case for when a function has no input and wants to return a generic output.
Can someone confirm or correct me? @hauntsaninja
Is the following achievable?
The advantage is code that returns a concrete instance without requiring to cast. For example, this:
could be replaced with:
JelleZijlstra commentedon Sep 21, 2023
@alelom you can achieve what you want with overloads. If the function simply returns
T, there is no way for the type checker to know thatsomeBoolbeing True leads to a particular return type.hauntsaninja commentedon Dec 22, 2024
mypy is right to flag that annotation, it literally doesn't mean anything. maybe read the docs before you tag people?