pydantic-yaml-parser
Install
pip install pydantic_yaml_parser
Background & Usage
To load YAML into a Pydantic model, you have to first convert the YAML to a dict. For example, given the below yaml file:
yaml_string="""
setting_1: Hello
setting_2:
- kind: name
sublist:
- first
- second
"""
You might define a Pydantic model to represent it like this:
from pydantic import BaseModel
from typing import List
class Setting2(BaseModel):
kind: str
sublist: List
class Test(BaseModel):
setting_1: str
setting_2: List[Setting2]
You can load the yaml file into a dict, and into the Pydantic model like so:
import yaml
yml_dict = yaml.safe_load(yaml_string)
yml_dict
{'setting_1': 'Hello',
'setting_2': [{'kind': 'name', 'sublist': ['first', 'second']}]}
To load this dict into the pydantic model Test, you can use
parse_obj:
Test.parse_obj(yml_dict)
Test(setting_1='Hello', setting_2=[Setting2(kind='name', sublist=['first', 'second'])])
However, let’s say there is an error in your yaml file such that you
accidentally set sublist to false, instead of setting sublist to
list type, you will get an error message that looks like this:
yaml_string="""
setting_1: ok
setting_2:
- kind: name
sublist: false
"""
yaml_dict_error = yaml.safe_load(yaml_string)
Test.parse_obj(yaml_dict_error)
ValidationError: 1 validation error for Test
setting_2 -> 0 -> sublist
value is not a valid list (type=type_error.list)
This error message is a bit confusing, especially for those with no
prior experience with pydantic! However when you use
pydantic_yaml_parser you get something a bit clearer:
from pydantic_yaml_parser.yaml import YamlModel
class Test(YamlModel):
setting_1: str
setting_2: List[Setting2]
Test.from_dict(yaml_dict_error)
ValueError: Configuration error(s) for YAML:
- value is not a valid list: `sublist` for element 0 in the list for `setting_2`
Metadata
Release files for pydantic-yaml-parser 0.0.1
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-parser-0.0.1.tar.gz | 8.7 kB | Without provenance | Details for pydantic-yaml-parser-0.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pydantic_yaml_parser-0.0.1-py3-none-any.whl | Python 3 | none | any | Details for pydantic_yaml_parser-0.0.1-py3-none-any.whl |
Total release size: 17.1 kB
Release files / pydantic-yaml-parser-0.0.1.tar.gz
| Download URL | pydantic-yaml-parser-0.0.1.tar.gz |
|---|---|
| Size | 8.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e48f10792e7c6c84a2704888b9a30641eb5bc37c10723048539a67e001722deb
|
|
BLAKE2b-256 checksum How to use checksums |
557c492ab183c26800d970c00eef2e0f784c5fb2ca9067b408dd0616e3fb0875
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.12
|
Release files / pydantic_yaml_parser-0.0.1-py3-none-any.whl
| Download URL | pydantic_yaml_parser-0.0.1-py3-none-any.whl |
|---|---|
| Size | 8.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9d96553fd7b690ba71a71adb3b7879c3fde05c4ed4b3081dbe3560a8843cdbbc
|
|
BLAKE2b-256 checksum How to use checksums |
00f3188726ef3f7274c5f4abc3b880a24ce5d9785d62b927a8750b793607294f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.12
|
Security for pydantic-yaml-parser 0.0.1
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.