diff --git a/crates/red_knot_python_semantic/resources/mdtest/binary/instances.md b/crates/red_knot_python_semantic/resources/mdtest/binary/instances.md index fdde6eed2b..ba2b9c5762 100644 --- a/crates/red_knot_python_semantic/resources/mdtest/binary/instances.md +++ b/crates/red_knot_python_semantic/resources/mdtest/binary/instances.md @@ -458,6 +458,25 @@ reveal_type(C() + A()) reveal_type(B() + C()) ``` +### Reflected dunder is not tried between two objects of the same type + +For the specific case where the left-hand operand is the exact same type as the +right-hand operand, the reflected dunder of the right-hand operand is not +tried; the runtime short-circuits after trying the unreflected dunder of the +left-hand operand. For context, see +[this mailing list discussion](https://mail.python.org/archives/list/python-dev@python.org/thread/7NZUCODEAPQFMRFXYRMGJXDSIS3WJYIV/). + +```py +class Foo: + def __radd__(self, other: Foo) -> Foo: + return self + + +# error: [unsupported-operator] +# revealed: Unknown +reveal_type(Foo() + Foo()) +``` + ### Wrong type TODO: check signature and error if `other` is the wrong type diff --git a/crates/red_knot_python_semantic/src/types/infer.rs b/crates/red_knot_python_semantic/src/types/infer.rs index 5e785e62c8..3e0edef49b 100644 --- a/crates/red_knot_python_semantic/src/types/infer.rs +++ b/crates/red_knot_python_semantic/src/types/infer.rs @@ -2682,10 +2682,14 @@ impl<'db> TypeInferenceBuilder<'db> { .call(self.db, &[left_ty, right_ty]) .return_ty(self.db) .or_else(|| { - right_class - .class_member(self.db, op.reflected_dunder()) - .call(self.db, &[right_ty, left_ty]) - .return_ty(self.db) + if left_class == right_class { + None + } else { + right_class + .class_member(self.db, op.reflected_dunder()) + .call(self.db, &[right_ty, left_ty]) + .return_ty(self.db) + } }) }