Skip to content

Allow __rpow__ to accept an optional third modulo argument - #21860

Open
fahadhewad wants to merge 1 commit into
python:masterfrom
fahadhewad:allow-ternary-rpow
Open

Allow __rpow__ to accept an optional third modulo argument#21860
fahadhewad wants to merge 1 commit into
python:masterfrom
fahadhewad:allow-ternary-rpow

Conversation

@fahadhewad

Copy link
Copy Markdown

Fixes #10786.

check_reverse_op_method validated every reverse operator method against a signature taking exactly two positional arguments, so mypy reported an Invalid signature error for any rpow that declares a third modulo argument. The data model documents rpow as taking an optional modulo argument, and typeshed itself declares the ternary form for int, float and complex, so valid code was being rejected.

The check also runs once per overload item, so an overloaded rpow was flagged even when a two argument variant was declared alongside the ternary one. That is the case reported in the issue.

rpow is now accepted with either two or three positional arguments. All other reverse operator methods are unchanged, and an rpow with fewer than two or more than three arguments is still reported.

A test case in test-data/unit/check-classes.test covers the ternary form, the overloaded form from the issue, an rpow with too many arguments, and a three argument radd that must still be rejected. The full check suite, the self check, black and ruff all pass locally.

check_reverse_op_method validated every reverse operator method against a
signature taking exactly two positional arguments, so mypy reported
"Invalid signature" for any __rpow__ declaring a third modulo argument.

The data model documents __rpow__ as object.__rpow__(self, other[, modulo]),
and typeshed declares the ternary form for int, float and complex. The check
also runs once per overload item, so an overloaded __rpow__ was rejected even
when a two argument variant was declared alongside the ternary one.

__rpow__ is now accepted with either two or three positional arguments. All
other reverse operator methods are unchanged, and an __rpow__ with fewer than
two or more than three arguments is still reported.

Fixes python#10786
@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. ✅

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

1 participant