-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Fix issubclass
for Protocol with overloaded methods
#11255
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
Conversation
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?).
There was a problem hiding this comment.
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?
issubclass
for Protocol with overloaded methods
@JukkaL would it be possible to get a second opinion on this? |
Thank you for making this PR! #9904 fixed the protocol typing that this PR fixes and various changes on master replaced the overload logic here with existing overload logic that handles order correctly. The mypy-play links here all pass with latest mypy (although there's still at least one bug, fixing in #14018 ) |
I'm glad to see this is now resolved, it's unfortunate that the lack of dialogue resulted in delaying a resolution from being merged for so long. |
Description
Where Protocols are defined with overloaded methods, mypy errors with the message:
See here for an example of the failure:
https://mypy-play.net/?mypy=latest&python=3.10&flags=strict&gist=2d01059b170f6d4c66e8b0af239d5e77
This PR attempts to fix this, by checking explicitly for overloaded methods when checking for compatibility with
issubclass
Test Plan
I have added this additional test: