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

Variant groups

When a field should hold any of several sections, declare a variant group: a base naming the key its sections select under, and one class per member naming the string a config file writes there.

from dataclasses import dataclass

from confingo import ConfigChoice


@dataclass
class Optimizer(ConfigChoice, tag_key="algorithm"):
    lr: float = 3e-4                      # shared by every variant


@dataclass
class AdamW(Optimizer, tag="adamw"):
    betas: tuple[float, float] = (0.9, 0.999)


@dataclass
class SGD(Optimizer, tag="sgd"):
    momentum: float = 0.9

A field annotated optimizer: Optimizer takes any variant, and the file names which one to build:

optimizer:
  algorithm: sgd
  lr: 0.1
  momentum: 0.8

One class is constructed, so its __post_init__ and __validate__ run once. The selection is required, and a file that leaves it out is told so at the key's own dotted path. It leads the exported section, so a saved snapshot loads back into the variant it came from. The rules live in variant groups.

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-3.0.0.tar.gz (101.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-3.0.0-py3-none-any.whl (111.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for confingo-3.0.0.tar.gz
Algorithm Hash digest
SHA256 54726d301192c8ddcb704dc35a66ed0b7a83e7a80dda6939eaacc87104a37bba
MD5 a17e969f0577818e6e7dc1f23b26f312
BLAKE2b-256 48a3c9e55e3f9f5310f58e6cae96b815a3db2503f66e18b5b86e082dbe18a937

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for confingo-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 750d348a906e864d3d05332707c8c94fb317858524d0c6592d07613dec8f555a
MD5 7499fad17f3908ba2ebe962814d09ffe
BLAKE2b-256 095b18cdfbf773a09bee221bd8953c263992508d07d20802d8ce7ff5cf555ef2

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

3.0.0 This release

2 files

2.1.0

2 files

2.0.0

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