@@ -196,6 +196,25 @@ def visit_Attribute(self, node):
196
196
if isinstance (node .value , ast .Name ):
197
197
self .nodes .add (node .value )
198
198
199
+ class NotBooleanTestVisitor (ASTVisitor ):
200
+ """Visitor that checks if a test is not a boolean test."""
201
+
202
+ def __init__ (self ):
203
+ self .nodes = set ()
204
+
205
+ def visit_MatchLiteralPattern (self , node ):
206
+ # MatchLiteralPatterns _look_ like boolean tests, but are not.
207
+ # Thus, without this check, we would interpret
208
+ #
209
+ # match x:
210
+ # case False:
211
+ # pass
212
+ #
213
+ # (and similarly for True) as if it was a boolean test. This would cause the true edge
214
+ # (leading to pass) to be pruned later on.
215
+ if isinstance (node .literal , ast .Name ) and node .literal .id in ('True' , 'False' ):
216
+ self .nodes .add (node .literal )
217
+
199
218
class NonlocalVisitor (ASTVisitor ):
200
219
def __init__ (self ):
201
220
self .names = set ()
@@ -306,6 +325,8 @@ def effective_constants_definitions(bool_const_defns, graph, branching_edges):
306
325
def do_pruning (tree , graph ):
307
326
v = BoolConstVisitor ()
308
327
v .visit (tree )
328
+ not_boolean_test = NotBooleanTestVisitor ()
329
+ not_boolean_test .visit (tree )
309
330
nonlocals = NonlocalVisitor ()
310
331
nonlocals .visit (tree )
311
332
global_vars = GlobalVisitor ()
@@ -353,6 +374,8 @@ def do_pruning(tree, graph):
353
374
b = const_value (pred .node )
354
375
if b is None :
355
376
continue
377
+ if pred .node in not_boolean_test .nodes :
378
+ continue
356
379
if b .contradicts (val ):
357
380
to_be_removed .add ((pred , succ ))
358
381
if not to_be_removed :
0 commit comments