Skip to main content

Bring version control and history to your Pydantic schemas

Project description

pydantic-versions

Bring version control and history to your Pydantic schemas.

pydantic-versions logo

pydantic-versions lets projects register ordered schema versions, derive historical Pydantic models from a current model, validate historical payloads, render historical config shapes, and upgrade data to the current model.

Install

pip install pydantic-versions

With uv:

uv add pydantic-versions

Example

Schema versions are independent from software versions. A config payload can declare the schema it uses, and the latest software can still validate and upgrade it:

from pydantic import BaseModel
from pydantic_versions import (
    dump_versioned,
    field_default,
    field_removed,
    migration,
    schema_version,
    validate_versioned,
    versioned_schema,
)


@versioned_schema(
    name="app_config",
    versions=["1", "2"],
    current="2",
    version_field="schema_version",
    missing_version="1",
)
@schema_version("1", patches=[
    field_default("timeout", 5.0),
    field_removed("new_feature"),
])
class AppConfig(BaseModel):
    timeout: float = 10.0
    retries: int = 3
    new_feature: bool = False


@migration(AppConfig, "1", "2")
def migrate_v1_to_v2(data: dict) -> dict:
    data.setdefault("new_feature", False)
    return data


result = validate_versioned(AppConfig, {"schema_version": "1", "retries": 2})
assert result.current_model == AppConfig(timeout=5.0, retries=2, new_feature=False)

v1_config = dump_versioned(AppConfig, version="1")
assert v1_config == {"timeout": 5.0, "retries": 3, "schema_version": "1"}

missing_version is only for legacy config files that do not contain a schema version field. For example, missing_version="1" means "if a payload has no schema_version, treat it as an old v1 config." If you do not set it, unversioned input raises MissingSchemaVersionError.

The docs include a larger nested config example and adoption guidance for deciding when to use patches, migrations, nested version fields, and legacy unversioned fallbacks.

Development

Install dependencies:

uv sync

Run the main checks:

make ci

Useful commands:

  • make format: format with Ruff.
  • make lint: lint and auto-fix with Ruff.
  • make typecheck: run ty.
  • make test: run pytest.
  • make docs-build: build the docs site.

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

pydantic_versions-0.1.0.tar.gz (899.3 kB view details)

Uploaded Source

Built Distribution

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

pydantic_versions-0.1.0-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pydantic_versions-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3b515be7922467d6b4dc333580c43b48044f16af03005893746e987a0544cb25
MD5 a3ea7a876cc5e3682562fe34d20a0a62
BLAKE2b-256 4ad97805790d531cf75c51d1eda05471a91f00ae03023498fc79c6d8c956ce7c

See more details on using hashes here.

Provenance

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

Publisher: release.yml on dariuszpanas/pydantic-versions

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

File details

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

File metadata

File hashes

Hashes for pydantic_versions-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 621070075311178de0e1ee3ed624d9c9851a090abbdc4b30be27f918e5f38e9b
MD5 a734c5017e0788c575ca541ef605c7b0
BLAKE2b-256 1adc7ea88765aedf2e58d4ab8f2ef0a9e1a062c39acc8f6159bbeb01fd7e9060

See more details on using hashes here.

Provenance

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

Publisher: release.yml on dariuszpanas/pydantic-versions

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