@@ -3180,7 +3180,8 @@ def create_unaryop(operand):
31803180 self .assert_ast (result_code , non_optimized_target , optimized_target )
31813181
31823182 def test_folding_not (self ):
3183- code = "not (1 %s (1,))"
3183+ # use list as left-hand side to avoid folding constant expression to True/False
3184+ code = "not ([] %s (1,))"
31843185 operators = {
31853186 "in" : ast .In (),
31863187 "is" : ast .Is (),
@@ -3192,7 +3193,7 @@ def test_folding_not(self):
31923193
31933194 def create_notop (operand ):
31943195 return ast .UnaryOp (op = ast .Not (), operand = ast .Compare (
3195- left = ast .Constant ( value = 1 ),
3196+ left = ast .List ( ),
31963197 ops = [operators [operand ]],
31973198 comparators = [ast .Tuple (elts = [ast .Constant (value = 1 )])]
31983199 ))
@@ -3201,7 +3202,7 @@ def create_notop(operand):
32013202 result_code = code % op
32023203 non_optimized_target = self .wrap_expr (create_notop (op ))
32033204 optimized_target = self .wrap_expr (
3204- ast .Compare (left = ast .Constant ( 1 ), ops = [opt_operators [op ]], comparators = [ast .Constant (value = (1 ,))])
3205+ ast .Compare (left = ast .List ( ), ops = [opt_operators [op ]], comparators = [ast .Constant (value = (1 ,))])
32053206 )
32063207
32073208 with self .subTest (
@@ -3239,8 +3240,11 @@ def test_folding_tuple(self):
32393240
32403241 self .assert_ast (code , non_optimized_target , optimized_target )
32413242
3242- def test_folding_comparator (self ):
3243- code = "1 %s %s1%s"
3243+ def test_folding_comparator_list_set_subst (self ):
3244+ """Test substitution of list/set with tuple/frozenset in expressions like "1 in [1]" or "1 in {1}" """
3245+
3246+ # use list as left-hand side to avoid folding constant comparison expression to True/False
3247+ code = "[] %s %s1%s"
32443248 operators = [("in" , ast .In ()), ("not in" , ast .NotIn ())]
32453249 braces = [
32463250 ("[" , "]" , ast .List , (1 ,)),
@@ -3249,11 +3253,11 @@ def test_folding_comparator(self):
32493253 for left , right , non_optimized_comparator , optimized_comparator in braces :
32503254 for op , node in operators :
32513255 non_optimized_target = self .wrap_expr (ast .Compare (
3252- left = ast .Constant ( 1 ), ops = [node ],
3256+ left = ast .List ( ), ops = [node ],
32533257 comparators = [non_optimized_comparator (elts = [ast .Constant (1 )])]
32543258 ))
32553259 optimized_target = self .wrap_expr (ast .Compare (
3256- left = ast .Constant ( 1 ), ops = [node ],
3260+ left = ast .List ( ), ops = [node ],
32573261 comparators = [ast .Constant (value = optimized_comparator )]
32583262 ))
32593263 self .assert_ast (code % (op , left , right ), non_optimized_target , optimized_target )
0 commit comments