pydantic-yaml-parser


Namepydantic-yaml-parser JSON
Version 0.0.4 PyPI version JSON
download
home_pagehttps://github.com/hamelsmu/pydantic-yaml-parser
Summaryparse yaml files with pydantic
upload_time2023-06-09 05:09:38
maintainer
docs_urlNone
authorHamel Husain
requires_python>=3.7
licenseApache Software License 2.0
keywords pydantic yaml
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pydantic-yaml-parser

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

<div>

[![](https://github.com/hamelsmu/pydantic-yaml-parser/actions/workflows/test.yaml/badge.svg)](https://github.com/hamelsmu/pydantic-yaml-parser/actions/workflows/test.yaml)

</div>

## Install

``` sh
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:

``` python
yaml_string="""
setting_1: Hello
setting_2:
    - kind: name
      sublist:
          - first
          - second
"""
```

You might define a Pydantic model to represent it like this:

``` python
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](https://pyyaml.org/wiki/PyYAMLDocumentation):

``` python
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:

``` python
yaml_string="""
setting_1: ok
setting_2:
    - kind: name
      sublist: false
"""
yaml_dict_error = yaml.safe_load(yaml_string)
```

``` python
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:

``` python
from pydantic_yaml_parser.yaml import YamlModel

class Test(YamlModel):
    setting_1: str
    setting_2: List[Setting2]
```

``` python
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](00_yaml.ipynb#error-validation).



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hamelsmu/pydantic-yaml-parser",
    "name": "pydantic-yaml-parser",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "pydantic yaml",
    "author": "Hamel Husain",
    "author_email": "hamel.husain@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/8a/26/ce6824701a4eab87bfafd20abac45823bfcce578080b8284f9a407da41ec/pydantic-yaml-parser-0.0.4.tar.gz",
    "platform": null,
    "description": "# pydantic-yaml-parser\n\n<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->\n\n<div>\n\n[![](https://github.com/hamelsmu/pydantic-yaml-parser/actions/workflows/test.yaml/badge.svg)](https://github.com/hamelsmu/pydantic-yaml-parser/actions/workflows/test.yaml)\n\n</div>\n\n## Install\n\n``` sh\npip install pydantic_yaml_parser\n```\n\n## Background & Usage\n\n### 1. Loading YAML into Pydantic models\n\nTo load YAML into a Pydantic model, you have to first convert the YAML\nstring to a dict. For example, given the below yaml file:\n\n``` python\nyaml_string=\"\"\"\nsetting_1: Hello\nsetting_2:\n    - kind: name\n      sublist:\n          - first\n          - second\n\"\"\"\n```\n\nYou might define a Pydantic model to represent it like this:\n\n``` python\nfrom pydantic import BaseModel\nfrom typing import List\n\nclass Setting2(BaseModel):\n    kind: str\n    sublist: List\n\nclass Test(BaseModel):\n    setting_1: str\n    setting_2: List[Setting2]\n```\n\nYou can load the yaml file into a dict, and into the Pydantic model like\nso using [pyyaml](https://pyyaml.org/wiki/PyYAMLDocumentation):\n\n``` python\nimport yaml\nyml_dict = yaml.safe_load(yaml_string)\n\n# use `parse_obj` to load a dict\nTest.parse_obj(yml_dict)\n```\n\n    Test(setting_1='Hello', setting_2=[Setting2(kind='name', sublist=['first', 'second'])])\n\n### 2. The Pydantic validation error message\n\nHowever, let\u2019s say there is an error in your yaml file such that you\naccidentally set `sublist` to `false`, instead of setting `sublist` to\n`list` type, you will get an error message that looks like this:\n\n``` python\nyaml_string=\"\"\"\nsetting_1: ok\nsetting_2:\n    - kind: name\n      sublist: false\n\"\"\"\nyaml_dict_error = yaml.safe_load(yaml_string)\n```\n\n``` python\nTest.parse_obj(yaml_dict_error)\n```\n\n    ValidationError: 1 validation error for Test\n    setting_2 -> 0 -> sublist\n      value is not a valid list (type=type_error.list)\n\nThis error message is a bit confusing, especially for those with no\nprior experience with pydantic!\n\n### 3. Human readable error messages with `pydantic_yaml_parser`\n\nWhen you use `pydantic_yaml_parser` you get an error message that is\nmuch clearer:\n\n``` python\nfrom pydantic_yaml_parser.yaml import YamlModel\n\nclass Test(YamlModel):\n    setting_1: str\n    setting_2: List[Setting2]\n```\n\n``` python\nTest.from_dict(yaml_dict_error)\n```\n\n    ValueError: Configuration error(s) for YAML:\n     - value is not a valid list: `sublist` for element 0 in the list for `setting_2`\n\n## Further Reading\n\nFor more examples of error validation see [these\ndocs](00_yaml.ipynb#error-validation).\n\n\n",
    "bugtrack_url": null,
    "license": "Apache Software License 2.0",
    "summary": "parse yaml files with pydantic",
    "version": "0.0.4",
    "project_urls": {
        "Homepage": "https://github.com/hamelsmu/pydantic-yaml-parser"
    },
    "split_keywords": [
        "pydantic",
        "yaml"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ffe9ae27295af79cc054c01cd242ce8f1f0fa6b07645983caa2ac83a4411a3d9",
                "md5": "5904e549a3605743c398d72398624d43",
                "sha256": "fdea6be667ad6532c66c87bd186d972b56fd067b103596daaa27643f89be8aee"
            },
            "downloads": -1,
            "filename": "pydantic_yaml_parser-0.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5904e549a3605743c398d72398624d43",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 8463,
            "upload_time": "2023-06-09T05:09:36",
            "upload_time_iso_8601": "2023-06-09T05:09:36.974836Z",
            "url": "https://files.pythonhosted.org/packages/ff/e9/ae27295af79cc054c01cd242ce8f1f0fa6b07645983caa2ac83a4411a3d9/pydantic_yaml_parser-0.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8a26ce6824701a4eab87bfafd20abac45823bfcce578080b8284f9a407da41ec",
                "md5": "f2b807ee2aa6a0b58c4a427f0b82cc48",
                "sha256": "1739c1850d782022a423f93585cf68f7ed1725fd26e516a549e7c4ea7f9476b8"
            },
            "downloads": -1,
            "filename": "pydantic-yaml-parser-0.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "f2b807ee2aa6a0b58c4a427f0b82cc48",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 8830,
            "upload_time": "2023-06-09T05:09:38",
            "upload_time_iso_8601": "2023-06-09T05:09:38.692507Z",
            "url": "https://files.pythonhosted.org/packages/8a/26/ce6824701a4eab87bfafd20abac45823bfcce578080b8284f9a407da41ec/pydantic-yaml-parser-0.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-09 05:09:38",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hamelsmu",
    "github_project": "pydantic-yaml-parser",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "pydantic-yaml-parser"
}
        
Elapsed time: 0.30372s