Skip to main content

Config-driven component trees: pydantic-validated classes that self-register by tag and assemble recursively from plain dict/JSON specs

Project description

bausatz

German: “construction kit.”

Config-driven component trees for Python — pydantic-validated classes that self-register by a class_name tag and assemble recursively from plain dict/JSON/YAML specs.

from typing import Literal
from bausatz import Component, ComponentFactory

class Retriever(Component):                      # an abstract family (no tag -> not registered)
    class Config(Component.Config):
        pass

class Dense(Retriever):                          # a concrete member, registered as "dense"
    class Config(Retriever.Config):
        class_name: Literal["dense"] = "dense"
        top_k: int = 50

class Pipeline(Component):                       # a container builds its children through the factory
    class Config(Component.Config):
        class_name: Literal["pipeline"] = "pipeline"
        retrievers: list[dict]

    def _build_children(self, factory: ComponentFactory) -> None:
        self.retrievers = [factory.create_as(spec, Retriever) for spec in self.config.retrievers]

pipeline = ComponentFactory.get().create(
    {"class_name": "pipeline", "retrievers": [{"class_name": "dense", "top_k": 20}]}
)

A whole system becomes a spec: swap implementations by editing config, not code. Unknown keys are rejected, fields are validated, and to_json() round-trips every component back to the spec that built it.

The pieces

piece job
Component base for buildable classes — declares a typed nested Config (pydantic) and self-registers at class-definition time by its class_name tag
ComponentFactory turns a spec dict into an instance (create / typed create_as / create_many), recursing into container children; config_adapter() builds a pydantic discriminated union over every registered Config for one-pass validation of whole trees
Registry the tag → class table; duplicate tags fail loudly

Scoped registries

Registration defaults to a process-global factory (zero config for a single library). Libraries that must coexist in one process scope their family instead — tags then only need to be unique per library:

my_factory = ComponentFactory()

class MyBase(Component, factory=my_factory):     # the whole subtree registers into my_factory
    class Config(Component.Config):
        pass

Design notes

  • The tag lives on the Config (class_name: Literal["dense"] = "dense"), so it serializes with the config and validates like any other field.
  • Containers own their children: _build_children(factory) builds nested components through the same factory, so a whole tree assembles against one registry in one recursive pass.
  • Import = register: a component must be imported before a spec can name it (package __init__s that import their members are the idiomatic registration point).
  • Zero dependencies beyond pydantic v2. Python ≥ 3.12. Fully typed (py.typed).

Install

pip install bausatz

MIT licensed.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bausatz-0.1.0.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bausatz-0.1.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file bausatz-0.1.0.tar.gz.

File metadata

  • Download URL: bausatz-0.1.0.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for bausatz-0.1.0.tar.gz
Algorithm Hash digest
SHA256 87fb940d43943838cc02fdaeb151c43ac0ebe1cb271abd82ff29ddfa4247b70f
MD5 4f707dcc70643d09ea24520d54dc7899
BLAKE2b-256 47cd970e5f73ab28a7855be54c3e282b0d41cc8fcde29541935c7b97e1af606b

See more details on using hashes here.

Provenance

The following attestation bundles were made for bausatz-0.1.0.tar.gz:

Publisher: publish.yml on MatthiasDenecke/bausatz

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bausatz-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: bausatz-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for bausatz-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 07f3ff09755855c6e83172c8fc71146c4ab436775371afdc32869ac545e14324
MD5 28354dbd39dd7fd54f58467733b48fe1
BLAKE2b-256 fa5f01e00e6c0f884cac09f5a7c669b6d4a34d7a7376096558822489fea5efe0

See more details on using hashes here.

Provenance

The following attestation bundles were made for bausatz-0.1.0-py3-none-any.whl:

Publisher: publish.yml on MatthiasDenecke/bausatz

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page