29

Metaclasses, __init_subclass__, and registries

A carrier registry filled by a class keyword on __init_subclass__, with refusals at definition, registrations that depend on imports, a metaclass form on the same table of cases, and what mypy checks about each.

Example 29Source on GitHub ↗

One shipping domain witnesses the registry that dissolves into __init_subclass__. A booking takes a carrier code and needs the gateway class for it, and other teams add carriers. Each test asserts the behaviour named in its title. No test registers a carrier in the test process: every class a test defines under CarrierBase is abstract or is refused before insertion, and a carrier that does register is checked in a fresh interpreter.

A registry filled at class creation

CarrierBase.__init_subclass__ takes the code as a class keyword and inserts the class into a read-only registry. gateway_for books through it and raises UnknownCarrierError for an unknown code. The signature takes no extra keywords, so mypy checks the keyword’s type and reports a misspelt keyword with a suggestion.

Refusing a bad carrier class

A duplicate code keeps the first class. A concrete class without a code and an abstract class with one are refused, an abstract intermediate class is skipped, and an unknown keyword raises TypeError. inspect.isabstract sees abstractness inside the hook. Every refusal leaves the registry unchanged.

Where registrations come from

carrier_registry imports no carrier module, so in a fresh interpreter the registry is empty until each carrier module is imported. A forgotten dict entry becomes a forgotten import.

The same registry as a metaclass

_CarrierMeta applies the same rules and passes the same table of bookings and refusals. A second metaclass that doesn’t derive from ABCMeta conflicts with an ABC or a Protocol base. mypy does not check a metaclass’s class keywords, and the reduced models show four more places where it differs. Only a metaclass gives the class object in, list and len.

Build, run, observe

From the repository root, once:

uv sync --locked --all-packages

Then change into the example directory:

cd examples/29-metaclasses-init-subclass-and-registries
uv run pytest -v
uv run mypy --strict src tests

The example README lists what each test verifies.