@@ -128,6 +128,11 @@ def __args__(self) -> tuple[Any, ...]:
128
128
def __argnames__ (self ) -> tuple [str , ...]:
129
129
"""Sequence of argument names."""
130
130
131
+ @property
132
+ def __children__ (self ) -> tuple [Node , ...]:
133
+ """Sequence of children nodes."""
134
+ return tuple (_flatten_collections (self .__args__ ))
135
+
131
136
def __rich_repr__ (self ):
132
137
"""Support for rich reprerentation of the node."""
133
138
return zip (self .__argnames__ , self .__args__ )
@@ -228,7 +233,7 @@ def find_topmost(self, pat: type, context: Optional[dict] = None) -> list[Node]:
228
233
if pat .match (node , ctx ) is not NoMatch :
229
234
result .append (node )
230
235
else :
231
- queue .extend (_flatten_collections ( node .__args__ ) )
236
+ queue .extend (node .__children__ )
232
237
seen .add (node )
233
238
else :
234
239
# fast path for locating a specific type
@@ -237,7 +242,7 @@ def find_topmost(self, pat: type, context: Optional[dict] = None) -> list[Node]:
237
242
if isinstance (node , pat ):
238
243
result .append (node )
239
244
else :
240
- queue .extend (_flatten_collections ( node .__args__ ) )
245
+ queue .extend (node .__children__ )
241
246
seen .add (node )
242
247
243
248
return result
@@ -454,7 +459,7 @@ def traverse(
454
459
455
460
if control is not halt :
456
461
if control is proceed :
457
- children = tuple ( _flatten_collections ( node .__args__ ))
462
+ children = node .__children__
458
463
elif isinstance (control , Iterable ):
459
464
children = control
460
465
else :
@@ -488,7 +493,7 @@ def bfs(root: Node) -> Graph:
488
493
489
494
while queue :
490
495
if (node := queue .popleft ()) not in graph :
491
- children = tuple ( _flatten_collections ( node .__args__ ))
496
+ children = node .__children__
492
497
graph [node ] = children
493
498
queue .extend (children )
494
499
@@ -524,7 +529,7 @@ def bfs_while(root: Node, filter: Optional[Any] = None) -> Graph:
524
529
if (node := queue .popleft ()) not in graph :
525
530
children = tuple (
526
531
child
527
- for child in _flatten_collections ( node .__args__ )
532
+ for child in node .__children__
528
533
if filter .match (child , {}) is not NoMatch
529
534
)
530
535
graph [node ] = children
@@ -555,7 +560,7 @@ def dfs(root: Node) -> Graph:
555
560
556
561
while stack :
557
562
if (node := stack .pop ()) not in graph :
558
- children = tuple ( _flatten_collections ( node .__args__ ))
563
+ children = node .__children__
559
564
graph [node ] = children
560
565
stack .extend (children )
561
566
@@ -591,7 +596,7 @@ def dfs_while(root: Node, filter: Optional[Any] = None) -> Graph:
591
596
if (node := stack .pop ()) not in graph :
592
597
children = tuple (
593
598
child
594
- for child in _flatten_collections ( node .__args__ )
599
+ for child in node .__children__
595
600
if filter .match (child , {}) is not NoMatch
596
601
)
597
602
graph [node ] = children
0 commit comments