diff --git a/pyo3-derive-backend/src/pyproto.rs b/pyo3-derive-backend/src/pyproto.rs index 8416dfb2e97..9e8bf172e56 100644 --- a/pyo3-derive-backend/src/pyproto.rs +++ b/pyo3-derive-backend/src/pyproto.rs @@ -86,7 +86,8 @@ fn impl_proto_impl( Some(pyo3::class::PyMethodDef { ml_name: stringify!(#name), ml_meth: pyo3::class::PyMethodType::PyCFunctionWithKeywords(__wrap), - ml_flags: pyo3::ffi::METH_VARARGS | pyo3::ffi::METH_KEYWORDS, + // We need METH_COEXIST here to prevent __add__ from overriding __radd__ + ml_flags: pyo3::ffi::METH_VARARGS | pyo3::ffi::METH_KEYWORDS | pyo3::ffi::METH_COEXIST, ml_doc: "" }) } diff --git a/tests/test_arithmetics.rs b/tests/test_arithmetics.rs index 39367406cc7..c30089ae2ee 100755 --- a/tests/test_arithmetics.rs +++ b/tests/test_arithmetics.rs @@ -291,9 +291,11 @@ fn lhs_override_rhs() { let py = gil.python(); let c = PyCell::new(py, LhsAndRhsArithmetic {}).unwrap(); - py_run!(py, c, "assert c.__radd__(1) == '1 + BA'"); + // Not overrided + py_run!(py, c, "assert c.__radd__(1) == '1 + RA'"); + py_run!(py, c, "assert c.__rsub__(1) == '1 - RA'"); + // Overrided py_run!(py, c, "assert 1 + c == '1 + BA'"); - py_run!(py, c, "assert c.__rsub__(1) == '1 - BA'"); py_run!(py, c, "assert 1 - c == '1 - BA'"); }