Skip to main content

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

Project description

pydantic-yaml

PyPI version Unit Tests

This is a small helper library that adds some YAML capabilities to pydantic, namely dumping to yaml via the yaml_model.yaml() function, and parsing from strings/files using YamlModel.parse_raw() and YamlModel.parse_file(). It also adds an Enum subclass that gets dumped to YAML as a string, and fixes dumping of some typical types.

Basic Usage

Example usage is seen below.

from pydantic_yaml import YamlEnum, YamlModel


class MyEnum(str, YamlEnum):
    a = "a"
    b = "b"


class MyModel(YamlModel):
    x: int = 1
    e: MyEnum = MyEnum.a

m1 = MyModel(x=2, e="b")
yml = m1.yaml()
jsn = m1.json()

m2 = MyModel.parse_raw(yml)  # This automatically assumes YAML
assert m1 == m2

m3 = MyModel.parse_raw(jsn)  # This will fallback to JSON
assert m1 == m3

m4 = MyModel.parse_raw(yml, proto="yaml")
assert m1 == m4

m5 = MyModel.parse_raw(yml, content_type="application/yaml")
assert m1 == m5

Installation

pip install pydantic_yaml

Make sure to install ruamel.yaml (recommended) or pyyaml as well. These are optional dependencies:

pip install pydantic_yaml[ruamel]

pip install pydantic_yaml[pyyaml]

Versioned Models

Since YAML is often used for config files, there is also a VersionedYamlModel class.

The version attribute is parsed according to the SemVer (Semantic Versioning) specification.

Usage example:

from pydantic import ValidationError
from pydantic_yaml import VersionedYamlModel

class A(VersionedYamlModel):
    foo: str = "bar"


class B(VersionedYamlModel):
    foo: str = "bar"

    class Config:
        min_version = "2.0.0"

yml = """
version: 1.0.0
foo: baz
"""

A.parse_raw(yml)

try:
    B.parse_raw(yml)
except ValidationError as e:
    print("Correctly got ValidationError:", e, sep="\n")

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-0.4.1.tar.gz (9.8 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-0.4.1-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_yaml-0.4.1.tar.gz.

File metadata

  • Download URL: pydantic_yaml-0.4.1.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10

File hashes

Hashes for pydantic_yaml-0.4.1.tar.gz
Algorithm Hash digest
SHA256 7d9f876e933dc62d497676005f49808ec3b095f63080d1389f7ba7f7a878f0a2
MD5 f4267809a1ca59d766896330ebbff474
BLAKE2b-256 6ca2c66554806634556995b1393b0b171eb2d8c4e92d5a1f33a4ea0c4a87e9e7

See more details on using hashes here.

File details

Details for the file pydantic_yaml-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: pydantic_yaml-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10

File hashes

Hashes for pydantic_yaml-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 09f116592af91a9b7b520c71c05aa87fa8c4ba67dd33e6ad87410a53f7cb6721
MD5 1e0e0e3ed071de37cd01eb500d10436c
BLAKE2b-256 d1c68c48ebbc54ce99a8d3b1667f4bff918ef50033679efb0b71d9d3e80240f3

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