Skip to content
Open
Show file tree
Hide file tree
Changes from all 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/6134.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The `#[pyclass]` macro will now eagerly evaluate `cfg` and `cfg_attr` on fields and enum variants.
4 changes: 2 additions & 2 deletions pyo3-macros-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

// Listed first so that macros in this module are available in the rest of the crate.
#[macro_use]
mod utils;
pub mod utils;

mod attributes;
mod combine_errors;
Expand All @@ -30,7 +30,7 @@ mod quotes;
pub use frompyobject::build_derive_from_pyobject;
pub use intopyobject::build_derive_into_pyobject;
pub use module::{pymodule_function_impl, pymodule_module_impl, PyModuleOptions};
pub use pyclass::{build_py_class, build_py_enum, PyClassArgs};
pub use pyclass::{build_py_class, build_py_enum, PyClassArgs, PyClassKind, PyClassPyO3Options};
pub use pyfunction::{build_py_function, PyFunctionOptions};
pub use pyimpl::{build_py_methods, PyClassMethodsType};
pub use utils::get_doc;
1 change: 1 addition & 0 deletions pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3211,6 +3211,7 @@ fn generate_cfg_check(variants: &[PyClassEnumUnitVariant<'_>], cls: &syn::Ident)
}
}

// Should be unreachable; we eagerly expand cfgs now.
quote_spanned! {
cls.span() =>
#[cfg(all(#(#conditions),*))]
Expand Down
11 changes: 7 additions & 4 deletions pyo3-macros-backend/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,7 @@ pub struct Ctx {

impl Ctx {
pub(crate) fn new(attr: &Option<CrateAttribute>, signature: Option<&syn::Signature>) -> Self {
let pyo3_path = match attr {
Some(attr) => PyO3CratePath::Given(attr.value.0.clone()),
None => PyO3CratePath::Default,
};
let pyo3_path = PyO3CratePath::from_crate_path(attr);

let output_span = if let Some(syn::Signature {
output: syn::ReturnType::Type(_, output_type),
Expand All @@ -230,6 +227,12 @@ pub enum PyO3CratePath {
}

impl PyO3CratePath {
pub fn from_crate_path(path: &Option<CrateAttribute>) -> Self {
match path {
Some(attr) => PyO3CratePath::Given(attr.value.0.clone()),
None => PyO3CratePath::Default,
}
}
pub fn to_tokens_spanned(&self, span: Span) -> TokenStream {
match self {
Self::Given(path) => quote::quote_spanned! { span => #path },
Expand Down
2 changes: 1 addition & 1 deletion pyo3-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ experimental-inspect = ["pyo3-macros-backend/experimental-inspect"]
[dependencies]
proc-macro2 = { version = "1.0.60", default-features = false }
quote = "1"
syn = { version = "2", features = ["full", "extra-traits"] }
syn = { version = "2", features = ["full", "extra-traits", "visit", "visit-mut"] }
pyo3-macros-backend = { path = "../pyo3-macros-backend", version = "=0.29.0" }

[lints]
Expand Down
Loading
Loading