@@ -63,6 +63,7 @@ impl FromStr for AttrName {
6363
6464#[ derive( Default ) ]
6565struct ImplContext {
66+ is_trait : bool ,
6667 attribute_items : ItemNursery ,
6768 method_items : MethodNursery ,
6869 getset_items : GetSetNursery ,
@@ -233,6 +234,7 @@ pub(crate) fn impl_pyclass_impl(attr: PunctuatedNestedMeta, item: Item) -> Resul
233234 }
234235 Item :: Trait ( mut trai) => {
235236 let mut context = ImplContext :: default ( ) ;
237+ context. is_trait = true ;
236238 let mut has_extend_slots = false ;
237239 for item in & trai. items {
238240 let has = match item {
@@ -892,6 +894,23 @@ where
892894 let item_meta = MethodItemMeta :: from_attr ( ident. clone ( ) , & item_attr) ?;
893895
894896 let py_name = item_meta. method_name ( ) ?;
897+
898+ // Disallow __new__ and __init__ as pymethod in impl blocks (not in traits)
899+ if !args. context . is_trait {
900+ if py_name == "__new__" {
901+ return Err ( syn:: Error :: new (
902+ ident. span ( ) ,
903+ "#[pymethod] cannot define '__new__'. Use the Constructor trait instead." ,
904+ ) ) ;
905+ }
906+ if py_name == "__init__" {
907+ return Err ( syn:: Error :: new (
908+ ident. span ( ) ,
909+ "#[pymethod] cannot define '__init__'. Use the Initializer trait instead." ,
910+ ) ) ;
911+ }
912+ }
913+
895914 let raw = item_meta. raw ( ) ?;
896915 let sig_doc = text_signature ( func. sig ( ) , & py_name) ;
897916
0 commit comments