@@ -1215,38 +1215,83 @@ impl Compiler<'_> {
12151215 // First, compile each type parameter and store it
12161216 for type_param in & type_params. type_params {
12171217 match type_param {
1218- TypeParam :: TypeVar ( TypeParamTypeVar { name, bound, .. } ) => {
1218+ TypeParam :: TypeVar ( TypeParamTypeVar {
1219+ name,
1220+ bound,
1221+ default,
1222+ ..
1223+ } ) => {
12191224 if let Some ( expr) = & bound {
12201225 self . compile_expression ( expr) ?;
12211226 self . emit_load_const ( ConstantData :: Str {
12221227 value : name. as_str ( ) . into ( ) ,
12231228 } ) ;
12241229 emit ! ( self , Instruction :: TypeVarWithBound ) ;
1225- emit ! ( self , Instruction :: Duplicate ) ;
1226- self . store_name ( name. as_ref ( ) ) ?;
12271230 } else {
1228- // self.store_name(type_name.as_str())?;
12291231 self . emit_load_const ( ConstantData :: Str {
12301232 value : name. as_str ( ) . into ( ) ,
12311233 } ) ;
12321234 emit ! ( self , Instruction :: TypeVar ) ;
1233- emit ! ( self , Instruction :: Duplicate ) ;
1234- self . store_name ( name. as_ref ( ) ) ?;
12351235 }
1236+
1237+ // Handle default value if present (PEP 695)
1238+ if let Some ( default_expr) = default {
1239+ // Compile the default expression
1240+ self . compile_expression ( default_expr) ?;
1241+
1242+ emit ! (
1243+ self ,
1244+ Instruction :: CallIntrinsic2 {
1245+ func: bytecode:: IntrinsicFunction2 :: SetTypeparamDefault
1246+ }
1247+ ) ;
1248+ }
1249+
1250+ emit ! ( self , Instruction :: Duplicate ) ;
1251+ self . store_name ( name. as_ref ( ) ) ?;
12361252 }
1237- TypeParam :: ParamSpec ( TypeParamParamSpec { name, .. } ) => {
1253+ TypeParam :: ParamSpec ( TypeParamParamSpec { name, default , .. } ) => {
12381254 self . emit_load_const ( ConstantData :: Str {
12391255 value : name. as_str ( ) . into ( ) ,
12401256 } ) ;
12411257 emit ! ( self , Instruction :: ParamSpec ) ;
1258+
1259+ // Handle default value if present (PEP 695)
1260+ if let Some ( default_expr) = default {
1261+ // Compile the default expression
1262+ self . compile_expression ( default_expr) ?;
1263+
1264+ emit ! (
1265+ self ,
1266+ Instruction :: CallIntrinsic2 {
1267+ func: bytecode:: IntrinsicFunction2 :: SetTypeparamDefault
1268+ }
1269+ ) ;
1270+ }
1271+
12421272 emit ! ( self , Instruction :: Duplicate ) ;
12431273 self . store_name ( name. as_ref ( ) ) ?;
12441274 }
1245- TypeParam :: TypeVarTuple ( TypeParamTypeVarTuple { name, .. } ) => {
1275+ TypeParam :: TypeVarTuple ( TypeParamTypeVarTuple { name, default , .. } ) => {
12461276 self . emit_load_const ( ConstantData :: Str {
12471277 value : name. as_str ( ) . into ( ) ,
12481278 } ) ;
12491279 emit ! ( self , Instruction :: TypeVarTuple ) ;
1280+
1281+ // Handle default value if present (PEP 695)
1282+ if let Some ( default_expr) = default {
1283+ // Compile the default expression
1284+ self . compile_expression ( default_expr) ?;
1285+
1286+ // Handle starred expression (*default)
1287+ emit ! (
1288+ self ,
1289+ Instruction :: CallIntrinsic2 {
1290+ func: bytecode:: IntrinsicFunction2 :: SetTypeparamDefault
1291+ }
1292+ ) ;
1293+ }
1294+
12501295 emit ! ( self , Instruction :: Duplicate ) ;
12511296 self . store_name ( name. as_ref ( ) ) ?;
12521297 }
@@ -1759,13 +1804,41 @@ impl Compiler<'_> {
17591804
17601805 self . emit_load_const ( ConstantData :: Str { value : name. into ( ) } ) ;
17611806
1762- // Call the __build_class__ builtin
1763- let call = if let Some ( arguments) = arguments {
1764- self . compile_call_inner ( 2 , arguments) ?
1807+ // For PEP 695 classes: handle Generic base creation
1808+ if type_params. is_some ( ) {
1809+ if let Some ( arguments) = arguments {
1810+ // Has explicit bases - use them as is, don't add Generic
1811+ // CPython doesn't add Generic when explicit bases are present
1812+ let call = self . compile_call_inner ( 2 , arguments) ?;
1813+ self . compile_normal_call ( call) ;
1814+ } else {
1815+ // No explicit bases, add Generic[*type_params] as the only base
1816+ // Stack currently: [function, class_name]
1817+
1818+ // Load .type_params for creating Generic base
1819+ let dot_type_params = self . name ( ".type_params" ) ;
1820+ emit ! ( self , Instruction :: LoadNameAny ( dot_type_params) ) ;
1821+
1822+ // Call INTRINSIC_SUBSCRIPT_GENERIC to create Generic[*type_params]
1823+ emit ! (
1824+ self ,
1825+ Instruction :: CallIntrinsic1 {
1826+ func: bytecode:: IntrinsicFunction1 :: SubscriptGeneric
1827+ }
1828+ ) ;
1829+
1830+ // Call __build_class__ with 3 positional args: function, class_name, Generic[T]
1831+ emit ! ( self , Instruction :: CallFunctionPositional { nargs: 3 } ) ;
1832+ }
17651833 } else {
1766- CallType :: Positional { nargs : 2 }
1767- } ;
1768- self . compile_normal_call ( call) ;
1834+ // No type params, normal compilation
1835+ let call = if let Some ( arguments) = arguments {
1836+ self . compile_call_inner ( 2 , arguments) ?
1837+ } else {
1838+ CallType :: Positional { nargs : 2 }
1839+ } ;
1840+ self . compile_normal_call ( call) ;
1841+ }
17691842
17701843 // Pop the special type params symbol table
17711844 if type_params. is_some ( ) {
0 commit comments