Created
May 1, 2025 16:07
-
-
Save mypy-play/ff40e78844fa3c037ee3b05c2e317676 to your computer and use it in GitHub Desktop.
Shared via mypy Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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