@@ -1215,38 +1215,86 @@ 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+ // CPython handles the unpacking during runtime evaluation of the default value
1288+ // We don't need special intrinsic for this - just compile the expression normally
1289+
1290+ emit ! (
1291+ self ,
1292+ Instruction :: CallIntrinsic2 {
1293+ func: bytecode:: IntrinsicFunction2 :: SetTypeparamDefault
1294+ }
1295+ ) ;
1296+ }
1297+
12501298 emit ! ( self , Instruction :: Duplicate ) ;
12511299 self . store_name ( name. as_ref ( ) ) ?;
12521300 }
@@ -1759,13 +1807,41 @@ impl Compiler<'_> {
17591807
17601808 self . emit_load_const ( ConstantData :: Str { value : name. into ( ) } ) ;
17611809
1762- // Call the __build_class__ builtin
1763- let call = if let Some ( arguments) = arguments {
1764- self . compile_call_inner ( 2 , arguments) ?
1810+ // For PEP 695 classes: handle Generic base creation
1811+ if type_params. is_some ( ) {
1812+ if let Some ( arguments) = arguments {
1813+ // Has explicit bases - use them as is, don't add Generic
1814+ // CPython doesn't add Generic when explicit bases are present
1815+ let call = self . compile_call_inner ( 2 , arguments) ?;
1816+ self . compile_normal_call ( call) ;
1817+ } else {
1818+ // No explicit bases, add Generic[*type_params] as the only base
1819+ // Stack currently: [function, class_name]
1820+
1821+ // Load .type_params for creating Generic base
1822+ let dot_type_params = self . name ( ".type_params" ) ;
1823+ emit ! ( self , Instruction :: LoadNameAny ( dot_type_params) ) ;
1824+
1825+ // Call INTRINSIC_SUBSCRIPT_GENERIC to create Generic[*type_params]
1826+ emit ! (
1827+ self ,
1828+ Instruction :: CallIntrinsic1 {
1829+ func: bytecode:: IntrinsicFunction1 :: SubscriptGeneric
1830+ }
1831+ ) ;
1832+
1833+ // Call __build_class__ with 3 positional args: function, class_name, Generic[T]
1834+ emit ! ( self , Instruction :: CallFunctionPositional { nargs: 3 } ) ;
1835+ }
17651836 } else {
1766- CallType :: Positional { nargs : 2 }
1767- } ;
1768- self . compile_normal_call ( call) ;
1837+ // No type params, normal compilation
1838+ let call = if let Some ( arguments) = arguments {
1839+ self . compile_call_inner ( 2 , arguments) ?
1840+ } else {
1841+ CallType :: Positional { nargs : 2 }
1842+ } ;
1843+ self . compile_normal_call ( call) ;
1844+ }
17691845
17701846 // Pop the special type params symbol table
17711847 if type_params. is_some ( ) {
0 commit comments