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

Some improvements to __richcmp__ on enums #2622

Merged
merged 2 commits into from Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 16 additions & 13 deletions pyo3-macros-backend/src/pyclass.rs
Expand Up @@ -526,13 +526,6 @@ fn impl_enum_class(
};

let (default_richcmp, default_richcmp_slot) = {
let variants_eq = variants.iter().map(|variant| {
let variant_name = variant.ident;
quote! {
(#cls::#variant_name, #cls::#variant_name) =>
Ok(true.to_object(py)),
}
});
let mut richcmp_impl: syn::ImplItemMethod = syn::parse_quote! {
fn __pyo3__richcmp__(
&self,
Expand All @@ -544,16 +537,26 @@ fn impl_enum_class(
use ::core::result::Result::*;
match op {
_pyo3::basic::CompareOp::Eq => {
let self_val = self.__pyo3__int__();
if let Ok(i) = other.extract::<#repr_type>() {
let self_val = self.__pyo3__int__();
return Ok((self_val == i).to_object(py));
}
let other = other.extract::<_pyo3::PyRef<Self>>()?;
let other = &*other;
match (self, other) {
#(#variants_eq)*
_ => Ok(false.to_object(py)),
if let Ok(other) = other.extract::<_pyo3::PyRef<Self>>() {
return Ok((self_val == other.__pyo3__int__()).to_object(py));
}

return Ok(py.NotImplemented());
}
_pyo3::basic::CompareOp::Ne => {
let self_val = self.__pyo3__int__();
if let Ok(i) = other.extract::<#repr_type>() {
return Ok((self_val != i).to_object(py));
}
if let Ok(other) = other.extract::<_pyo3::PyRef<Self>>() {
return Ok((self_val != other.__pyo3__int__()).to_object(py));
}

return Ok(py.NotImplemented());
}
_ => Ok(py.NotImplemented()),
}
Expand Down
10 changes: 5 additions & 5 deletions tests/test_enum.rs
Expand Up @@ -52,23 +52,23 @@ fn test_enum_arg() {
}

#[test]
fn test_enum_eq() {
fn test_enum_eq_enum() {
Python::with_gil(|py| {
let var1 = Py::new(py, MyEnum::Variant).unwrap();
let var2 = Py::new(py, MyEnum::Variant).unwrap();
let other_var = Py::new(py, MyEnum::OtherVariant).unwrap();
py_assert!(py, var1 var2, "var1 == var2");
py_assert!(py, var1 other_var, "var1 != other_var");
py_assert!(py, var1 var2, "(var1 != var2) == False");
})
}

#[test]
fn test_default_repr_correct() {
fn test_enum_eq_incomparable() {
Python::with_gil(|py| {
let var1 = Py::new(py, MyEnum::Variant).unwrap();
let var2 = Py::new(py, MyEnum::OtherVariant).unwrap();
py_assert!(py, var1, "repr(var1) == 'MyEnum.Variant'");
py_assert!(py, var2, "repr(var2) == 'MyEnum.OtherVariant'");
py_assert!(py, var1, "(var1 == 'foo') == False");
py_assert!(py, var1, "(var1 != 'foo') == True");
})
}

Expand Down