Allow module= attribute in complex enum variants (#4228)

* Allow module= attribute in complex enum variants

* stderr test update

* towncrier

* inherit `module`, rather than specifying everywhere.

* clippy fix
This commit is contained in:
liammcinroy 2024-06-03 01:57:04 -06:00 committed by GitHub
parent 88b6f23e3b
commit b4b780b475
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 33 additions and 8 deletions

View File

@ -0,0 +1 @@
Changed the `module` option for complex enum variants to inherit from the value set on the complex enum `module`.

View File

@ -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>,
@ -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);

View File

@ -13,6 +13,7 @@ use crate::{
method::{FnArg, RegularArg},
};
#[derive(Clone)]
pub struct Signature {
paren_token: syn::token::Paren,
pub items: Punctuated<SignatureItem, Token![,]>,
@ -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),

View File

@ -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 {