Pydantic-YAML
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
# You can also auto-generate comments in YAML from the model docstrings and field descriptions
print(to_yaml_str(m1, add_comments=True))
See comment generation docs for more info.
With Pydantic v2, you can also dump dataclasses:
from pydantic import RootModel
from pydantic.dataclasses import dataclass
from pydantic.version import VERSION as PYDANTIC_VERSION
from pydantic_yaml import to_yaml_str
assert PYDANTIC_VERSION >= "2"
@dataclass
class YourType:
foo: str = "bar"
obj = YourType(foo="wuz")
assert to_yaml_str(RootModel[YourType](obj)) == 'foo: wuz\n'
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
You can control some YAML-specfic options via the keyword options:
to_yaml_str(model, indent=4) # Makes it wider
to_yaml_str(model, map_indent=9, sequence_indent=7) # ... you monster.
You can additionally pass your own YAML instance:
from ruamel.yaml import YAML
my_writer = YAML(typ="safe")
my_writer.default_flow_style = True
to_yaml_file("foo.yaml", model, custom_yaml_writer=my_writer)
A separate configuration for YAML specifically will be added later, likely in v2.
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.
Release files for pydantic_yaml 1.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pydantic_yaml-1.7.0.tar.gz | 96.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pydantic_yaml-1.7.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 121.4 kB
Release files / pydantic_yaml-1.7.0.tar.gz
| Download URL | pydantic_yaml-1.7.0.tar.gz |
|---|---|
| Size | 96.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b7c9420647537166c58ff9b150176a434fbdbae843d076e37011dd7e3e2e0044
|
|
BLAKE2b-256 checksum How to use checksums |
c1c91952345a95c9699783ea138a0cc47a151189adbbc2be5106fdab3788eaab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.5 {"installer":{"name":"uv","version":"0.11.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / pydantic_yaml-1.7.0-py3-none-any.whl
| Download URL | pydantic_yaml-1.7.0-py3-none-any.whl |
|---|---|
| Size | 25.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
794867172285e444653605c16adfc4b874938ce60132eff24cda4e3c590f75f3
|
|
BLAKE2b-256 checksum How to use checksums |
53a5198079eb0445eeaa28c732e2b87df7e2bf3452a8823a8774398b4104b674
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.5 {"installer":{"name":"uv","version":"0.11.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|