Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/metaclass_registry/own_declarations.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"""

@classmethod
def own_declarations(cls, name: str, bases: Iterable[type] | None = None) -> Iterator[tuple[type, object]]:

Check failure on line 16 in src/metaclass_registry/own_declarations.py

View workflow job for this annotation

GitHub Actions / code-quality

ruff (E501)

src/metaclass_registry/own_declarations.py:16:101: E501 Line too long (111 > 100)
"""Each base, in MRO order unless ``bases`` gives another order or scope, with the
value it declares in its own body; bases that declare nothing are skipped."""
for base in cls.__mro__ if bases is None else bases:
Expand All @@ -26,6 +26,14 @@
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]:

Check failure on line 30 in src/metaclass_registry/own_declarations.py

View workflow job for this annotation

GitHub Actions / code-quality

ruff (E501)

src/metaclass_registry/own_declarations.py:30:101: E501 Line too long (104 > 100)

Check failure on line 30 in src/metaclass_registry/own_declarations.py

View workflow job for this annotation

GitHub Actions / code-quality

ruff (invalid-syntax)

src/metaclass_registry/own_declarations.py:30:20: invalid-syntax: Cannot use type parameter lists on Python 3.10 (syntax was added in Python 3.12)
"""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."""
Expand Down
8 changes: 8 additions & 0 deletions tests/test_own_declarations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -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]
Loading