Skip to content

Fix self-types in access to overloaded class methods on instances #7937

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

Merged
merged 1 commit into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ def analyze_instance_member_access(name: str,
# TODO: use proper treatment of special methods on unions instead
# of this hack here and below (i.e. mx.self_type).
dispatched_type = meet.meet_types(mx.original_type, typ)
signature = check_self_arg(signature, dispatched_type, False, mx.context,
name, mx.msg)
signature = bind_self(signature, mx.self_type)
signature = check_self_arg(signature, dispatched_type, method.is_class,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops.

mx.context, name, mx.msg)
signature = bind_self(signature, mx.self_type, is_classmethod=method.is_class)
typ = map_instance_to_supertype(typ, method.info)
member_type = expand_type_by_instance(signature, typ)
freeze_type_vars(member_type)
Expand Down Expand Up @@ -623,6 +623,8 @@ def f(self: S) -> T: ...
if not items:
return functype
new_items = []
if is_classmethod:
dispatched_arg_type = TypeType.make_normalized(dispatched_arg_type)
for item in items:
if not item.arg_types or item.arg_kinds[0] not in (ARG_POS, ARG_STAR):
# No positional first (self) argument (*args is okay).
Expand All @@ -632,8 +634,6 @@ def f(self: S) -> T: ...
return functype
else:
selfarg = item.arg_types[0]
if is_classmethod:
dispatched_arg_type = TypeType.make_normalized(dispatched_arg_type)
if subtypes.is_subtype(dispatched_arg_type, erase_typevars(erase_to_bound(selfarg))):
new_items.append(item)
if not new_items:
Expand Down
32 changes: 32 additions & 0 deletions test-data/unit/check-selftype.test
Original file line number Diff line number Diff line change
Expand Up @@ -937,3 +937,35 @@ t: Type[Union[B, C]]
x = t.meth()[0]
reveal_type(x) # N: Revealed type is 'Union[__main__.B*, __main__.C*]'
[builtins fixtures/isinstancelist.pyi]

[case testSelfTypeClassMethodOverloadedOnInstance]
from typing import Optional, Type, TypeVar, overload, Union

Id = int

A = TypeVar("A", bound=AClass)

class AClass:
@overload
@classmethod
def delete(cls: Type[A], id: Id, id2: Id) -> Optional[int]: ...

@overload
@classmethod
def delete(cls: Type[A], id: A, id2: None = None) -> Optional[int]: ...

@classmethod
def delete(cls: Type[A], id: Union[A, Id], id2: Optional[Id] = None) -> Optional[int]:
...

def foo(x: Type[AClass]) -> None:
reveal_type(x.delete) # N: Revealed type is 'Overload(def (id: builtins.int, id2: builtins.int) -> builtins.int, def (id: __main__.AClass*, id2: None =) -> builtins.int)'
y = x()
reveal_type(y.delete) # N: Revealed type is 'Overload(def (id: builtins.int, id2: builtins.int) -> builtins.int, def (id: __main__.AClass*, id2: None =) -> builtins.int)'
y.delete(10, 20)
y.delete(y)

def bar(x: AClass) -> None:
reveal_type(x.delete) # N: Revealed type is 'Overload(def (id: builtins.int, id2: builtins.int) -> builtins.int, def (id: __main__.AClass*, id2: None =) -> builtins.int)'
x.delete(10, 20)
[builtins fixtures/classmethod.pyi]