Skip to content

Commit

Permalink
Allow module= attribute in complex enum variants (#4228)
Browse files Browse the repository at this point in the history
* Allow module= attribute in complex enum variants

* stderr test update

* towncrier

* inherit `module`, rather than specifying everywhere.

* clippy fix
  • Loading branch information
liammcinroy committed Jun 3, 2024
1 parent 88b6f23 commit b4b780b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
1 change: 1 addition & 0 deletions newsfragments/4228.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed the `module` option for complex enum variants to inherit from the value set on the complex enum `module`.
9 changes: 7 additions & 2 deletions pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ struct PyClassEnumVariantUnnamedField<'a> {
}

/// `#[pyo3()]` options for pyclass enum variants
#[derive(Default)]
#[derive(Clone, Default)]
struct EnumVariantPyO3Options {
name: Option<NameAttribute>,
constructor: Option<ConstructorAttribute>,
Expand Down Expand Up @@ -949,7 +949,12 @@ fn impl_complex_enum(
let variant_args = PyClassArgs {
class_kind: PyClassKind::Struct,
// TODO(mkovaxx): propagate variant.options
options: parse_quote!(extends = #cls, frozen),
options: {
let mut rigged_options: PyClassPyO3Options = parse_quote!(extends = #cls, frozen);
// If a specific module was given to the base class, use it for all variants.
rigged_options.module.clone_from(&args.options.module);
rigged_options
},
};

let variant_cls_pytypeinfo = impl_pytypeinfo(&variant_cls, &variant_args, None, ctx);
Expand Down
13 changes: 7 additions & 6 deletions pyo3-macros-backend/src/pyfunction/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
method::{FnArg, RegularArg},
};

#[derive(Clone)]
pub struct Signature {
paren_token: syn::token::Paren,
pub items: Punctuated<SignatureItem, Token![,]>,
Expand All @@ -36,35 +37,35 @@ impl ToTokens for Signature {
}
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SignatureItemArgument {
pub ident: syn::Ident,
pub eq_and_default: Option<(Token![=], syn::Expr)>,
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SignatureItemPosargsSep {
pub slash: Token![/],
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SignatureItemVarargsSep {
pub asterisk: Token![*],
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SignatureItemVarargs {
pub sep: SignatureItemVarargsSep,
pub ident: syn::Ident,
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SignatureItemKwargs {
pub asterisks: (Token![*], Token![*]),
pub ident: syn::Ident,
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SignatureItem {
Argument(Box<SignatureItemArgument>),
PosargsSep(SignatureItemPosargsSep),
Expand Down
18 changes: 18 additions & 0 deletions tests/test_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,24 @@ fn test_renaming_all_enum_variants() {
});
}

#[pyclass(module = "custom_module")]
#[derive(Debug, Clone)]
enum CustomModuleComplexEnum {
Variant(),
}

#[test]
fn test_custom_module() {
Python::with_gil(|py| {
let enum_obj = py.get_type_bound::<CustomModuleComplexEnum>();
py_assert!(
py,
enum_obj,
"enum_obj.Variant.__module__ == 'custom_module'"
);
});
}

#[pyclass(frozen, eq, eq_int, hash)]
#[derive(PartialEq, Hash)]
enum SimpleEnumWithHash {
Expand Down

0 comments on commit b4b780b

Please sign in to comment.