Configuration management for Python projects with dataclass-based schemas
Project description
Clevis
Configuration management for Python projects with dataclass-based schemas
Clevis provides type-safe configuration management for Python applications:
- Dataclass schemas — Define config structure with Python dataclasses
- TOML support — Load from
.tomlfiles - Env vars —
${VAR}interpolation (with envtoml/tomlev) - CLI generation — Auto-generate argparse from dataclass
- Layered config — User config < project config < CLI args
About the Name
A clevis is a U-shaped mechanical fastener that connects components while allowing pivoting. It's used in everything from agricultural equipment to aerospace control systems — a simple, robust connector that provides flexibility without compromising strength.
This library follows the same principle: it connects multiple configuration sources (TOML files, environment variables, CLI arguments) into a single, cohesive interface. Just as a mechanical clevis allows articulation, Clevis allows your configuration to flex and adapt — user-level defaults, project-level settings, and runtime overrides all pivot around a single dataclass schema.
Quick Start
# Install (Python 3.11+)
pip install clevis
# Or with env var support
pip install clevis[envtoml]
from dataclasses import dataclass
from clevis import get_config
@dataclass
class Config:
name: str = "MyApp"
debug: bool = False
config = get_config(Config, name="app")
Installation
Choose your TOML parser based on needs:
| Extra | Features | Use When |
|---|---|---|
| (none) | Stdlib tomllib |
Python 3.11+, minimal deps |
tomli |
Pure Python TOML | Python 3.10 compatibility |
envtoml |
${VAR} interpolation |
Environment-based config |
tomlev |
${VAR|default} syntax |
Env vars with defaults |
# Python 3.11+ - no extras needed
pip install clevis
# Python 3.10
pip install clevis[tomli]
# Environment variable support
pip install clevis[envtoml]
Usage
Define Your Config
from dataclasses import dataclass, field
@dataclass
class DatabaseConfig:
host: str = "localhost"
port: int = 5432
user: str | None = None
password: str | None = None
@dataclass
class AppConfig:
name: str = "MyApp"
debug: bool = False
database: DatabaseConfig = field(default_factory=DatabaseConfig)
Load Configuration
from clevis import get_config
# Load from ~/.myapp.toml and ./myapp.toml
config = get_config(AppConfig, name="myapp")
Configuration layers (lowest to highest priority):
- Dataclass defaults
- User-level TOML —
~/.{name}.toml - Project-level TOML —
./{name}.toml - CLI arguments —
--database-host,--debug
TOML Files
Create myapp.toml:
name = "Production App"
debug = true
[database]
host = "db.example.com"
port = 5432
With env var support (clevis[envtoml] or clevis[tomlev]):
[database]
password = "${DB_PASSWORD}" # envtoml
host = "${DB_HOST|localhost}" # tomlev (with default)
CLI Arguments
Clevis auto-generates CLI arguments:
python app.py --database-host localhost
python app.py --database-port 5432
python app.py --debug
Nested dataclasses become dashed arguments: database.host → --database-host
Testing
# Run tests
make test
# Run with coverage
make test-cov
API Reference
get_config(data_class, name="project", user=True, project=True, args=None)
Load configuration from TOML files and CLI arguments.
Parameters:
data_class— The dataclass type to populatename— Config file name (without.toml)user— Load user-level config (~/.{name}.toml)project— Load project-level config (./{name}.toml)args— CLI arguments (defaults tosys.argv[1:])
Returns: Instance of the dataclass with merged configuration
Raises:
ConfigError— Missing required fields or wrong typesImportError— No TOML parser available
Error Messages
Clevis provides helpful, actionable errors:
======================================================================
Configuration Error
======================================================================
Field: database.host
Issue: Required field has no value
Provide this value in one of these ways:
1. Project config: ./project.toml
[database]
host = "your_value"
2. User config: ~/.project.toml
(same format as above)
3. CLI argument: --database-host <value>
======================================================================
Acknowledgments
Clevis builds on excellent work from the Python community:
- tomllib — Python 3.11+ stdlib
- tomli — Pure Python TOML 1.0
- envtoml — Env var interpolation
- tomlev — Env vars with defaults
- dacite — Dict-to-dataclass conversion
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file clevis-0.1.0.tar.gz.
File metadata
- Download URL: clevis-0.1.0.tar.gz
- Upload date:
- Size: 108.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
63a0ac934b5a8b0dbb1f033cda26a2df729b24abc7d053a0b0fdace67e48f648
|
|
| MD5 |
516f949dda89e587b7d6456e1929ba67
|
|
| BLAKE2b-256 |
fecc73f5cb06f8f27940ad3cba4b6604d924f6e06e55bf21739364887f391c73
|
File details
Details for the file clevis-0.1.0-py3-none-any.whl.
File metadata
- Download URL: clevis-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7a57326811361dcaa5dd6b460890c5cdac8e0311823ec59ae689e6ff9217e110
|
|
| MD5 |
db572ec20540c9c73e7f7dc524f82013
|
|
| BLAKE2b-256 |
95816c560af6779defb92fb7d3f937a42921b77d78aa168fa59b41e9ff49204d
|