Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary operators don't work for __r*__ methods #838

Closed
g-bauer opened this issue Mar 27, 2020 · 0 comments · Fixed by #839
Closed

Binary operators don't work for __r*__ methods #838

g-bauer opened this issue Mar 27, 2020 · 0 comments · Fixed by #839
Assignees
Labels

Comments

@g-bauer
Copy link

g-bauer commented Mar 27, 2020

The implementation as presented here:

#[pyclass]
struct RhsArithmetic {}

#[pyproto]
impl PyNumberProtocol for RhsArithmetic {
    fn __radd__(&self, other: &PyAny) -> PyResult<String> {
        Ok(format!("{:?} + RA", other))
    }
}

leads to the following behaviour when called from python:

# rhs is an instance of RhsArithmetic 
1.0 + rhs         # error:  unsupported operand type(s) for +: 'float' and 'RhsArithmetic'
1.0.__add__(rhs)  # error: NotImplemented
rhs.__radd__(1.0) # works as intended

while we'd expect that 1.0 + rhs should fall back to rhs.__radd__(1.0) which is not working.

From the disussion in gitter with @kngwyu :

We have to fill nb_add field in PyNumberMethods if the object has radd.

@g-bauer g-bauer changed the title Binary opreators don't work for __r*__ methods Binary operators don't work for __r*__ methods Mar 27, 2020
@kngwyu kngwyu added the bug label Mar 27, 2020
@kngwyu kngwyu self-assigned this Mar 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants