Using dunder parameter name in builtins._SupportsLessThan#4264
Merged
srittau merged 2 commits intopython:masterfrom Jun 25, 2020
Merged
Using dunder parameter name in builtins._SupportsLessThan#4264srittau merged 2 commits intopython:masterfrom
builtins._SupportsLessThan#4264srittau merged 2 commits intopython:masterfrom
Conversation
PEP 544 intentionally did not specify whether method conformance check in protocol inference should look at parameter names. For example, it's up to the type checker to decide whether a class with method defined as `def foo(self, x: int)` would implement a protocol with method `def foo(self, y: int)`. Mypy decided to ignore parameter names altogether, but we Pyre team decided to be more strict and refuse to match different parameter names (as it is unsound to do so when those methods are invoked with named parameters). Since Pyre also rely on the typeshed stubs, we want to make sure at least the important stubs on typeshed conform to our standard. This PR changes `_SupportsLessThan.__lt__` (introduced in python#4192) to use dunder (i.e. positional-only) parameter name specified in PEP484. This change should not affect mypy since it ignores parameter names, but will make Pyre happy when builtin functions like `sorted` is used. Note there are likely to be other cases where we might want this. We'll submit subsequent PRs in a demand-driven fashion.
srittau
approved these changes
Jun 25, 2020
Collaborator
srittau
left a comment
There was a problem hiding this comment.
I think it makes sense for most of these "simple" protocols to use dunder argument names. We also should move this protocol over to _typeshed.pyi at some point.
hauntsaninja
approved these changes
Jun 25, 2020
vishalkuo
pushed a commit
to vishalkuo/typeshed
that referenced
this pull request
Jun 26, 2020
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PEP 544 intentionally did not specify whether method conformance check in protocol inference should look at parameter names. For example, it's up to the type checker to decide whether a class with method defined as
def foo(self, x: int)would implement a protocol with methoddef foo(self, y: int).Mypy decided to ignore parameter names altogether, but we Pyre team decided to be more strict and refuse to match different parameter names (as it is unsound to do so when those methods are invoked with named parameters). Since Pyre also rely on the typeshed stubs, we want to make sure at least the important stubs on typeshed conform to our standard.
This PR changes
_SupportsLessThan.__lt__(introduced in #4192) to use dunder (i.e. positional-only) parameter name specified in PEP484. This change should not affect mypy since it ignores parameter names, but will make Pyre happy when builtin functions likesortedis used.Note there are likely to be other cases where we might want this. We'll submit subsequent PRs in a demand-driven fashion.