[codex] Allow ternary __rpow__ in protocols#21659
Open
EltonChang1 wants to merge 1 commit into
Open
Conversation
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
There was a problem hiding this comment.
Pull request overview
This PR fixes mypy rejecting protocol declarations that include a ternary __rpow__ method by skipping the reverse-operator signature validation for methods defined on protocol classes. This aligns protocol method declarations with their role as structural requirements rather than concrete runtime operator implementations.
Changes:
- Skip
check_reverse_op_method()for reverse-operator methods when the enclosing class is a protocol (TypeInfo.is_protocol). - Add a regression test ensuring a protocol can declare ternary
__pow__and ternary__rpow__without triggering an “Invalid signature” error.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
mypy/checker.py |
Avoids reverse-operator signature validation for methods declared on protocol classes. |
test-data/unit/check-protocols.test |
Adds a regression test case for a protocol declaring ternary __pow__/__rpow__. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Fixes #10786.
What changed
This skips the concrete reverse-operator signature validation for methods declared on protocol classes. Protocol declarations are structural requirements, so they should be able to describe a ternary
__rpow__method without mypy rejecting the declaration as an invalid binary reverse-operator signature.A regression test covers a protocol that declares both ternary
__pow__and ternary__rpow__.Validation
pytest -n0 mypy/test/testcheck.py::TypeCheckSuite::check-protocols.test -k testProtocolTernaryRpowpytest -n0 mypy/test/testcheck.py::TypeCheckSuite::check-protocols.testpytest -n0 mypy/test/testcheck.py::TypeCheckSuite::check-classes.test -k 'ReverseOperator or Operator'python -m mypy --config-file=/dev/nullnow reports success.Limitations
I did not run the full mypy test suite locally; the targeted protocol and adjacent operator tests passed.