@@ -133,6 +133,7 @@ fn generate_class_def(
133133 ident : & Ident ,
134134 name : & str ,
135135 module_name : Option < & str > ,
136+ base : Option < String > ,
136137 attrs : & [ Attribute ] ,
137138) -> std:: result:: Result < TokenStream , Diagnostic > {
138139 let doc = if let Some ( doc) = attrs. doc ( ) {
@@ -159,11 +160,27 @@ fn generate_class_def(
159160 false
160161 }
161162 } ) ;
163+ if base. is_some ( ) && is_pystruct {
164+ return Err ( syn:: Error :: new_spanned (
165+ ident,
166+ "PyStructSequence cannot have `base` class attr" ,
167+ )
168+ . into ( ) ) ;
169+ }
170+ let base = base. map ( |name| Ident :: new ( & name, ident. span ( ) ) ) ;
162171
163172 let base_class = if is_pystruct {
164173 quote ! {
165- fn base_class( ctx: & :: rustpython_vm:: pyobject:: PyContext ) -> :: rustpython_vm:: builtins:: PyTypeRef {
166- ctx. types. tuple_type. clone( )
174+ fn static_baseclass( ) -> & ' static :: rustpython_vm:: builtins:: PyTypeRef {
175+ use rustpython_vm:: pyobject:: StaticType ;
176+ rustpython_vm:: builtins:: PyTuple :: static_type( )
177+ }
178+ }
179+ } else if let Some ( base) = base {
180+ quote ! {
181+ fn static_baseclass( ) -> & ' static :: rustpython_vm:: builtins:: PyTypeRef {
182+ use rustpython_vm:: pyobject:: StaticType ;
183+ #base:: static_type( )
167184 }
168185 }
169186 } else {
@@ -176,6 +193,17 @@ fn generate_class_def(
176193 const MODULE_NAME : Option <& ' static str > = #module_name;
177194 const TP_NAME : & ' static str = #module_class_name;
178195 const DOC : Option <& ' static str > = #doc;
196+ }
197+
198+ impl :: rustpython_vm:: pyobject:: StaticType for #ident {
199+ fn static_cell( ) -> & ' static :: rustpython_common:: static_cell:: StaticCell <:: rustpython_vm:: builtins:: PyTypeRef > {
200+ use :: rustpython_common:: static_cells;
201+ static_cells! {
202+ static CELL : :: rustpython_vm:: builtins:: PyTypeRef ;
203+ }
204+ & CELL
205+ }
206+
179207 #base_class
180208 }
181209 } ;
@@ -191,7 +219,8 @@ pub(crate) fn impl_pyclass(
191219 let class_meta = ClassItemMeta :: from_nested ( ident. clone ( ) , fake_ident, attr. into_iter ( ) ) ?;
192220 let class_name = class_meta. class_name ( ) ?;
193221 let module_name = class_meta. module ( ) ?;
194- let class_def = generate_class_def ( & ident, & class_name, module_name. as_deref ( ) , & attrs) ?;
222+ let base = class_meta. base ( ) ?;
223+ let class_def = generate_class_def ( & ident, & class_name, module_name. as_deref ( ) , base, & attrs) ?;
195224
196225 let ret = quote ! {
197226 #item
0 commit comments