From 7e35c4402260b4b4c0f27de6663af0de9c941427 Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Mon, 16 Nov 2026 16:21:19 -0500 Subject: [PATCH] OwnDeclarations.declared_of: every value of a kind each base declares The fork's theme states walked vars(owner) over the MRO filtering by type; that is the same own-declaration read, keyed by kind instead of name. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016gZLdupVQyBzHTmHBC9Cyn --- src/metaclass_registry/own_declarations.py | 8 ++++++++ tests/test_own_declarations.py | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/metaclass_registry/own_declarations.py b/src/metaclass_registry/own_declarations.py index c52685b..17b8255 100644 --- a/src/metaclass_registry/own_declarations.py +++ b/src/metaclass_registry/own_declarations.py @@ -26,6 +26,14 @@ def declared_items(cls, name: str, bases: Iterable[type] | None = None) -> Itera for _, declared in cls.own_declarations(name, bases): yield from declared + @classmethod + def declared_of[Kind](cls, kind: type[Kind], bases: Iterable[type] | None = None) -> Iterator[Kind]: + """Every value of ``kind`` each base declares in its own body, in that order.""" + for base in cls.__mro__ if bases is None else bases: + for value in base.__dict__.values(): + if isinstance(value, kind): + yield value + @classmethod def declared_union(cls, name: str, bases: Iterable[type] | None = None) -> tuple[object, ...]: """The declared items, each once, nearest declaration first.""" diff --git a/tests/test_own_declarations.py b/tests/test_own_declarations.py index ac01e89..79cc8e5 100644 --- a/tests/test_own_declarations.py +++ b/tests/test_own_declarations.py @@ -3,8 +3,13 @@ from metaclass_registry.own_declarations import OwnDeclarations +class Marker: + pass + + class Root(OwnDeclarations): keys = ("a",) + root_marker = Marker() class Left(Root): @@ -17,6 +22,7 @@ class Right(Root): class Both(Left, Right): keys = ("c",) + both_marker = Marker() def test_own_declarations_follow_the_mro_and_skip_silent_bases() -> None: @@ -25,3 +31,5 @@ def test_own_declarations_follow_the_mro_and_skip_silent_bases() -> None: assert Both.declared_union("keys") == ("c", "b", "a") assert Both.declared_union("keys", reversed(Both.__mro__)) == ("a", "b", "c") assert Both.declared_union("missing") == () + assert list(Both.declared_of(Marker)) == [Both.both_marker, Root.root_marker] + assert list(Both.declared_of(Marker, reversed(Both.__mro__))) == [Root.root_marker, Both.both_marker]