Skip to content
Merged
Changes from 1 commit
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
Next Next commit
disallow __new__, __init__
  • Loading branch information
youknowone committed Dec 21, 2025
commit 364756e17497b8f79bd1a676f2d08c1c5905018e
23 changes: 22 additions & 1 deletion crates/derive-impl/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ impl FromStr for AttrName {

#[derive(Default)]
struct ImplContext {
is_trait: bool,
attribute_items: ItemNursery,
method_items: MethodNursery,
getset_items: GetSetNursery,
Expand Down Expand Up @@ -232,7 +233,10 @@ pub(crate) fn impl_pyclass_impl(attr: PunctuatedNestedMeta, item: Item) -> Resul
}
}
Item::Trait(mut trai) => {
let mut context = ImplContext::default();
let mut context = ImplContext {
is_trait: true,
..Default::default()
};
let mut has_extend_slots = false;
for item in &trai.items {
let has = match item {
Expand Down Expand Up @@ -892,6 +896,23 @@ where
let item_meta = MethodItemMeta::from_attr(ident.clone(), &item_attr)?;

let py_name = item_meta.method_name()?;

// Disallow __new__ and __init__ as pymethod in impl blocks (not in traits)
if !args.context.is_trait {
if py_name == "__new__" {
return Err(syn::Error::new(
ident.span(),
"#[pymethod] cannot define '__new__'. Use #[pyclass(with(Constructor))] instead.",
));
}
if py_name == "__init__" {
return Err(syn::Error::new(
ident.span(),
"#[pymethod] cannot define '__init__'. Use #[pyclass(with(Initializer))] instead.",
));
}
}

let raw = item_meta.raw()?;
let sig_doc = text_signature(func.sig(), &py_name);

Expand Down