Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created May 1, 2025 16:07
Show Gist options
  • Save mypy-play/ff40e78844fa3c037ee3b05c2e317676 to your computer and use it in GitHub Desktop.
Save mypy-play/ff40e78844fa3c037ee3b05c2e317676 to your computer and use it in GitHub Desktop.
Shared via mypy Playground
"""
Functions for *walking* the DOM.
!!! note
For most purposes you would be better off using [query][textual.dom.DOMNode.query], which uses these functions internally.
"""
from __future__ import annotations
from collections import deque
from typing import TYPE_CHECKING, Iterable, Iterator, TypeVar, overload
class DOMNode:
pass
WalkType = TypeVar("WalkType", bound=DOMNode)
if TYPE_CHECKING:
@overload
def walk_depth_first(
root: DOMNode,
*,
with_root: bool = True,
) -> Iterable[DOMNode]: ...
@overload
def walk_depth_first(
root: WalkType,
filter_type: type[WalkType],
*,
with_root: bool = True,
) -> Iterable[WalkType]: ...
def walk_depth_first(
root: DOMNode,
filter_type: type[WalkType] | None = None,
*,
with_root: bool = True,
) -> Iterable[DOMNode] | Iterable[WalkType]:
return []
if TYPE_CHECKING:
@overload
def walk_breadth_first(
root: DOMNode,
*,
with_root: bool = True,
) -> Iterable[DOMNode]: ...
@overload
def walk_breadth_first(
root: WalkType,
filter_type: type[WalkType],
*,
with_root: bool = True,
) -> Iterable[WalkType]: ...
def walk_breadth_first(
root: DOMNode,
filter_type: type[WalkType] | None = None,
*,
with_root: bool = True,
) -> Iterable[DOMNode] | Iterable[WalkType]:
return []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment