From 1258bd24391ca7c7031b1466e5c6201d86a98869 Mon Sep 17 00:00:00 2001 From: Brian Pugh <bnp117@gmail.com> Date: Tue, 11 Apr 2023 19:14:40 -0700 Subject: [PATCH 1/2] Get rid of circular references to Registry class in RegistryMeta --- autoregistry/_registry.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) 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. From ee74b220c60c2b41a40e81d17237188ab3d08717 Mon Sep 17 00:00:00 2001 From: Brian Pugh <bnp117@gmail.com> Date: Tue, 11 Apr 2023 19:41:01 -0700 Subject: [PATCH 2/2] Added extended registry meta test --- tests/test_meta_extension.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/test_meta_extension.py 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"]