@@ -147,18 +147,17 @@ def test_optimization_levels__debug__(self):
147
147
self .assertEqual (res .body [0 ].value .id , expected )
148
148
149
149
def test_optimization_levels_const_folding (self ):
150
- folded = ('Expr' , (1 , 0 , 1 , 5 ), ('Constant' , (1 , 0 , 1 , 5 ), 3 , None ))
151
- not_folded = ('Expr' , (1 , 0 , 1 , 5 ),
152
- ('BinOp' , (1 , 0 , 1 , 5 ),
153
- ('Constant' , (1 , 0 , 1 , 1 ), 1 , None ),
154
- ('Add' ,),
155
- ('Constant' , (1 , 4 , 1 , 5 ), 2 , None )))
150
+ folded = ('Expr' , (1 , 0 , 1 , 6 ), ('Constant' , (1 , 0 , 1 , 6 ), (1 , 2 ), None ))
151
+ not_folded = ('Expr' , (1 , 0 , 1 , 6 ),
152
+ ('Tuple' , (1 , 0 , 1 , 6 ),
153
+ [('Constant' , (1 , 1 , 1 , 2 ), 1 , None ),
154
+ ('Constant' , (1 , 4 , 1 , 5 ), 2 , None )], ('Load' ,)))
156
155
157
156
cases = [(- 1 , not_folded ), (0 , not_folded ), (1 , folded ), (2 , folded )]
158
157
for (optval , expected ) in cases :
159
158
with self .subTest (optval = optval ):
160
- tree1 = ast .parse ("1 + 2 " , optimize = optval )
161
- tree2 = ast .parse (ast .parse ("1 + 2 " ), optimize = optval )
159
+ tree1 = ast .parse ("(1, 2) " , optimize = optval )
160
+ tree2 = ast .parse (ast .parse ("(1, 2) " ), optimize = optval )
162
161
for tree in [tree1 , tree2 ]:
163
162
res = to_tuple (tree .body [0 ])
164
163
self .assertEqual (res , expected )
@@ -3134,32 +3133,6 @@ def assert_ast(self, code, non_optimized_target, optimized_target):
3134
3133
f"{ ast .dump (optimized_tree )} " ,
3135
3134
)
3136
3135
3137
- def create_binop (self , operand , left = ast .Constant (1 ), right = ast .Constant (1 )):
3138
- return ast .BinOp (left = left , op = self .binop [operand ], right = right )
3139
-
3140
- def test_folding_binop (self ):
3141
- code = "1 %s 1"
3142
- operators = self .binop .keys ()
3143
-
3144
- for op in operators :
3145
- result_code = code % op
3146
- non_optimized_target = self .wrap_expr (self .create_binop (op ))
3147
- optimized_target = self .wrap_expr (ast .Constant (value = eval (result_code )))
3148
-
3149
- with self .subTest (
3150
- result_code = result_code ,
3151
- non_optimized_target = non_optimized_target ,
3152
- optimized_target = optimized_target
3153
- ):
3154
- self .assert_ast (result_code , non_optimized_target , optimized_target )
3155
-
3156
- # Multiplication of constant tuples must be folded
3157
- code = "(1,) * 3"
3158
- non_optimized_target = self .wrap_expr (self .create_binop ("*" , ast .Tuple (elts = [ast .Constant (value = 1 )]), ast .Constant (value = 3 )))
3159
- optimized_target = self .wrap_expr (ast .Constant (eval (code )))
3160
-
3161
- self .assert_ast (code , non_optimized_target , optimized_target )
3162
-
3163
3136
def test_folding_unaryop (self ):
3164
3137
code = "%s1"
3165
3138
operators = self .unaryop .keys ()
@@ -3240,9 +3213,9 @@ def test_folding_tuple(self):
3240
3213
self .assert_ast (code , non_optimized_target , optimized_target )
3241
3214
3242
3215
def test_folding_type_param_in_function_def (self ):
3243
- code = "def foo[%s = 1 + 1 ](): pass"
3216
+ code = "def foo[%s = (1, 2) ](): pass"
3244
3217
3245
- unoptimized_binop = self . create_binop ( "+" )
3218
+ unoptimized_tuple = ast . Tuple ( elts = [ ast . Constant ( 1 ), ast . Constant ( 2 )] )
3246
3219
unoptimized_type_params = [
3247
3220
("T" , "T" , ast .TypeVar ),
3248
3221
("**P" , "P" , ast .ParamSpec ),
@@ -3256,23 +3229,23 @@ def test_folding_type_param_in_function_def(self):
3256
3229
name = 'foo' ,
3257
3230
args = ast .arguments (),
3258
3231
body = [ast .Pass ()],
3259
- type_params = [type_param (name = name , default_value = ast .Constant (2 ))]
3232
+ type_params = [type_param (name = name , default_value = ast .Constant (( 1 , 2 ) ))]
3260
3233
)
3261
3234
)
3262
3235
non_optimized_target = self .wrap_statement (
3263
3236
ast .FunctionDef (
3264
3237
name = 'foo' ,
3265
3238
args = ast .arguments (),
3266
3239
body = [ast .Pass ()],
3267
- type_params = [type_param (name = name , default_value = unoptimized_binop )]
3240
+ type_params = [type_param (name = name , default_value = unoptimized_tuple )]
3268
3241
)
3269
3242
)
3270
3243
self .assert_ast (result_code , non_optimized_target , optimized_target )
3271
3244
3272
3245
def test_folding_type_param_in_class_def (self ):
3273
- code = "class foo[%s = 1 + 1 ]: pass"
3246
+ code = "class foo[%s = (1, 2) ]: pass"
3274
3247
3275
- unoptimized_binop = self . create_binop ( "+" )
3248
+ unoptimized_tuple = ast . Tuple ( elts = [ ast . Constant ( 1 ), ast . Constant ( 2 )] )
3276
3249
unoptimized_type_params = [
3277
3250
("T" , "T" , ast .TypeVar ),
3278
3251
("**P" , "P" , ast .ParamSpec ),
@@ -3285,22 +3258,22 @@ def test_folding_type_param_in_class_def(self):
3285
3258
ast .ClassDef (
3286
3259
name = 'foo' ,
3287
3260
body = [ast .Pass ()],
3288
- type_params = [type_param (name = name , default_value = ast .Constant (2 ))]
3261
+ type_params = [type_param (name = name , default_value = ast .Constant (( 1 , 2 ) ))]
3289
3262
)
3290
3263
)
3291
3264
non_optimized_target = self .wrap_statement (
3292
3265
ast .ClassDef (
3293
3266
name = 'foo' ,
3294
3267
body = [ast .Pass ()],
3295
- type_params = [type_param (name = name , default_value = unoptimized_binop )]
3268
+ type_params = [type_param (name = name , default_value = unoptimized_tuple )]
3296
3269
)
3297
3270
)
3298
3271
self .assert_ast (result_code , non_optimized_target , optimized_target )
3299
3272
3300
3273
def test_folding_type_param_in_type_alias (self ):
3301
- code = "type foo[%s = 1 + 1 ] = 1"
3274
+ code = "type foo[%s = (1, 2) ] = 1"
3302
3275
3303
- unoptimized_binop = self . create_binop ( "+" )
3276
+ unoptimized_tuple = ast . Tuple ( elts = [ ast . Constant ( 1 ), ast . Constant ( 2 )] )
3304
3277
unoptimized_type_params = [
3305
3278
("T" , "T" , ast .TypeVar ),
3306
3279
("**P" , "P" , ast .ParamSpec ),
@@ -3312,14 +3285,14 @@ def test_folding_type_param_in_type_alias(self):
3312
3285
optimized_target = self .wrap_statement (
3313
3286
ast .TypeAlias (
3314
3287
name = ast .Name (id = 'foo' , ctx = ast .Store ()),
3315
- type_params = [type_param (name = name , default_value = ast .Constant (2 ))],
3288
+ type_params = [type_param (name = name , default_value = ast .Constant (( 1 , 2 ) ))],
3316
3289
value = ast .Constant (value = 1 ),
3317
3290
)
3318
3291
)
3319
3292
non_optimized_target = self .wrap_statement (
3320
3293
ast .TypeAlias (
3321
3294
name = ast .Name (id = 'foo' , ctx = ast .Store ()),
3322
- type_params = [type_param (name = name , default_value = unoptimized_binop )],
3295
+ type_params = [type_param (name = name , default_value = unoptimized_tuple )],
3323
3296
value = ast .Constant (value = 1 ),
3324
3297
)
3325
3298
)
0 commit comments