Skip to content

Commit 21300f6

Browse files
authored
Allow with() in pyexception (#6436)
1 parent b3c2aa6 commit 21300f6

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

crates/derive-impl/src/pyclass.rs

Lines changed: 32 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. If Constructor trait is used, don't generate slot_new
583+
let mut has_slot_new = false;
584+
585+
let mut extra_attrs = Vec::new();
586+
for nested in &attr {
587+
if let NestedMeta::Meta(Meta::List(MetaList { path, nested, .. })) = nested {
588+
if path.is_ident("with") {
589+
// Check if Constructor is in the list
590+
for meta in nested {
591+
if let NestedMeta::Meta(Meta::Path(p)) = meta
592+
&& p.is_ident("Constructor")
593+
{
594+
has_slot_new = true;
595+
}
596+
}
597+
}
598+
extra_attrs.push(NestedMeta::Meta(Meta::List(MetaList {
599+
path: path.clone(),
600+
paren_token: Default::default(),
601+
nested: nested.clone(),
602+
})));
603+
}
587604
}
588605

589-
let mut has_slot_new = false;
590606
let mut has_slot_init = false;
591607
let syn::ItemImpl {
592608
generics,
@@ -611,6 +627,8 @@ pub(crate) fn impl_pyexception_impl(attr: PunctuatedNestedMeta, item: Item) -> R
611627
}
612628
}
613629

630+
// TODO: slot_new, slot_init must be Constructor or Initializer later
631+
614632
let slot_new = if has_slot_new {
615633
quote!()
616634
} else {
@@ -646,8 +664,15 @@ pub(crate) fn impl_pyexception_impl(attr: PunctuatedNestedMeta, item: Item) -> R
646664
}
647665
}
648666
};
667+
668+
let extra_attrs_tokens = if extra_attrs.is_empty() {
669+
quote!()
670+
} else {
671+
quote!(, #(#extra_attrs),*)
672+
};
673+
649674
Ok(quote! {
650-
#[pyclass(flags(BASETYPE, HAS_DICT))]
675+
#[pyclass(flags(BASETYPE, HAS_DICT) #extra_attrs_tokens)]
651676
impl #generics #self_ty {
652677
#(#items)*
653678

0 commit comments

Comments
 (0)