← Open source

registry-pattern

PythonPydanticPydantic

registry-pattern provides name-based registries for classes and functions plus a recursive factory that builds object graphs from JSON-friendly {type, repo, data, meta} envelopes.

Example

from registry import TypeRegistry, build, serialize
import torch.nn as nn

class ModelRegistry(TypeRegistry[nn.Module]):
    pass

@ModelRegistry.register_artifact
class MLP(nn.Module):
    def __init__(self, hidden: int = 128):
        super().__init__()
        self.hidden = hidden

model = build({"type": "MLP", "data": {"hidden": 256}})  # -> MLP(hidden=256)
env = serialize(model)                                   # -> {type, data, meta}

How it works

A TypeRegistry or FunctionalRegistry collects artifacts by name via the register_artifact decorator. build() consumes a {type, repo, data, meta} envelope, validates the data against the target's signature with Pydantic, recurses on nested envelopes to assemble whole pipelines, and runs hooks; serialize() is the inverse, producing the same envelope for round-tripping. Registries are tree-shaped and pass post_init / serialize_meta (and pre_call / post_call) hooks down by inheritance, so a sub-registry can add validation or emit provenance into meta, with optional meters and reporters for observability. The core depends only on Pydantic and typing-extensions; published on PyPI as registry-pattern.