-
Notifications
You must be signed in to change notification settings - Fork 909
Fix bug in default implementation of __ne__
#3506
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
Conversation
| @pytest.mark.parametrize("ty", (EqDefaultNe, PyEqDefaultNe), ids=("rust", "python")) | ||
| def test_eq_default_ne(ty: Type[Union[EqDefaultNe, PyEqDefaultNe]]): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was my original bug in the test suite.
| PyO3 supports the usual magic comparison methods available in Python such as `__eq__`, `__lt__` | ||
| and so on. It is also possible to support all six operations at once with `__richcmp__`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
protocols.md already covers __eq__ etc so I think it was just this section of the guide which I found needed updating.
| unsafe fn __ne__( | ||
| self, | ||
| _py: Python<'_>, | ||
| _slf: *mut ffi::PyObject, | ||
| _other: *mut ffi::PyObject, | ||
| py: Python<'_>, | ||
| slf: *mut ffi::PyObject, | ||
| other: *mut ffi::PyObject, | ||
| ) -> PyResult<*mut ffi::PyObject> { | ||
| Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented())) | ||
| // By default `__ne__` will try `__eq__` and invert the result | ||
| let slf: &PyAny = py.from_borrowed_ptr(slf); | ||
| let other: &PyAny = py.from_borrowed_ptr(other); | ||
| if !slf.eq(other)? { | ||
| Ok(ffi::Py_NewRef(ffi::Py_True())) | ||
| } else { | ||
| Ok(ffi::Py_NewRef(ffi::Py_False())) | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... and this is the bugfix. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Adjusted to use a simpler implementation in a force-push)
|
Skip changelog as |
9a7420e to
76f6c45
Compare
76f6c45 to
e1d4173
Compare
mejrs
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just woke up but this seems good to me :)
|
Thanks! |
Closes #3417
While updating the documentation for
__eq__I fortunately stumbled across an error in the testing for__eq__and__ne__which caused us to miss the fact that__ne__was not defaulting to__eq__the same way it does for Python classes.If anyone is available to give this a quick review (hopefully it's not a difficult review) then I'd be very grateful, as I think this blocks the release.