pydantic-yaml
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")
Metadata
Release files for pydantic_yaml 0.4.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pydantic_yaml-0.4.3.tar.gz | 10.3 kB | Without provenance | Details for pydantic_yaml-0.4.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pydantic_yaml-0.4.3-py3-none-any.whl | Python 3 | none | any | Details for pydantic_yaml-0.4.3-py3-none-any.whl |
Total release size: 22.2 kB
Release files / pydantic_yaml-0.4.3.tar.gz
| Download URL | pydantic_yaml-0.4.3.tar.gz |
|---|---|
| Size | 10.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
97772436c334416d85bbaeba4fc6936a46febd2523ef1e53c3957ddf27cf1b30
|
|
BLAKE2b-256 checksum How to use checksums |
47ff187bcb7afe2cb53c34be7ee71dca8a53bf9e83c0b2b0ad2b3781b18f5bd4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
|
Release files / pydantic_yaml-0.4.3-py3-none-any.whl
| Download URL | pydantic_yaml-0.4.3-py3-none-any.whl |
|---|---|
| Size | 11.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
37a6c96bba0c033fe22eb69fc66ae275518f0732fbbb2520de65e1ff3036298d
|
|
BLAKE2b-256 checksum How to use checksums |
91932af0ad47205ccdaff72825245e7b32b7ac4f48d920caee607f6b8882f4d4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
|
Security for pydantic_yaml 0.4.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.