3939 | nodes .Await
4040)
4141
42- _SubGraphNodes : TypeAlias = nodes .If | nodes .Try | nodes .For | nodes .While
42+ _SubGraphNodes : TypeAlias = nodes .If | nodes .Try | nodes .For | nodes .While | nodes . Match
4343_AppendableNodeT = TypeVar (
4444 "_AppendableNodeT" , bound = _StatementNodes | nodes .While | nodes .FunctionDef
4545)
@@ -106,6 +106,9 @@ def visitWith(self, node: nodes.With) -> None:
106106
107107 visitAsyncWith = visitWith
108108
109+ def visitMatch (self , node : nodes .Match ) -> None :
110+ self ._subgraph (node , f"match_{ id (node )} " , node .cases )
111+
109112 def _append_node (self , node : _AppendableNodeT ) -> _AppendableNodeT | None :
110113 if not self .tail or not self .graph :
111114 return None
@@ -117,9 +120,9 @@ def _subgraph(
117120 self ,
118121 node : _SubGraphNodes ,
119122 name : str ,
120- extra_blocks : Sequence [nodes .ExceptHandler ] = (),
123+ extra_blocks : Sequence [nodes .ExceptHandler | nodes . MatchCase ] = (),
121124 ) -> None :
122- """Create the subgraphs representing any `if` and `for` statements."""
125+ """Create the subgraphs representing any `if`, `for` or `match ` statements."""
123126 if self .graph is None :
124127 # global loop
125128 self .graph = PathGraph (node )
@@ -134,23 +137,34 @@ def _subgraph_parse(
134137 self ,
135138 node : _SubGraphNodes ,
136139 pathnode : _SubGraphNodes ,
137- extra_blocks : Sequence [nodes .ExceptHandler ],
140+ extra_blocks : Sequence [nodes .ExceptHandler | nodes . MatchCase ],
138141 ) -> None :
139- """Parse the body and any `else` block of `if` and `for` statements."""
142+ """Parse `match`/`case` blocks, or the body and `else` block of `if`/`for`
143+ statements.
144+ """
140145 loose_ends = []
141- self . tail = node
142- self . dispatch_list ( node . body )
143- loose_ends . append ( self . tail )
144- for extra in extra_blocks :
145- self . tail = node
146- self . dispatch_list ( extra . body )
147- loose_ends .append (self . tail )
148- if node . orelse :
146+ if isinstance ( node , nodes . Match ):
147+ for case in extra_blocks :
148+ if isinstance ( case , nodes . MatchCase ):
149+ self . tail = node
150+ self . dispatch_list ( case . body )
151+ loose_ends . append ( self . tail )
152+ loose_ends .append (node )
153+ else :
149154 self .tail = node
150- self .dispatch_list (node .orelse )
155+ self .dispatch_list (node .body )
151156 loose_ends .append (self .tail )
152- else :
153- loose_ends .append (node )
157+ for extra in extra_blocks :
158+ self .tail = node
159+ self .dispatch_list (extra .body )
160+ loose_ends .append (self .tail )
161+ if node .orelse :
162+ self .tail = node
163+ self .dispatch_list (node .orelse )
164+ loose_ends .append (self .tail )
165+ else :
166+ loose_ends .append (node )
167+
154168 if node and self .graph :
155169 bottom = f"{ self ._bottom_counter } "
156170 self ._bottom_counter += 1
0 commit comments