39
39
| nodes .Await
40
40
)
41
41
42
- _SubGraphNodes : TypeAlias = nodes .If | nodes .Try | nodes .For | nodes .While
42
+ _SubGraphNodes : TypeAlias = nodes .If | nodes .Try | nodes .For | nodes .While | nodes . Match
43
43
_AppendableNodeT = TypeVar (
44
44
"_AppendableNodeT" , bound = _StatementNodes | nodes .While | nodes .FunctionDef
45
45
)
@@ -106,6 +106,9 @@ def visitWith(self, node: nodes.With) -> None:
106
106
107
107
visitAsyncWith = visitWith
108
108
109
+ def visitMatch (self , node : nodes .Match ) -> None :
110
+ self ._subgraph (node , f"match_{ id (node )} " , node .cases )
111
+
109
112
def _append_node (self , node : _AppendableNodeT ) -> _AppendableNodeT | None :
110
113
if not self .tail or not self .graph :
111
114
return None
@@ -117,9 +120,9 @@ def _subgraph(
117
120
self ,
118
121
node : _SubGraphNodes ,
119
122
name : str ,
120
- extra_blocks : Sequence [nodes .ExceptHandler ] = (),
123
+ extra_blocks : Sequence [nodes .ExceptHandler | nodes . MatchCase ] = (),
121
124
) -> None :
122
- """Create the subgraphs representing any `if` and `for` statements."""
125
+ """Create the subgraphs representing any `if`, `for` or `match ` statements."""
123
126
if self .graph is None :
124
127
# global loop
125
128
self .graph = PathGraph (node )
@@ -134,23 +137,32 @@ def _subgraph_parse(
134
137
self ,
135
138
node : _SubGraphNodes ,
136
139
pathnode : _SubGraphNodes ,
137
- extra_blocks : Sequence [nodes .ExceptHandler ],
140
+ extra_blocks : Sequence [nodes .ExceptHandler | nodes . MatchCase ],
138
141
) -> None :
139
- """Parse the body and any `else` block of `if` and `for` statements."""
142
+ """Parse the body and any `else` block of `if`, `for` or `match ` statements."""
140
143
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 :
144
+ if isinstance ( node , nodes . Match ):
145
+ for case in extra_blocks :
146
+ if isinstance ( case , nodes . MatchCase ):
147
+ self . tail = node
148
+ self . dispatch_list ( case . body )
149
+ loose_ends . append ( self . tail )
150
+ loose_ends .append (node )
151
+ else :
149
152
self .tail = node
150
- self .dispatch_list (node .orelse )
153
+ self .dispatch_list (node .body )
151
154
loose_ends .append (self .tail )
152
- else :
153
- loose_ends .append (node )
155
+ for extra in extra_blocks :
156
+ self .tail = node
157
+ self .dispatch_list (extra .body )
158
+ loose_ends .append (self .tail )
159
+ if node .orelse :
160
+ self .tail = node
161
+ self .dispatch_list (node .orelse )
162
+ loose_ends .append (self .tail )
163
+ else :
164
+ loose_ends .append (node )
165
+
154
166
if node and self .graph :
155
167
bottom = f"{ self ._bottom_counter } "
156
168
self ._bottom_counter += 1
0 commit comments