From 3eebd9b5ec6c26e80d36a935e643e84e1600be93 Mon Sep 17 00:00:00 2001 From: Will-thom <116388885+Will-thom@users.noreply.github.com> Date: Mon, 25 May 2026 03:44:33 -0300 Subject: [PATCH] Allow reverse operators in protocols --- mypy/checker.py | 3 +++ test-data/unit/check-protocols.test | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/mypy/checker.py b/mypy/checker.py index c35e78d83a60..86ae701546d3 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -1962,6 +1962,9 @@ def check_reverse_op_method( assert defn.info + if defn.info.is_protocol: + return + # First check for a valid signature method_type = CallableType( [AnyType(TypeOfAny.special_form), AnyType(TypeOfAny.special_form)], diff --git a/test-data/unit/check-protocols.test b/test-data/unit/check-protocols.test index 6e86507eb48d..c9309316e5c5 100644 --- a/test-data/unit/check-protocols.test +++ b/test-data/unit/check-protocols.test @@ -1539,6 +1539,17 @@ f(C()) # E: No overload variant of "f" matches argument type "C" \ [builtins fixtures/isinstance.pyi] [typing fixtures/typing-full.pyi] +[case testProtocolReversePowWithModulo] +from typing import overload, Protocol + +class PowableProto(Protocol): + @overload + def __rpow__(self, other: "PowableProto") -> "PowableProto": ... + @overload + def __rpow__( + self, other: "PowableProto", modulo: "PowableProto" + ) -> "PowableProto": ... + -- Unions of protocol types -- ------------------------