Skip to content

Commit 519f592

Browse files
committed
Allow with() in pyexception
1 parent db71554 commit 519f592

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

crates/derive-impl/src/pyclass.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -579,14 +579,30 @@ pub(crate) fn impl_pyexception_impl(attr: PunctuatedNestedMeta, item: Item) -> R
579579
return Ok(item.into_token_stream());
580580
};
581581

582-
if !attr.is_empty() {
583-
return Err(syn::Error::new_spanned(
584-
&attr[0],
585-
"#[pyexception] impl doesn't allow attrs. Use #[pyclass] instead.",
586-
));
582+
// Check if with(Constructor) is specified
583+
let mut has_constructor_trait = false;
584+
let mut extra_attrs = Vec::new();
585+
for nested in &attr {
586+
if let NestedMeta::Meta(Meta::List(MetaList { path, nested, .. })) = nested {
587+
if path.is_ident("with") {
588+
// Check if Constructor is in the list
589+
for meta in nested {
590+
if let NestedMeta::Meta(Meta::Path(p)) = meta
591+
&& p.is_ident("Constructor")
592+
{
593+
has_constructor_trait = true;
594+
}
595+
}
596+
}
597+
extra_attrs.push(NestedMeta::Meta(Meta::List(MetaList {
598+
path: path.clone(),
599+
paren_token: Default::default(),
600+
nested: nested.clone(),
601+
})));
602+
}
587603
}
588604

589-
let mut has_slot_new = false;
605+
let mut has_slot_new = has_constructor_trait; // If Constructor trait is used, don't generate slot_new
590606
let mut has_slot_init = false;
591607
let syn::ItemImpl {
592608
generics,
@@ -646,8 +662,15 @@ pub(crate) fn impl_pyexception_impl(attr: PunctuatedNestedMeta, item: Item) -> R
646662
}
647663
}
648664
};
665+
666+
let extra_attrs_tokens = if extra_attrs.is_empty() {
667+
quote!()
668+
} else {
669+
quote!(, #(#extra_attrs),*)
670+
};
671+
649672
Ok(quote! {
650-
#[pyclass(flags(BASETYPE, HAS_DICT))]
673+
#[pyclass(flags(BASETYPE, HAS_DICT) #extra_attrs_tokens)]
651674
impl #generics #self_ty {
652675
#(#items)*
653676

0 commit comments

Comments
 (0)