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

silence deprecation warning for enum with custom __eq__ #4692

Merged
merged 4 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/4692.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix incorrect deprecation warning for `#[pyclass] enum`s with custom `__eq__` implementation.
7 changes: 1 addition & 6 deletions pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1908,12 +1908,7 @@ fn pyclass_richcmp_simple_enum(
let deprecation = (options.eq_int.is_none() && options.eq.is_none())
.then(|| {
quote! {
#[deprecated(
since = "0.22.0",
note = "Implicit equality for simple enums is deprecated. Use `#[pyclass(eq, eq_int)]` to keep the current behavior."
)]
const DEPRECATION: () = ();
const _: () = DEPRECATION;
let _ = #pyo3_path::impl_::pyclass::DeprecationTest::<#cls>::new().autogenerated_equality();
}
})
.unwrap_or_default();
Expand Down
46 changes: 46 additions & 0 deletions src/impl_/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,8 @@ macro_rules! generate_pyclass_richcompare_slot {
other: *mut $crate::ffi::PyObject,
op: ::std::os::raw::c_int,
) -> *mut $crate::ffi::PyObject {
impl $crate::impl_::pyclass::HasCustomRichCmp for $cls {}

$crate::impl_::trampoline::richcmpfunc(slf, other, op, |py, slf, other, op| {
use $crate::class::basic::CompareOp;
use $crate::impl_::pyclass::*;
Expand Down Expand Up @@ -1519,6 +1521,50 @@ fn pyo3_get_value<
Ok((unsafe { &*value }).clone().into_py(py).into_ptr())
}

/// Marker trait whether a class implemented a custom comparison. Used to
/// silence deprecation of autogenerated `__richcmp__` for enums.
pub trait HasCustomRichCmp {}

/// Autoref specialization setup to emit deprecation warnings for autogenerated
/// pyclass equality.
pub struct DeprecationTest<T>(Deprecation, ::std::marker::PhantomData<T>);
pub struct Deprecation;

impl<T> DeprecationTest<T> {
#[inline]
#[allow(clippy::new_without_default)]
pub const fn new() -> Self {
DeprecationTest(Deprecation, ::std::marker::PhantomData)
}
}

impl<T> std::ops::Deref for DeprecationTest<T> {
type Target = Deprecation;
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<T> DeprecationTest<T>
where
T: HasCustomRichCmp,
{
/// For `HasCustomRichCmp` types; no deprecation warning.
#[inline]
pub fn autogenerated_equality(&self) {}
}

impl Deprecation {
#[deprecated(
since = "0.22.0",
note = "Implicit equality for simple enums is deprecated. Use `#[pyclass(eq, eq_int)]` to keep the current behavior."
)]
/// For types which don't implement `HasCustomRichCmp`; emits deprecation warning.
#[inline]
pub fn autogenerated_equality(&self) {}
}

#[cfg(test)]
#[cfg(feature = "macros")]
mod tests {
Expand Down
46 changes: 46 additions & 0 deletions tests/test_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use pyo3::prelude::*;
use pyo3::py_run;
use pyo3::types::PyString;

#[path = "../src/tests/common.rs"]
mod common;
Expand Down Expand Up @@ -357,3 +358,48 @@ mod deprecated {
})
}
}

#[test]
fn custom_eq() {
#[pyclass(frozen)]
#[derive(PartialEq)]
pub enum CustomPyEq {
A,
B,
}

#[pymethods]
impl CustomPyEq {
fn __eq__(&self, other: &Bound<'_, PyAny>) -> bool {
if let Ok(rhs) = other.downcast::<PyString>() {
rhs.to_cow().map_or(false, |rhs| self.__str__() == rhs)
} else if let Ok(rhs) = other.downcast::<Self>() {
self == rhs.get()
} else {
false
}
}

fn __str__(&self) -> String {
match self {
CustomPyEq::A => "A".to_string(),
CustomPyEq::B => "B".to_string(),
}
}
}

Python::with_gil(|py| {
let a = Bound::new(py, CustomPyEq::A).unwrap();
let b = Bound::new(py, CustomPyEq::B).unwrap();

assert!(a.as_any().eq(&a).unwrap());
assert!(a.as_any().eq("A").unwrap());
assert!(a.as_any().ne(&b).unwrap());
assert!(a.as_any().ne("B").unwrap());

assert!(b.as_any().eq(&b).unwrap());
assert!(b.as_any().eq("B").unwrap());
assert!(b.as_any().ne(&a).unwrap());
assert!(b.as_any().ne("A").unwrap());
})
}
2 changes: 1 addition & 1 deletion tests/ui/deprecations.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ error: use of deprecated constant `__pyfunction_pyfunction_option_4::SIGNATURE`:
21 | fn pyfunction_option_4(
| ^^^^^^^^^^^^^^^^^^^

error: use of deprecated constant `SimpleEnumWithoutEq::__pyo3__generated____richcmp__::DEPRECATION`: Implicit equality for simple enums is deprecated. Use `#[pyclass(eq, eq_int)]` to keep the current behavior.
error: use of deprecated method `pyo3::impl_::pyclass::Deprecation::autogenerated_equality`: Implicit equality for simple enums is deprecated. Use `#[pyclass(eq, eq_int)]` to keep the current behavior.
--> tests/ui/deprecations.rs:28:1
|
28 | #[pyclass]
Expand Down
Loading