Repository navigation
Mypy signals error with Protocol and __rpow__ method with three arguments #10786
Description
Activity
Actually, scratch that. Here's a simpler test case that still breaks on
__rpow__(and only on__rpow__):# test_case2.py from typing import Protocol, runtime_checkable @runtime_checkable class PowableProto(Protocol): def __pow__(self, other: "PowableProto", modulo: "PowableProto") -> "PowableProto": # <- fine ... def __rpow__(self, other: "PowableProto", modulo: "PowableProto") -> "PowableProto": # <- this breaks ...
This (in the style of using ellipses for default arguments in stub files) works, though:
# test_case3.py from typing import Protocol, runtime_checkable @runtime_checkable class PowableProto(Protocol): def __pow__(self, other: "PowableProto", modulo: "PowableProto" = ...) -> "PowableProto": # <- this now breaks ... def __rpow__(self, other: "PowableProto", modulo: "PowableProto" = ...) -> "PowableProto": # <- this still breaks ...
The callout of
__rpow__still seems…weird. 🤨- changed the title
[-]Protocol doesn't like second @overload-ed __rpow__ method—and *only* the second @overload-ed __rpow__ method[/-][+]Mypy signals error with Protocol and __rpow__ method with three arguments[/+]on Jul 8, 2021 I don't think it is related to recursive types. Mypy has strong requirements for Python special methods, and FWIW I think it is fine to relax those for protocols, probably an easy fix.
- added a commit that references this issue
on Sep 3, 2023 I think the error is correct in this case, isn't it? The three argument version of
__pow__is called when the built-inpowfunction is called, but the python docs explicitly mention that__rpow__does not use the three argument version. From https://docs.python.org/3/reference/datamodel.htmlNote that ternary pow() will not try calling rpow() (the coercion rules would become too complicated).
Since Python 3.14, the 3-argument
__rpow__for ternarypowis supported: python/cpython#130251. The 3.14 relnotes also mention this: https://docs.python.org/3/whatsnew/3.14.html#built-insReacted by Matt BogosianSince Python 3.14, the 3-argument
__rpow__for ternarypowis supported: python/cpython#130251. The 3.14 relnotes also mention this: https://docs.python.org/3/whatsnew/3.14.html#built-insHmmm...then maybe @Cnoor0171 was right with #10786 (comment) ? My first two test cases still produce errors:
% /python3 --version Python 3.14.6 % mypy --version mypy 2.1.0 (compiled: yes) % mypy test_case.py test_case2.py test_case3.py test_case.py:15: error: Invalid signature "Callable[[PowableProto, PowableProto, PowableProto], PowableProto]" [misc] test_case2.py:7: error: Invalid signature "Callable[[PowableProto, PowableProto, PowableProto], PowableProto]" [misc] Found 2 errors in 2 files (checked 3 source files)
Sounds like this can be closed as either fixed or as-designed?
- added 2 commits that reference this issue
on Aug 7, 2026
Okay, this is a head-scratcher. 😕
To Reproduce
Expected vs. Actual Behavior
I would expect at least consistent behavior among the overridden methods. This only crops up with
__rpow__for some reason. (I have tested many others.)Your Environment
--config-file=/dev/null(i.e., force default configuration)mypy.ini(and other config files): N/APython 3.9.6(frompython --version)Darwin Kernel Version 19.6.0: Thu May 6 00:48:39 PDT 2021; root:xnu-6153.141.33~1/RELEASE_X86_64(fromuname -v)