Skip to content
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

Fix issubclass for Protocol with overloaded methods #11255

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 9 additions & 3 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ def non_method_protocol_members(tp: TypeInfo) -> List[str]:

for member in tp.protocol_members:
typ = get_proper_type(find_member(member, instance, instance))
if not isinstance(typ, CallableType):
if not isinstance(typ, (CallableType, Overloaded)):
result.append(member)
return result

Expand Down Expand Up @@ -1376,8 +1376,14 @@ def visit_literal_type(self, left: LiteralType) -> bool:
return self._is_proper_subtype(left.fallback, self.right)

def visit_overloaded(self, left: Overloaded) -> bool:
# TODO: What's the right thing to do here?
return False
if isinstance(self.right, Overloaded):
for right_item in self.right.items:
if not any(self._is_proper_subtype(left_item, right_item)
Copy link
Collaborator

Choose a reason for hiding this comment

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

In general, order of overloads matters (think multiple inheritance, or an overload that shadows another one). I think I'd just check that the overloads match exactly, allowing for more items at the end of right. Happily, this would also mean that the check would no longer be quadratic.

Copy link
Author

@eganjs eganjs Oct 4, 2021

Choose a reason for hiding this comment

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

I do not understand why the order of overloads should matter (I'm not aware of other languages where the overloads have order), and I think enforcing them to overlap exactly would be restrictive.

To expand on my thoughts, I think it would restrict otherwise valid structural typing, for example this case (mypy-play example). I might be getting my terminology wrong, but I think this is an example of contravariance, which I think is something that should be permissible with overloads, as it is with other methods on a protocol (mypy-play example). This example is valid under the initial implementation and I do not think it would be if the check was re-implemented the way you have described (perhaps I am wrong here?).

Copy link
Author

@eganjs eganjs Oct 16, 2021

Choose a reason for hiding this comment

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

@hauntsaninja could you please respond and expand more on the reasoning behind what you've said?

for left_item in left.items):
return False
return True
else:
return False

def visit_union_type(self, left: UnionType) -> bool:
return all([self._is_proper_subtype(item, self.orig_right) for item in left.items])
Expand Down
37 changes: 36 additions & 1 deletion test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -2331,7 +2331,7 @@ y: PBad = None # E: Incompatible types in assignment (expression has type "None
[out]

[case testOnlyMethodProtocolUsableWithIsSubclass]
from typing import Protocol, runtime_checkable, Union, Type
from typing import Protocol, runtime_checkable, Union, Type, overload
@runtime_checkable
class P(Protocol):
def meth(self) -> int:
Expand All @@ -2344,6 +2344,41 @@ class C:
x: str
def meth(self) -> int:
pass

class E: pass

cls: Type[Union[C, E]]
issubclass(cls, PBad) # E: Only protocols that don't have non-method members can be used with issubclass() \
# N: Protocol "PBad" has non-method member(s): x
if issubclass(cls, P):
reveal_type(cls) # N: Revealed type is "Type[__main__.C]"
else:
reveal_type(cls) # N: Revealed type is "Type[__main__.E]"
[builtins fixtures/isinstance.pyi]
[typing fixtures/typing-full.pyi]
[out]

[case testProtocolWithOverloadUsableWithIsSubclass]
from typing import Protocol, runtime_checkable, Union, Type, overload
@runtime_checkable
class P(Protocol):
@overload
def meth(self, x: str) -> int: ...
@overload
def meth(self, x: int) -> str: ...
@runtime_checkable
class PBad(Protocol):
x: str

class C:
x: str
@overload
def meth(self, x: str) -> int: ...
@overload
def meth(self, x: int) -> str: ...
def meth(self, x: Union[str, int]) -> Union[int, str]:
pass

class E: pass

cls: Type[Union[C, E]]
Expand Down