-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Error inferring typevar on generic classes #18706
Comments
Does this cause any false positives or false negatives? Mypy should eventually bind |
The related problem was that T was not bound to the type in an instance object. The current problem is that T is not bound to the type in the class object. It could produce false negative if the class method is referenced. I know it is not common and a little bit strange. from typing import Iterator, Generic, TypeVar, Callable, Any
T = TypeVar("T", str, int)
class Parent(Generic[T]):
@classmethod
def method_1(cls, value: T) -> T:
return 2*value
def method_2(self, value: T) -> T:
return 2*value
class ChildInt(Parent[int]):
pass
instance = ChildInt()
reveal_type(ChildInt.method_1) # Revealed type is "def (value: builtins.int) -> builtins.int"
reveal_type(instance.method_1) # Revealed type is "def (value: builtins.int) -> builtins.int"
reveal_type(ChildInt.method_2) # Revealed type is "def (self: __main__.Parent[T`1], value: T`1) -> T`1"
reveal_type(instance.method_2) # Revealed type is "def (value: builtins.int) -> builtins.int"
def decorator[S](f: Callable[[S, int], int]) -> Callable[[S, int], int]:
return f
class GrandChildInt(ChildInt):
method_3 = decorator(ChildInt.method_2) # error: Argument 1 to "decorator" has incompatible type "Callable[[Parent[T], T], T]"; expected "Callable[[Parent[T], int], int]" [arg-type] https://mypy-play.net/?mypy=latest&python=3.12&gist=7ce2739ccc2b43f37187d54dcdd5f923 |
FYI: PyRigth reveal ChildInt.method_2 as |
Bug Report
The inferred type of methods of a generic class are not bounded to typevar value on descendant classes.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=7ce2739ccc2b43f37187d54dcdd5f923
Expected Behavior
This soulution could be acceptable, as well:
Actual Behavior
Your Environment
mypy.ini
(and other config files): no configReleated issues
#15658
The text was updated successfully, but these errors were encountered: