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
Does this cause any false positives or false negatives? Mypy should eventually bind self and then at that point it will find what T is. (I don't know how to interpret the related issue)
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.
fromtypingimportIterator, Generic, TypeVar, Callable, AnyT=TypeVar("T", str, int)
classParent(Generic[T]):
@classmethoddefmethod_1(cls, value: T) ->T:
return2*valuedefmethod_2(self, value: T) ->T:
return2*valueclassChildInt(Parent[int]):
passinstance=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"defdecorator[S](f: Callable[[S, int], int]) ->Callable[[S, int], int]:
returnfclassGrandChildInt(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]
This is another branch missing in checkmember.py: analyze_class_attribute_access (and precisely add_class_tvars?) does not map instance methods properly. A map_self helper similar to bind_self should help, do we have it somewhere? In all other cases using checker.bind_and_map_method and its helpers suffices, but here we do need to preserve the type of the first argument. (it isn't as trivial as "replace with current self type" due to self-type constraints on overloads and mapping generics)
Activity
A5rocks commentedon Feb 19, 2025
Does this cause any false positives or false negatives? Mypy should eventually bind
selfand then at that point it will find whatTis. (I don't know how to interpret the related issue)alfred82santa commentedon Feb 19, 2025
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.
https://mypy-play.net/?mypy=latest&python=3.12&gist=7ce2739ccc2b43f37187d54dcdd5f923
alfred82santa commentedon Feb 19, 2025
FYI: PyRigth reveal ChildInt.method_2 as
def (self: __main__.ChildInt, value: builtins.int) -> builtins.inthttps://pyright-play.net/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoCSMApiAIYy4A0UA4sSqUgMY0AqixAamSDQMJkANkLIAjIcRoBBFHABQ8tlAC8UDgm68AFACI2umgGcYfTChgBKRc1FGjUAAq8GMbfUYgWAbTYBdSwAuRShQqAABWzJ7CGIYAAswABN5MKgk4mAoWITkgH0ARm1bIxoAN2EAV2JA9UsoAFoAPnVgtLSQOMqQFCgAJgAqCqFq1PbQsdCMrJzEpLy%2B7SNiIWByqpq6xpa2NvGoTphu3sHh0f2oeSj7KH54pCEkggttZ06Lb1QYAL3QhGijIpUCYyChmMRVLd7o9nm5rPJOmViMI8vBNNo7g8nhYAHSzfIFepQADEUAASsQkcJiElYJxMA5dNMoNozpsxJUHjBgTivvVmlAOVyeV9dAjKcihKjONpgTBQeC8XE5oUiaSKVTJLS0RCkIzmayNrUhUJuSgjLyLPyWiazRbRYpEZLpejMTDcfj5n01eSJdTtfS9VAmZkWctVrU8nkIGRUFGcW9XL4AAYFPzrEabNip63qVNip0onWy83ysHEJW5L0%2BjWSml0zQM4MGtnGzmmkVW7aC9t2y0wMXyZkZZi4Ci4bwAZT82mAtUEInEkm8U5oX3T5m%2BuYXogkxBXk7XFg369%2BBy6PSgwEmlzSVzsDlo5BQSTd2Lcb9hQRvN89eQAzJCI5jpQIAYtC76Viq3qhKSpDgCAtTSCAaCVLEFhQAUsBgM2xCjuQoG6FA8TROYo7YBQSB7g2EK6DuS77t4iYfP47Abv4ugANxQMQAAemjMCQtJ0cIu7LkxLgsSex5Ht8RFQN4vBoA0Op%2BEAA
sterliakov commentedon Feb 22, 2025
This is another branch missing in
checkmember.py:analyze_class_attribute_access(and preciselyadd_class_tvars?) does not map instance methods properly. Amap_selfhelper similar tobind_selfshould help, do we have it somewhere? In all other cases usingchecker.bind_and_map_methodand its helpers suffices, but here we do need to preserve the type of the first argument. (it isn't as trivial as "replace with current self type" due to self-type constraints on overloads and mapping generics)A5rocks commentedon Feb 22, 2025
infer_type_argumentswithisuperbut polymorphic? Like something like this: https://github.com/python/mypy/pull/18401/files#diff-1dce7e2a19559e2ff4e08fd46877b87c9cfec3cb5e6d465157788542948e11e4R369-R370 (not sure if that would work ifisuperis still generic likelist[T]and notlist[str]...)sterliakov commentedon Feb 22, 2025
That's what
bind_selfdoes, yep, I hoped there's something higher-level somewhere:)Use checkmember.py to check protocol subtyping (python#18943)
Use checkmember.py to check protocol subtyping (python#18943)