Skip to content

Conversation

@davidhewitt
Copy link
Member

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.

Comment on lines +56 to 57
@pytest.mark.parametrize("ty", (EqDefaultNe, PyEqDefaultNe), ids=("rust", "python"))
def test_eq_default_ne(ty: Type[Union[EqDefaultNe, PyEqDefaultNe]]):
Copy link
Member Author

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.

Comment on lines +152 to +153
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__`.
Copy link
Member Author

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.

Comment on lines 806 to 818
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()))
}
}
}
Copy link
Member Author

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. :)

Copy link
Member Author

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)

@davidhewitt davidhewitt mentioned this pull request Oct 11, 2023
@davidhewitt davidhewitt added the CI-skip-changelog Skip checking changelog entry label Oct 11, 2023
@davidhewitt
Copy link
Member Author

Skip changelog as __ne__ is a new feature shipping in 0.20.

Copy link
Member

@mejrs mejrs left a 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 :)

@davidhewitt
Copy link
Member Author

Thanks!

@davidhewitt davidhewitt added this pull request to the merge queue Oct 11, 2023
Merged via the queue into PyO3:main with commit b03c4cb Oct 11, 2023
@davidhewitt davidhewitt deleted the default-ne branch October 11, 2023 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI-skip-changelog Skip checking changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update docs for new __eq__ support

2 participants