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

fix: adding tests for pyclass hygiene cases #4359

Merged
merged 3 commits into from
Jul 17, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,8 +1091,8 @@ fn impl_complex_enum_struct_variant_cls(
let field_getter_impl = quote! {
fn #field_name(slf: #pyo3_path::PyRef<Self>) -> #pyo3_path::PyResult<#field_type> {
match &*slf.into_super() {
#enum_name::#variant_ident { #field_name, .. } => Ok(#field_name.clone()),
_ => unreachable!("Wrong complex enum variant found in variant wrapper PyClass"),
#enum_name::#variant_ident { #field_name, .. } => ::std::result::Result::Ok(#field_name.clone()),
Icxolu marked this conversation as resolved.
Show resolved Hide resolved
_ => ::core::unreachable!("Wrong complex enum variant found in variant wrapper PyClass"),
}
}
};
Expand Down Expand Up @@ -1157,12 +1157,11 @@ fn impl_complex_enum_tuple_variant_field_getters(
}
})
.collect();

let field_getter_impl: syn::ImplItemFn = parse_quote! {
fn #field_name(slf: #pyo3_path::PyRef<Self>) -> #pyo3_path::PyResult<#field_type> {
match &*slf.into_super() {
#enum_name::#variant_ident ( #(#field_access_tokens), *) => Ok(val.clone()),
_ => unreachable!("Wrong complex enum variant found in variant wrapper PyClass"),
#enum_name::#variant_ident ( #(#field_access_tokens), *) => ::std::result::Result::Ok(val.clone()),
_ => ::core::unreachable!("Wrong complex enum variant found in variant wrapper PyClass"),
}
}
};
Expand All @@ -1186,7 +1185,7 @@ fn impl_complex_enum_tuple_variant_len(

let mut len_method_impl: syn::ImplItemFn = parse_quote! {
fn __len__(slf: #pyo3_path::PyRef<Self>) -> #pyo3_path::PyResult<usize> {
Ok(#num_fields)
::std::result::Result::Ok(#num_fields)
}
};

Expand All @@ -1208,7 +1207,7 @@ fn impl_complex_enum_tuple_variant_getitem(
.map(|i| {
let field_access = format_ident!("_{}", i);
quote! {
#i => Ok(
#i => ::std::result::Result::Ok(
#pyo3_path::IntoPy::into_py(
#variant_cls::#field_access(slf)?
, py)
Expand All @@ -1223,7 +1222,7 @@ fn impl_complex_enum_tuple_variant_getitem(
let py = slf.py();
match idx {
#( #match_arms, )*
_ => Err(pyo3::exceptions::PyIndexError::new_err("tuple index out of range")),
_ => ::std::result::Result::Err(#pyo3_path::exceptions::PyIndexError::new_err("tuple index out of range")),
}
}
};
Expand Down Expand Up @@ -1828,7 +1827,7 @@ fn pyclass_richcmp(
op: #pyo3_path::pyclass::CompareOp
) -> #pyo3_path::PyResult<#pyo3_path::PyObject> {
let self_val = self;
if let Ok(other) = #pyo3_path::types::PyAnyMethods::downcast::<Self>(other) {
if let ::std::result::Result::Ok(other) = #pyo3_path::types::PyAnyMethods::downcast::<Self>(other) {
let other = &*other.borrow();
match op {
#arms
Expand Down
25 changes: 25 additions & 0 deletions src/tests/hygiene/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,28 @@ pub struct Foo4 {
#[cfg(not(any()))]
field: u32,
}

#[crate::pyclass(eq, ord)]
#[pyo3(crate = "crate")]
#[derive(PartialEq, PartialOrd)]
pub struct PointEqOrd {
x: u32,
y: u32,
z: u32,
}

#[crate::pyclass(eq, ord)]
#[pyo3(crate = "crate")]
#[derive(PartialEq, PartialOrd)]
pub enum ComplexEnumEqOrd {
Variant1 { a: u32, b: u32 },
Variant2 { c: u32 },
}

#[crate::pyclass(eq, ord)]
#[pyo3(crate = "crate")]
#[derive(PartialEq, PartialOrd)]
pub enum TupleEnumEqOrd {
Variant1(u32, u32),
Variant2(u32),
}
Loading