Skip to content

[codex] Allow ternary __rpow__ in protocols#21659

Open
EltonChang1 wants to merge 1 commit into
python:masterfrom
EltonChang1:codex/allow-protocol-rpow-modulo
Open

[codex] Allow ternary __rpow__ in protocols#21659
EltonChang1 wants to merge 1 commit into
python:masterfrom
EltonChang1:codex/allow-protocol-rpow-modulo

Conversation

@EltonChang1

Copy link
Copy Markdown

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 testProtocolTernaryRpow
  • pytest -n0 mypy/test/testcheck.py::TypeCheckSuite::check-protocols.test
  • pytest -n0 mypy/test/testcheck.py::TypeCheckSuite::check-classes.test -k 'ReverseOperator or Operator'
  • Manual reproducer from the issue with python -m mypy --config-file=/dev/null now reports success.

Limitations

I did not run the full mypy test suite locally; the targeted protocol and adjacent operator tests passed.

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@EltonChang1 EltonChang1 marked this pull request as ready for review June 30, 2026 01:06
Copilot AI review requested due to automatic review settings June 30, 2026 01:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mypy signals error with Protocol and __rpow__ method with three arguments

2 participants