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

Derive macro for IntoPyObject #4495

Merged
merged 17 commits into from
Oct 26, 2024
Prev Previous commit
Next Next commit
add hygiene tests
Icxolu committed Oct 25, 2024
commit 730d6ce72cbc672ad04a39d3589ff13b0a82f556
4 changes: 2 additions & 2 deletions pyo3-macros-backend/src/intopyobject.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ use syn::parse::{Parse, ParseStream};
use syn::spanned::Spanned as _;
use syn::{parse_quote, Attribute, DataEnum, DeriveInput, Fields, Ident, Index, Result, Token};

/// Attributes for deriving FromPyObject scoped on containers.
/// Attributes for deriving `IntoPyObject` scoped on containers.
enum ContainerPyO3Attribute {
/// Treat the Container as a Wrapper, directly convert its field into the output object.
Transparent(attributes::kw::transparent),
@@ -374,7 +374,7 @@ impl<'a> Enum<'a> {
{#body}
.map(#pyo3_path::BoundObject::into_any)
.map(#pyo3_path::BoundObject::into_bound)
.map_err(::std::convert::Into::<PyErr>::into)
.map_err(::std::convert::Into::<#pyo3_path::PyErr>::into)
}
}
})
36 changes: 36 additions & 0 deletions src/tests/hygiene/misc.rs
Original file line number Diff line number Diff line change
@@ -56,3 +56,39 @@ macro_rules! macro_rules_hygiene {
}

macro_rules_hygiene!(MyClass1, MyClass2);

#[derive(crate::IntoPyObject)]
#[pyo3(crate = "crate")]
struct IntoPyObject1(i32); // transparent newtype case

#[derive(crate::IntoPyObject)]
#[pyo3(crate = "crate", transparent)]
struct IntoPyObject2<'a> {
inner: &'a str, // transparent newtype case
}

#[derive(crate::IntoPyObject)]
#[pyo3(crate = "crate")]
struct IntoPyObject3<'py>(i32, crate::Bound<'py, crate::PyAny>); // tuple case

#[derive(crate::IntoPyObject)]
#[pyo3(crate = "crate")]
struct IntoPyObject4<'a, 'py> {
callable: &'a crate::Bound<'py, crate::PyAny>, // struct case
num: usize,
}

#[derive(crate::IntoPyObject)]
#[pyo3(crate = "crate")]
enum IntoPyObject5<'a, 'py> {
TransparentTuple(i32),
#[pyo3(transparent)]
TransparentStruct {
f: crate::Py<crate::PyAny>,
},
Tuple(crate::Bound<'py, crate::types::PyString>, usize),
Struct {
f: i32,
g: &'a str,
},
} // enum case