medium +25 pts

Metaclass Registry

Build a metaclass that automatically registers every subclass in a class-level registry.

Define a metaclass named `RegistryMeta` and a function `create_and_check` that tests it. The metaclass must automatically register every class that uses it (including subclasses of classes that use it) in a class attribute `registry` on the base class. The registry should be a dictionary mapping the class name (string) to the class object itself. Specifically, when a new class is created using `RegistryMeta`, the metaclass must add an entry to the `registry` attribute of the **base class** (the first class in the inheritance chain that has a `registry` attribute). All subclasses, even indirect ones, should be registered in the same base class's registry. Implement the metaclass so that the following behavior holds: - If a class explicitly sets a `registry` attribute as an empty dict in its body, that class becomes the base of a new registry. - Any subclass of a class that already has a `registry` attribute (inherited or defined) does **not** create a new registry; it uses the existing registry of the nearest base class that has one. - The metaclass should only register classes that have a non-empty `__mro__` chain ending in `object`. - The `registry` dictionary should be a class attribute on the base class, shared by all subclasses. Your task: write both `RegistryMeta` and a function `create_and_check()` that exercises it and returns `True` if all checks pass. The function does not take any arguments and must return a boolean. It should define at least the following classes and assertions: - `class Base(metaclass=RegistryMeta): registry = {}` - `class A(Base): pass` - `class B(A): pass` - Verify `Base.registry` has keys `['A', 'B']` (order does not matter) and that `Base.registry['B'] is B`. - Define `class OtherBase(metaclass=RegistryMeta): registry = {}` and `class C(OtherBase): pass`, then verify `OtherBase.registry` has key `'C'` and that `Base.registry is not OtherBase.registry`. - Also verify that the base classes themselves are not necessarily registered? Actually, in the intended behavior, the class that defines its own registry also registers itself. So `Base.registry['Base']` should be `Base` and `OtherBase.registry['OtherBase']` should be `OtherBase`. Use `assert` for all checks and return `True` at the end. **Function signatures:** ```python class RegistryMeta(type): pass def create_and_check() -> bool: pass ``` **Important details:** - When a new class is created, the metaclass's `__new__` method is called. Use it to perform registration. - The registry should map `cls.__name__` to `cls`. - If a class defines its own `registry` attribute in its body (as a dict), that class becomes a base for a new registry. All its subclasses will be registered into that dict. - If no base class has a `registry` attribute, create a new dict for this class (as its own `registry`). - Do register the base class itself if it defines an empty registry? Yes, it should be registered as well because it is a new class. - The metaclass must handle both direct and indirect subclasses. The `create_and_check` function must run without errors and return `True` when run in the test harness.

Constraints

No specific numeric constraints. The solution must work for arbitrary class hierarchies of any depth. The metaclass should run in O(1) per class creation and not use recursion.

Example

>>> create_and_check()
True
25 points ~25 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Overriding `__new__` is the cleanest place to mutate the class before it is fully created.
To find the registry, walk `bases` and check `hasattr(base, 'registry')`; pick the first base that has it.
If no base provides a registry, set a fresh dict on the new class and assign it as `cls.registry` after creation.
Remember that the class's own `__dict__` may already contain 'registry' if the class body defined it; prefer that over inherited registries.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.