Skip to content

Commit d9284fe

Browse files
funsafemathTpt
andauthored
Fix ambiguous associated item error (#5784)
Co-authored-by: Thomas Tanon <thomas@pellissier-tanon.fr>
1 parent 12d57fe commit d9284fe

4 files changed

Lines changed: 26 additions & 2 deletions

File tree

newsfragments/5784.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix `ambiguous_associated_items` compilation error when deriving `FromPyObject` or using `#[pyclass(from_py_object)]` macro on enums with `Error` variant.

pyo3-macros-backend/src/frompyobject.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ pub fn build_derive_from_pyobject(tokens: &DeriveInput) -> Result<TokenStream> {
588588
#[automatically_derived]
589589
impl #impl_generics #pyo3_path::FromPyObject<'_, #lt_param> for #ident #ty_generics #where_clause {
590590
type Error = #pyo3_path::PyErr;
591-
fn extract(obj: #pyo3_path::Borrowed<'_, #lt_param, #pyo3_path::PyAny>) -> ::std::result::Result<Self, Self::Error> {
591+
fn extract(obj: #pyo3_path::Borrowed<'_, #lt_param, #pyo3_path::PyAny>) -> ::std::result::Result<Self, #pyo3_path::PyErr> {
592592
let obj: &#pyo3_path::Bound<'_, _> = &*obj;
593593
#derives
594594
}

pyo3-macros-backend/src/pyclass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2833,7 +2833,7 @@ impl<'a> PyClassImplsBuilder<'a> {
28332833

28342834
#input_type
28352835

2836-
fn extract(obj: #pyo3_path::Borrowed<'a, 'py, #pyo3_path::PyAny>) -> ::std::result::Result<Self, Self::Error> {
2836+
fn extract(obj: #pyo3_path::Borrowed<'a, 'py, #pyo3_path::PyAny>) -> ::std::result::Result<Self, <Self as #pyo3_path::FromPyObject<'a, 'py>>::Error> {
28372837
::std::result::Result::Ok(::std::clone::Clone::clone(&*obj.extract::<#pyo3_path::PyClassGuard<'_, #cls>>()?))
28382838
}
28392839
}

tests/ui/ambiguous_associated_items.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,27 @@ enum DeriveItems {
2222
Target(Py<PyAny>),
2323
}
2424

25+
#[pyclass(from_py_object)]
26+
#[derive(Clone)]
27+
pub enum SimpleItemsFromPyObject {
28+
Error,
29+
Output,
30+
Target,
31+
}
32+
33+
#[pyclass(from_py_object)]
34+
#[derive(Clone)]
35+
pub enum ComplexItemsFromPyObject {
36+
Error(i32),
37+
Output(i32),
38+
Target(i32),
39+
}
40+
41+
#[derive(FromPyObject, Clone)]
42+
enum DeriveItemsFromPyObject {
43+
Error(i32),
44+
Output(i32),
45+
Target(i32),
46+
}
47+
2548
fn main() {}

0 commit comments

Comments
 (0)