@@ -1215,38 +1215,92 @@ 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 ) ;
1235+ }
1236+
1237+ // Handle default value if present (PEP 695)
1238+ if let Some ( default_expr) = default {
12331239 emit ! ( self , Instruction :: Duplicate ) ;
1234- self . store_name ( name. as_ref ( ) ) ?;
1240+
1241+ // Compile the default expression
1242+ self . compile_expression ( default_expr) ?;
1243+
1244+ emit ! (
1245+ self ,
1246+ Instruction :: CallIntrinsic2 {
1247+ func: bytecode:: IntrinsicFunction2 :: SetTypeparamDefault
1248+ }
1249+ ) ;
12351250 }
1251+
1252+ emit ! ( self , Instruction :: Duplicate ) ;
1253+ self . store_name ( name. as_ref ( ) ) ?;
12361254 }
1237- TypeParam :: ParamSpec ( TypeParamParamSpec { name, .. } ) => {
1255+ TypeParam :: ParamSpec ( TypeParamParamSpec { name, default , .. } ) => {
12381256 self . emit_load_const ( ConstantData :: Str {
12391257 value : name. as_str ( ) . into ( ) ,
12401258 } ) ;
12411259 emit ! ( self , Instruction :: ParamSpec ) ;
1260+
1261+ // Handle default value if present (PEP 695)
1262+ if let Some ( default_expr) = default {
1263+ emit ! ( self , Instruction :: Duplicate ) ;
1264+
1265+ // Compile the default expression
1266+ self . compile_expression ( default_expr) ?;
1267+
1268+ emit ! (
1269+ self ,
1270+ Instruction :: CallIntrinsic2 {
1271+ func: bytecode:: IntrinsicFunction2 :: SetTypeparamDefault
1272+ }
1273+ ) ;
1274+ }
1275+
12421276 emit ! ( self , Instruction :: Duplicate ) ;
12431277 self . store_name ( name. as_ref ( ) ) ?;
12441278 }
1245- TypeParam :: TypeVarTuple ( TypeParamTypeVarTuple { name, .. } ) => {
1279+ TypeParam :: TypeVarTuple ( TypeParamTypeVarTuple { name, default , .. } ) => {
12461280 self . emit_load_const ( ConstantData :: Str {
12471281 value : name. as_str ( ) . into ( ) ,
12481282 } ) ;
12491283 emit ! ( self , Instruction :: TypeVarTuple ) ;
1284+
1285+ // Handle default value if present (PEP 695)
1286+ if let Some ( default_expr) = default {
1287+ emit ! ( self , Instruction :: Duplicate ) ;
1288+
1289+ // Compile the default expression
1290+ self . compile_expression ( default_expr) ?;
1291+
1292+ // Handle starred expression (*default)
1293+ // CPython handles the unpacking during runtime evaluation of the default value
1294+ // We don't need special intrinsic for this - just compile the expression normally
1295+
1296+ emit ! (
1297+ self ,
1298+ Instruction :: CallIntrinsic2 {
1299+ func: bytecode:: IntrinsicFunction2 :: SetTypeparamDefault
1300+ }
1301+ ) ;
1302+ }
1303+
12501304 emit ! ( self , Instruction :: Duplicate ) ;
12511305 self . store_name ( name. as_ref ( ) ) ?;
12521306 }
@@ -1759,13 +1813,41 @@ impl Compiler<'_> {
17591813
17601814 self . emit_load_const ( ConstantData :: Str { value : name. into ( ) } ) ;
17611815
1762- // Call the __build_class__ builtin
1763- let call = if let Some ( arguments) = arguments {
1764- self . compile_call_inner ( 2 , arguments) ?
1816+ // For PEP 695 classes: handle Generic base creation
1817+ if type_params. is_some ( ) {
1818+ if let Some ( arguments) = arguments {
1819+ // Has explicit bases - use them as is, don't add Generic
1820+ // CPython doesn't add Generic when explicit bases are present
1821+ let call = self . compile_call_inner ( 2 , arguments) ?;
1822+ self . compile_normal_call ( call) ;
1823+ } else {
1824+ // No explicit bases, add Generic[*type_params] as the only base
1825+ // Stack currently: [function, class_name]
1826+
1827+ // Load .type_params for creating Generic base
1828+ let dot_type_params = self . name ( ".type_params" ) ;
1829+ emit ! ( self , Instruction :: LoadNameAny ( dot_type_params) ) ;
1830+
1831+ // Call INTRINSIC_SUBSCRIPT_GENERIC to create Generic[*type_params]
1832+ emit ! (
1833+ self ,
1834+ Instruction :: CallIntrinsic1 {
1835+ func: bytecode:: IntrinsicFunction1 :: SubscriptGeneric
1836+ }
1837+ ) ;
1838+
1839+ // Call __build_class__ with 3 positional args: function, class_name, Generic[T]
1840+ emit ! ( self , Instruction :: CallFunctionPositional { nargs: 3 } ) ;
1841+ }
17651842 } else {
1766- CallType :: Positional { nargs : 2 }
1767- } ;
1768- self . compile_normal_call ( call) ;
1843+ // No type params, normal compilation
1844+ let call = if let Some ( arguments) = arguments {
1845+ self . compile_call_inner ( 2 , arguments) ?
1846+ } else {
1847+ CallType :: Positional { nargs : 2 }
1848+ } ;
1849+ self . compile_normal_call ( call) ;
1850+ }
17691851
17701852 // Pop the special type params symbol table
17711853 if type_params. is_some ( ) {
0 commit comments