Skip to main content

confingo

A dataclass-driven configuration toolkit. Define your program's settings once as typed dataclasses, then load them from a config file, with everything validated and coerced against the schema on the way in.

The dataclass declaration is the single source of truth: it serves at once as the schema, the type validator, and the default values. Defaults are validated against the same annotations supplied values are coerced toward, so every authored default that reaches the object has a plain serializable form. Config objects compare by value and are unhashable; config_hash is the stable value-identity operation.

Installation

pip install confingo

Runs on Python 3.12 and newer.

Quick example

Define the schema as dataclasses. Any of them subclasses ConfigNode to get load, save, and hash methods over its own subtree; the optimizer section carries a bare annotation and builds itself, so optimizer.name is the one value the file must supply.

from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path
from typing import Literal

from confingo import ConfigNode


@dataclass
class OptimizerConfig(ConfigNode):
    name: Literal["adamw", "sgd"]
    lr: float = 3e-4


@dataclass
class TrainingConfig(ConfigNode):
    optimizer: OptimizerConfig
    seed: int = 0
    output_dir: Path = Path("runs")

Write a config file that supplies the required value and any leaves that differ from the defaults:

{
  "optimizer": {"name": "adamw", "lr": 0.001}
}

Load it into a typed, validated object and derive a stable run identity:

config = TrainingConfig.cfg.load_json("train.json")

config.optimizer.lr             # 0.001, coerced to float
config.seed                     # 0, from the default
run_id = config.cfg.hash()   # "344e28a35dd4"
saved = config.cfg.save_json(config.output_dir / run_id / "resolved.json")
saved.as_posix()                # "runs/344e28a35dd4/resolved.json"

The runnable version lives in examples/quickstart/ and is walked through in Getting started.

Validation walks the whole tree in one pass and reports every problem at once, each tagged with a dotted path:

confingo.ConfigError: config file train.json has 3 issues:
  - sed: unknown key (known keys: optimizer, output_dir, seed)
  - optimizer.name: expected one of 'adamw' | 'sgd', got 'adam'
  - optimizer.lr: expected float, got str

Arrays and tensors

NumPy arrays and PyTorch tensors work as field types whenever your application already imports the backend; the array/tensor integration activates from that already-imported backend and detects it at runtime. Values serialize as plain JSON data (a scalar for a 0-d value, nested lists otherwise) and rebuild against the annotated dtype, with bare torch.Tensor pinned to value-stable dtypes and Annotated[torch.Tensor, torch.float32] pinning a specific one. The rules live in arrays and tensors.

Documentation

Full documentation lives in docs/, which offers two routes through one set of pages.

Essentials covers everything needed to write, load, save, and debug a config:

Exact reference holds the precise rules, for lookup:

In one line

confingo packages the "config file plus dataclass schema" pattern into a reusable toolkit: a typed marshal / unmarshal pair over plain stdlib dataclasses, with exhaustive error reporting and a reproducible fingerprint.

Download files

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

Source Distribution

confingo-2.0.0.tar.gz (85.6 kB view details)

Uploaded Source

Built Distribution

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

confingo-2.0.0-py3-none-any.whl (95.0 kB view details)

Uploaded Python 3

File details

Details for the file confingo-2.0.0.tar.gz.

File metadata

  • Download URL: confingo-2.0.0.tar.gz
  • Upload date:
  • Size: 85.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.4

File hashes

Hashes for confingo-2.0.0.tar.gz
Algorithm Hash digest
SHA256 11f5159466890c6e1e430125d67e8eb9ec5e688bc52300630ea589c07653805b
MD5 f3d61ca4494be1cb2d09f7aa6a6e0dda
BLAKE2b-256 5c27d8d3abe813e75ce9cfb9d88519763008da65565fc2296db4b26aac822d6f

See more details on using hashes here.

File details

Details for the file confingo-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: confingo-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 95.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.4

File hashes

Hashes for confingo-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fa4e05c309fa55cfe77d72888e774a83e9f228077b23edbc5404baf3525c1329
MD5 aeb42452304a7a284470873c67d3614f
BLAKE2b-256 e7aa70b33a316d60807feed14eeb41814f1529df7bfe85764683fdf2ab938b0b

See more details on using hashes here.

Release history Release notifications | RSS feed

3.0.0

2 files

2.1.0

2 files

This release

2.0.0 This release

2 files

1.0.1

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page