From afc65cbcbe3046f7cefc5a3856dc03191b5a8766 Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Mon, 16 Nov 2026 12:46:06 -0500 Subject: [PATCH] AbstractHooksMeta lives here, below Textual and Toad's shared contracts The metaclass that refuses to build a class whose MRO leaves an abstract hook unresolved, without ABCMeta's isinstance cost, sat in the Textual fork (textual/abstract_hooks.py). Toad's shared contracts (TargetRows, StartsSessions, WorkspaceViews, SessionOrigin, CoveredHistory, ...) use it, so every one of them imported Textual, and the Qt client on Android, which has no Textual, could not share them. It moves here unchanged; Textual's _MessagePumpMeta and Toad import it from metaclass_registry, and Textual's copy is deleted in its own PR. Check: tests/test_abstract_hooks.py, 2 passed: an unresolved mixin hook refuses construction; a resolved one builds and isinstance stays plain. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XbFMSofn8pw1EZ8PbTeYxm --- src/metaclass_registry/__init__.py | 2 ++ src/metaclass_registry/abstract_hooks.py | 27 +++++++++++++++++++ tests/test_abstract_hooks.py | 34 ++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 src/metaclass_registry/abstract_hooks.py create mode 100644 tests/test_abstract_hooks.py diff --git a/src/metaclass_registry/__init__.py b/src/metaclass_registry/__init__.py index 3983a98..f42c9b3 100644 --- a/src/metaclass_registry/__init__.py +++ b/src/metaclass_registry/__init__.py @@ -37,9 +37,11 @@ DuplicateRegistryKeyError, RegistryError, ) +from .abstract_hooks import AbstractHooksMeta from .mro_dispatch import MroDispatch, handles __all__ = [ + "AbstractHooksMeta", # Core "AutoRegisterMeta", "RegistryConfig", diff --git a/src/metaclass_registry/abstract_hooks.py b/src/metaclass_registry/abstract_hooks.py new file mode 100644 index 0000000..95d5355 --- /dev/null +++ b/src/metaclass_registry/abstract_hooks.py @@ -0,0 +1,27 @@ +"""Abstract methods enforced without ABCMeta.""" + +from __future__ import annotations + +from typing import Any + + +class AbstractHooksMeta(type): + """A class whose MRO declares an abstract method it still resolves can't be built. + + ABCMeta does the same but replaces isinstance and issubclass, which doubles their + cost on every check. This reads the whole MRO once, at class creation, so a plain + mixin's abstract hooks count as well as a declared base's. Assigning + __abstractmethods__ sets the flag object.__new__ refuses on. + """ + + def __init__( + cls, name: str, bases: tuple[type, ...], namespace: dict[str, Any], **kwargs: Any + ) -> None: + super().__init__(name, bases, namespace, **kwargs) + cls.__abstractmethods__ = frozenset( + member + for base in cls.__mro__ + for member, value in vars(base).items() + if getattr(value, "__isabstractmethod__", False) + and getattr(getattr(cls, member, None), "__isabstractmethod__", False) + ) diff --git a/tests/test_abstract_hooks.py b/tests/test_abstract_hooks.py new file mode 100644 index 0000000..a6e6f05 --- /dev/null +++ b/tests/test_abstract_hooks.py @@ -0,0 +1,34 @@ +"""A class whose MRO still leaves an abstract hook unresolved cannot be built; a mixin's +hooks count as well as a declared base's.""" + +from abc import abstractmethod + +import pytest + +from metaclass_registry import AbstractHooksMeta + + +class Hook: + @abstractmethod + def answer(self) -> int: ... + + +class Contract(Hook, metaclass=AbstractHooksMeta): + pass + + +class Answered(Contract): + def answer(self) -> int: + return 1 + + +def test_an_unresolved_mixin_hook_refuses_construction() -> None: + assert Contract.__abstractmethods__ == frozenset({"answer"}) + with pytest.raises(TypeError): + Contract() + + +def test_a_resolved_hook_builds_and_isinstance_stays_plain() -> None: + assert Answered().answer() == 1 + assert type(Contract) is AbstractHooksMeta + assert isinstance(Answered(), Hook)