From 5b6d2a385a0a2951af3f7ada4c409858dc25cf6a Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Mon, 16 Nov 2026 16:08:43 -0500 Subject: [PATCH] DOMNode and Component walk their families through metaclass_registry's Descendants DOMNode.contexts and Component.named's hand-written __subclasses__ walks are deleted; both classes inherit Descendants and read descendants(). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016gZLdupVQyBzHTmHBC9Cyn --- src/textual/css/native.py | 14 +++++--------- src/textual/dom.py | 15 ++------------- tests/test_navigation_spellings.py | 2 +- 3 files changed, 8 insertions(+), 23 deletions(-) diff --git a/src/textual/css/native.py b/src/textual/css/native.py index e674fb73e0..987e98fb85 100644 --- a/src/textual/css/native.py +++ b/src/textual/css/native.py @@ -7,6 +7,8 @@ from functools import cache, partial from typing import ClassVar, Protocol, Self, overload +from metaclass_registry.descendants import Descendants + from textual.css._declaration import NativeProperty, ValueBuilder from textual.css._style_properties import StyleProperty from textual.css.model import Declaration @@ -108,7 +110,7 @@ def of[Value]( ) -class Component(Enum): +class Component(Descendants, Enum): """A widget's styled parts. Members are the parts; each part's CSS component class name is derived here, once, from the declaring widget's prefix.""" @@ -126,14 +128,8 @@ def css_name(self) -> str: @cache def named(css_name: str) -> "Component | None": """The declared part a stylesheet component class names, if any.""" - pending: list[type[Component]] = [Component] - while pending: - parts = pending.pop() - pending.extend(parts.__subclasses__()) - for part in parts: - if part.css_name == css_name: - return part - return None + return next((part for parts in Component.descendants() for part in parts + if part.css_name == css_name), None) class DeclaresTheme: diff --git a/src/textual/dom.py b/src/textual/dom.py index 305530076f..fda945e98c 100644 --- a/src/textual/dom.py +++ b/src/textual/dom.py @@ -32,6 +32,7 @@ from rich.tree import Tree from formbridge.navigation_commands import NavigationCommand +from metaclass_registry.descendants import Descendants from metaclass_registry.own_declarations import OwnDeclarations from formbridge.key_declarations import TextEntryMode from textual._context import active_message_pump @@ -139,7 +140,7 @@ def __set__(self, obj: DOMNode, classes: str | Iterable[str]) -> None: @rich.repr.auto -class DOMNode(DeclaresTheme, PaintNode, MessagePump, OwnDeclarations): +class DOMNode(DeclaresTheme, PaintNode, MessagePump, OwnDeclarations, Descendants): """The base class for object that can be in the Textual DOM (App and Widget)""" _subtree_style_revision = 0 @@ -531,18 +532,6 @@ def _component_styles(self) -> dict[str, RenderStyles]: _own_key_families: ClassVar[tuple[type[NavigationCommand], ...]] = () key_families: ClassVar[tuple[type[NavigationCommand], ...]] = () - @classmethod - def contexts(cls) -> tuple[type[DOMNode], ...]: - """Native and application declarations share the actual node hierarchy.""" - pending = [cls] - found: dict[type[DOMNode], None] = {} - while pending: - owner = pending.pop() - if owner not in found: - found[owner] = None - pending.extend(reversed(owner.__subclasses__())) - return tuple(found) - @classmethod def method_key_owners(cls) -> tuple[type, ...]: """A native method-key capability declares the ancestor scope it exposes.""" diff --git a/tests/test_navigation_spellings.py b/tests/test_navigation_spellings.py index a3c185bbd6..1c70341971 100644 --- a/tests/test_navigation_spellings.py +++ b/tests/test_navigation_spellings.py @@ -15,7 +15,7 @@ def test_navigation_spellings_follow_every_declared_context() -> None: for name in widgets.__all__: getattr(widgets, name) checked = 0 - for owner in DOMNode.contexts(): + for owner in DOMNode.descendants(): standard = owner.binding_defaults(TextEntryMode) modal = owner.binding_defaults(VimMode) for direction in NavigationKey.__subclasses__():