Skip to content

Commit

Permalink
Merge pull request #952 from davep/css-walk-children-listify
Browse files Browse the repository at this point in the history
Modify DOMNode.walk_children to return a list
  • Loading branch information
davep authored Oct 19, 2022
2 parents 46a885c + b56fb01 commit d485949
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/textual/dom.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ def walk_children(
with_self: bool = True,
method: WalkMethod = "depth",
reverse: bool = False,
) -> Iterable[WalkType]:
) -> list[WalkType]:
...

@overload
Expand All @@ -644,7 +644,7 @@ def walk_children(
with_self: bool = True,
method: WalkMethod = "depth",
reverse: bool = False,
) -> Iterable[DOMNode]:
) -> list[DOMNode]:
...

def walk_children(
Expand All @@ -654,7 +654,7 @@ def walk_children(
with_self: bool = True,
method: WalkMethod = "depth",
reverse: bool = False,
) -> Iterable[DOMNode | WalkType]:
) -> list[DOMNode] | list[WalkType]:
"""Generate descendant nodes.
Args:
Expand All @@ -665,7 +665,7 @@ def walk_children(
reverse (bool, optional): Reverse the order (bottom up). Defaults to False.
Returns:
Iterable[DOMNode | WalkType]: An iterable of nodes.
list[DOMNode] | list[WalkType]: A list of nodes.
"""

Expand Down Expand Up @@ -708,13 +708,12 @@ def walk_breadth_first() -> Iterable[DOMNode]:
walk_depth_first() if method == "depth" else walk_breadth_first()
)

# We want a snapshot of the DOM at this point
# So that is doesn't change mid-walk
# We want a snapshot of the DOM at this point So that it doesn't
# change mid-walk
nodes = list(node_generator)
if reverse:
yield from reversed(nodes)
else:
yield from nodes
nodes.reverse()
return nodes

def get_child(self, id: str) -> DOMNode:
"""Return the first child (immediate descendent) of this node with the given ID.
Expand Down

0 comments on commit d485949

Please sign in to comment.