@@ -1212,6 +1212,7 @@ impl Compiler<'_> {
12121212 /// Store each type parameter so it is accessible to the current scope, and leave a tuple of
12131213 /// all the type parameters on the stack.
12141214 fn compile_type_params ( & mut self , type_params : & TypeParams ) -> CompileResult < ( ) > {
1215+ // First, compile each type parameter and store it
12151216 for type_param in & type_params. type_params {
12161217 match type_param {
12171218 TypeParam :: TypeVar ( TypeParamTypeVar { name, bound, .. } ) => {
@@ -1664,8 +1665,12 @@ impl Compiler<'_> {
16641665 let qualified_name = self . qualified_path . join ( "." ) ;
16651666
16661667 // If there are type params, we need to push a special symbol table just for them
1667- if type_params . is_some ( ) {
1668+ if let Some ( type_params ) = type_params {
16681669 self . push_symbol_table ( ) ;
1670+ // Compile type parameters and store as .type_params
1671+ self . compile_type_params ( type_params) ?;
1672+ let dot_type_params = self . name ( ".type_params" ) ;
1673+ emit ! ( self , Instruction :: StoreLocal ( dot_type_params) ) ;
16691674 }
16701675
16711676 self . push_output ( bytecode:: CodeFlags :: empty ( ) , 0 , 0 , 0 , name. to_owned ( ) ) ;
@@ -1688,6 +1693,18 @@ impl Compiler<'_> {
16881693 if Self :: find_ann ( body) {
16891694 emit ! ( self , Instruction :: SetupAnnotation ) ;
16901695 }
1696+
1697+ // Set __type_params__ from .type_params if we have type parameters (PEP 695)
1698+ if type_params. is_some ( ) {
1699+ // Load .type_params from enclosing scope
1700+ let dot_type_params = self . name ( ".type_params" ) ;
1701+ emit ! ( self , Instruction :: LoadNameAny ( dot_type_params) ) ;
1702+
1703+ // Store as __type_params__
1704+ let dunder_type_params = self . name ( "__type_params__" ) ;
1705+ emit ! ( self , Instruction :: StoreLocal ( dunder_type_params) ) ;
1706+ }
1707+
16911708 self . compile_statements ( body) ?;
16921709
16931710 let classcell_idx = self
@@ -1721,8 +1738,10 @@ impl Compiler<'_> {
17211738 let mut func_flags = bytecode:: MakeFunctionFlags :: empty ( ) ;
17221739
17231740 // Prepare generic type parameters:
1724- if let Some ( type_params) = type_params {
1725- self . compile_type_params ( type_params) ?;
1741+ if type_params. is_some ( ) {
1742+ // Load .type_params from the type params scope
1743+ let dot_type_params = self . name ( ".type_params" ) ;
1744+ emit ! ( self , Instruction :: LoadNameAny ( dot_type_params) ) ;
17261745 func_flags |= bytecode:: MakeFunctionFlags :: TYPE_PARAMS ;
17271746 }
17281747
0 commit comments