parse yaml files with pydantic
Project description
pydantic-yaml-parser
Install
pip install pydantic_yaml_parser
Background & Usage
1. Loading YAML into Pydantic models
To load YAML into a Pydantic model, you have to first convert the YAML string 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 using pyyaml:
import yaml
yml_dict = yaml.safe_load(yaml_string)
# use `parse_obj` to load a dict
Test.parse_obj(yml_dict)
Test(setting_1='Hello', setting_2=[Setting2(kind='name', sublist=['first', 'second'])])
2. The Pydantic validation error message
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!
3. Human readable error messages with pydantic_yaml_parser
When you use pydantic_yaml_parser
you get an error message that is
much 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`
Further Reading
For more examples of error validation see these docs.
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
Built Distribution
File details
Details for the file pydantic-yaml-parser-0.0.4.tar.gz
.
File metadata
- Download URL: pydantic-yaml-parser-0.0.4.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1739c1850d782022a423f93585cf68f7ed1725fd26e516a549e7c4ea7f9476b8 |
|
MD5 | f2b807ee2aa6a0b58c4a427f0b82cc48 |
|
BLAKE2b-256 | 8a26ce6824701a4eab87bfafd20abac45823bfcce578080b8284f9a407da41ec |
File details
Details for the file pydantic_yaml_parser-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: pydantic_yaml_parser-0.0.4-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fdea6be667ad6532c66c87bd186d972b56fd067b103596daaa27643f89be8aee |
|
MD5 | 5904e549a3605743c398d72398624d43 |
|
BLAKE2b-256 | ffe9ae27295af79cc054c01cd242ce8f1f0fa6b07645983caa2ac83a4411a3d9 |