@@ -147,18 +147,17 @@ def test_optimization_levels__debug__(self):
147147 self .assertEqual (res .body [0 ].value .id , expected )
148148
149149 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' ,)))
156155
157156 cases = [(- 1 , not_folded ), (0 , not_folded ), (1 , folded ), (2 , folded )]
158157 for (optval , expected ) in cases :
159158 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 )
162161 for tree in [tree1 , tree2 ]:
163162 res = to_tuple (tree .body [0 ])
164163 self .assertEqual (res , expected )
@@ -3134,32 +3133,6 @@ def assert_ast(self, code, non_optimized_target, optimized_target):
31343133 f"{ ast .dump (optimized_tree )} " ,
31353134 )
31363135
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-
31633136 def test_folding_unaryop (self ):
31643137 code = "%s1"
31653138 operators = self .unaryop .keys ()
@@ -3240,9 +3213,9 @@ def test_folding_tuple(self):
32403213 self .assert_ast (code , non_optimized_target , optimized_target )
32413214
32423215 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"
32443217
3245- unoptimized_binop = self . create_binop ( "+" )
3218+ unoptimized_tuple = ast . Tuple ( elts = [ ast . Constant ( 1 ), ast . Constant ( 2 )] )
32463219 unoptimized_type_params = [
32473220 ("T" , "T" , ast .TypeVar ),
32483221 ("**P" , "P" , ast .ParamSpec ),
@@ -3256,23 +3229,23 @@ def test_folding_type_param_in_function_def(self):
32563229 name = 'foo' ,
32573230 args = ast .arguments (),
32583231 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 ) ))]
32603233 )
32613234 )
32623235 non_optimized_target = self .wrap_statement (
32633236 ast .FunctionDef (
32643237 name = 'foo' ,
32653238 args = ast .arguments (),
32663239 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 )]
32683241 )
32693242 )
32703243 self .assert_ast (result_code , non_optimized_target , optimized_target )
32713244
32723245 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"
32743247
3275- unoptimized_binop = self . create_binop ( "+" )
3248+ unoptimized_tuple = ast . Tuple ( elts = [ ast . Constant ( 1 ), ast . Constant ( 2 )] )
32763249 unoptimized_type_params = [
32773250 ("T" , "T" , ast .TypeVar ),
32783251 ("**P" , "P" , ast .ParamSpec ),
@@ -3285,22 +3258,22 @@ def test_folding_type_param_in_class_def(self):
32853258 ast .ClassDef (
32863259 name = 'foo' ,
32873260 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 ) ))]
32893262 )
32903263 )
32913264 non_optimized_target = self .wrap_statement (
32923265 ast .ClassDef (
32933266 name = 'foo' ,
32943267 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 )]
32963269 )
32973270 )
32983271 self .assert_ast (result_code , non_optimized_target , optimized_target )
32993272
33003273 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"
33023275
3303- unoptimized_binop = self . create_binop ( "+" )
3276+ unoptimized_tuple = ast . Tuple ( elts = [ ast . Constant ( 1 ), ast . Constant ( 2 )] )
33043277 unoptimized_type_params = [
33053278 ("T" , "T" , ast .TypeVar ),
33063279 ("**P" , "P" , ast .ParamSpec ),
@@ -3312,14 +3285,14 @@ def test_folding_type_param_in_type_alias(self):
33123285 optimized_target = self .wrap_statement (
33133286 ast .TypeAlias (
33143287 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 ) ))],
33163289 value = ast .Constant (value = 1 ),
33173290 )
33183291 )
33193292 non_optimized_target = self .wrap_statement (
33203293 ast .TypeAlias (
33213294 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 )],
33233296 value = ast .Constant (value = 1 ),
33243297 )
33253298 )
0 commit comments