Skip to main content

File-backed pydantic configuration with migration support

Project description

fluxconf

Tests PyPI version Supported versions license

File-backed Pydantic configuration with migration support.

Installation

pip install fluxconf

or with uv:

uv add fluxconf

Quick start

ConfigIO is a generic base class for reading and writing YAML-backed Pydantic models. Subclass it, set file_name and config_type, and you get type-safe read/write out of the box.

from pydantic import BaseModel
from fluxconf import ConfigIO

class AppConfig(BaseModel):
    name: str = "my-app"
    debug: bool = False

class AppConfigIO(ConfigIO[AppConfig]):
    file_name = "app.yml"
    config_type = AppConfig

io = AppConfigIO("~/.config/my-app")
io.write(AppConfig(name="my-app", debug=True))
config = io.read()  # AppConfig(name='my-app', debug=True)

Migrations

Migrations transform stored configuration data when your schema evolves. Instead of breaking existing config files, you define migration steps that update old data to match the new schema.

To use migrations, inherit from VersionedBaseModel instead of Pydantic's BaseModel. This adds a version field that tracks which migrations have been applied.

Migration keys follow the "N_description" format — the integer prefix determines execution order and is stored as the version in the config file. On read(), any pending migrations run automatically and the file is updated on disk.

JSON Patch migrations

The simplest approach: declare JSON Patch (RFC 6902) operations directly. No Python functions needed.

from fluxconf import ConfigIO, VersionedBaseModel

class ServerConfig(VersionedBaseModel):
    host: str = "localhost"
    port: int = 8080

class ServerConfigIO(ConfigIO[ServerConfig]):
    file_name = "server.yml"
    config_type = ServerConfig
    migrations = {
        "1_rename_host": [
            {"op": "move", "from": "/hostname", "path": "/host"},
        ],
        "2_add_port": [
            {"op": "add", "path": "/port", "value": 8080},
        ],
    }

Supported operations: add, remove, replace, move, copy, and test.

Python function migrations

When you need conditional logic or complex transforms, use a Python function. Each function receives the raw config dict and must return the updated dict.

from fluxconf import ConfigIO, VersionedBaseModel

class UserConfig(VersionedBaseModel):
    full_name: str = ""
    email: str = ""

def merge_name_fields(data: dict) -> dict:
    first = data.pop("first_name", "")
    last = data.pop("last_name", "")
    if first or last:
        data["full_name"] = f"{first} {last}".strip()
    return data

class UserConfigIO(ConfigIO[UserConfig]):
    file_name = "user.yml"
    config_type = UserConfig
    migrations = {
        "1_merge_name": merge_name_fields,
    }

Python functions and JSON Patches can be mixed freely in the same migrations dict.

Directory-based migrations

For projects with many migrations, store each one as a separate file in a directory instead of inlining them all in the class definition.

myapp/migrations/
    1_rename_host.json
    2_merge_name.py
    3_add_defaults.py
    _helpers.py           # skipped (starts with _)

Only files with an integer prefix are loaded. Files starting with _ or without an integer prefix are silently skipped, so helper modules can live alongside migration files.

.json files contain a JSON array of patch operations:

myapp/migrations/1_rename_host.json

[
    {"op": "move", "from": "/hostname", "path": "/host"}
]

.py files with a patch attribute are equivalent to .json files but written in Python:

myapp/migrations/3_add_defaults.py

patch = [
    {"op": "add", "path": "/port", "value": 8080},
    {"op": "add", "path": "/retries", "value": 3},
]

.py files with a migrate function offer full flexibility:

myapp/migrations/2_merge_name.py

def migrate(data: dict) -> dict:
    first = data.pop("first_name", "")
    last = data.pop("last_name", "")
    if first or last:
        data["full_name"] = f"{first} {last}".strip()
    return data

If a .py file defines both migrate and patch, the migrate function takes precedence.

Point migrations_dir at the directory to load them:

from pathlib import Path
from fluxconf import ConfigIO, VersionedBaseModel

class AppConfigIO(ConfigIO[AppConfig]):
    file_name = "app.yml"
    config_type = AppConfig
    migrations_dir = Path(__file__).parent / "migrations"

migrations and migrations_dir can be used together — fluxconf merges them, raising ValueError on key collisions.

Error handling

MigrationError is raised when a migration function or patch fails. It carries two attributes:

  • last_successful_migration — the version of the last migration that completed successfully (or the stored version if none succeeded)
  • original_error — the underlying exception

ValueError is raised when the stored version is ahead of the latest known migration. This typically means the config file was written by a newer version of the software than the one currently running.

License

MIT License

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

fluxconf-0.0.4.tar.gz (75.3 kB view details)

Uploaded Source

Built Distribution

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

fluxconf-0.0.4-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file fluxconf-0.0.4.tar.gz.

File metadata

  • Download URL: fluxconf-0.0.4.tar.gz
  • Upload date:
  • Size: 75.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for fluxconf-0.0.4.tar.gz
Algorithm Hash digest
SHA256 a6a3908dbac39c7e389a5c82abaa43632d64ff913385fa31e806572b95c3c99c
MD5 30d0e191acc1bb73d81a53cfcbf191fb
BLAKE2b-256 12b6b1e0243a0415a468844e6c49fc4c82859f21bec93696e84e1c912570d5d7

See more details on using hashes here.

File details

Details for the file fluxconf-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: fluxconf-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for fluxconf-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 616af81f8b8654a8e3029911ae91123b3486c75ec5942d20624bb08c41c13a67
MD5 731c6a18260d689019bbf5ba25f666f4
BLAKE2b-256 bfee7140c7d9d6849a9a4fdf2c3e4d9adcc9062d719dff3f052190a94a8ea46d

See more details on using hashes here.

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