Skip to main content

Adds some YAML functionality to the excellent `pydantic` library.

Project description

Pydantic-YAML

PyPI version Documentation Status Unit Tests

Pydantic-YAML adds YAML capabilities to Pydantic, which is an excellent Python library for data validation and settings management. If you aren't familiar with Pydantic, I would suggest you first check out their docs.

Documentation on ReadTheDocs.org

Basic Usage

from enum import Enum
from pydantic import BaseModel, validator
from pydantic_yaml import parse_yaml_raw_as, to_yaml_str

class MyEnum(str, Enum):
    """A custom enumeration that is YAML-safe."""

    a = "a"
    b = "b"

class InnerModel(BaseModel):
    """A normal pydantic model that can be used as an inner class."""

    fld: float = 1.0

class MyModel(BaseModel):
    """Our custom Pydantic model."""

    x: int = 1
    e: MyEnum = MyEnum.a
    m: InnerModel = InnerModel()

    @validator("x")
    def _chk_x(cls, v: int) -> int:  # noqa
        """You can add your normal pydantic validators, like this one."""
        assert v > 0
        return v

m1 = MyModel(x=2, e="b", m=InnerModel(fld=1.5))

# This dumps to YAML and JSON respectively
yml = to_yaml_str(m1)
jsn = m1.json()

# This parses YAML as the MyModel type
m2 = parse_yaml_raw_as(MyModel, yml)
assert m1 == m2

# JSON is also valid YAML, so this works too
m3 = parse_yaml_raw_as(MyModel, jsn)
assert m1 == m3

Configuration

Currently we use the JSON dumping of Pydantic to perform most of the magic.

This uses the Config inner class, as in Pydantic:

class MyModel(BaseModel):
    # ...
    class Config:
        # You can override these fields, which affect JSON and YAML:
        json_dumps = my_custom_dumper
        json_loads = lambda x: MyModel()
        # As well as other Pydantic configuration:
        allow_mutation = False

A separate configuration for YAML specifically will be added later.

# TODO

Breaking Changes for pydantic-yaml V1

The API for pydantic-yaml version 1.0.0 has been greatly simplified!

Mixin Class

This functionality has currently been removed! YamlModel and YamlModelMixin base classes are no longer needed. The plan is to re-add it before v1 fully releases, to allow the .yaml() or .parse_*() methods. However, this will be availble only for pydantic<2.

Versioned Models

This functionality has been removed, as it's questionably useful for most users. There is an example in the docs that's available.

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_yaml-1.0.0a2.tar.gz (17.2 kB view details)

Uploaded Source

Built Distribution

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

pydantic_yaml-1.0.0a2-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_yaml-1.0.0a2.tar.gz.

File metadata

  • Download URL: pydantic_yaml-1.0.0a2.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for pydantic_yaml-1.0.0a2.tar.gz
Algorithm Hash digest
SHA256 148a31bae9d70d5eb88830bb3d8eb9899de8d4f64a181370457c209e31d4cf32
MD5 c5ed0eb6db06763c902c75163fb761a2
BLAKE2b-256 e3ea3de105da56fc6e051d49da6772a64695417e4fe2eeae5785f5e70e71d648

See more details on using hashes here.

File details

Details for the file pydantic_yaml-1.0.0a2-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_yaml-1.0.0a2-py3-none-any.whl
Algorithm Hash digest
SHA256 116838bb0ec897c3ecd1131c70a90e32d6ac357df115d57281b825abe2262db6
MD5 512500bbf44981656955463a81b41f15
BLAKE2b-256 f0242300e93d5eadf8089276dbad37b0b25f18d9231d519d9c6f9ce67048375c

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