diff --git a/autoregistry/_registry.py b/autoregistry/_registry.py index cec0091..759ffeb 100644 --- a/autoregistry/_registry.py +++ b/autoregistry/_registry.py @@ -202,14 +202,6 @@ def __new__( # Manipulate namespace instead of modifying attributes after calling __new__ so # that hooks like __init_subclass__ have appropriately set registry attributes. # Each subclass gets its own registry. - try: - Registry # pyright: ignore[reportUnusedExpression] - except NameError: - # Should only happen the very first time that - # Registry is being defined. - namespace["__registry__"] = _Registry(RegistryConfig(**config)) - new_cls = super().__new__(cls, cls_name, bases, namespace) - return new_cls # Copy the nearest parent config, then update it with new params for parent_cls in bases: @@ -219,7 +211,10 @@ def __new__( except AttributeError: pass else: - raise InternalError("Should never happen.") # pragma: no cover + # No parent config, create a new one from scratch. + namespace["__registry__"] = _Registry(RegistryConfig(**config)) + new_cls = super().__new__(cls, cls_name, bases, namespace) + return new_cls # Derive registry name before updating registry config, since a classes own name is # subject to it's parents configuration, not its own. diff --git a/tests/test_meta_extension.py b/tests/test_meta_extension.py new file mode 100644 index 0000000..c8e6e25 --- /dev/null +++ b/tests/test_meta_extension.py @@ -0,0 +1,26 @@ +from autoregistry import RegistryMeta + + +class ExtendedRegistryMeta(RegistryMeta): + def __call__(cls, *args, **kwargs): # noqa: N805 + out = super().__call__(*args, **kwargs) + out.extended_attribute = cls.__name__ + return out + + +class Foo(metaclass=ExtendedRegistryMeta): + pass + + +class Bar(Foo): + pass + + +def test_extended_registry(): + foo = Foo() + bar = Bar() + + assert foo.extended_attribute == "Foo" + assert bar.extended_attribute == "Bar" + + assert list(Foo) == ["bar"]