Pydantic: Support for parsing from YAML file

Created on 2 Mar 2018  路  3Comments  路  Source: samuelcolvin/pydantic

Hello, and thanks for creating this wonderful library. I was wondering if you would merge a PR that added YAML as a possible parsing protocol? Seems fairly trivial to add in parser.py, but I wanted to check with you before doing the work. (I would implement using pyyaml unless you think another one would be better)

Most helpful comment

Thanks for the question, and offering to submit a PR.

The answer is probably no, sorry.

The reason is that on reflection I think that parse.py was a mistake in the first place, and I'm intending to remove it. In turn, my reasoning for removing parsing from pydantic is as follows:

  • deserialising is completely separate from data validation: once you've deserialised some data pydantic really doesn't care where it came from
  • it's generally very simple, just Model.parse_obj(**xxx.loads(data))
  • there are numerous choices and sub choices, many of them requiring external libraries: json (std.json, ujson, simplejson, load vs. loads), msgpack (msgpack-python, msgpack, u-msgpack-python), yaml (PyYAML, yaml, python-yaml), parsing environment variables, maybe even xml
  • even with these options I often end up using other methods, for example with aiohttp I generally use Model.parse_obj(await request.json())
  • the exceptions are all different and you might well want to deal with them differently, eg. returning 400 or 415

So instead of this, I think it would be better to add a section to the help docs demonstrating how to load data from numerous different formats.

Does this make sense?

All 3 comments

Thanks for the question, and offering to submit a PR.

The answer is probably no, sorry.

The reason is that on reflection I think that parse.py was a mistake in the first place, and I'm intending to remove it. In turn, my reasoning for removing parsing from pydantic is as follows:

  • deserialising is completely separate from data validation: once you've deserialised some data pydantic really doesn't care where it came from
  • it's generally very simple, just Model.parse_obj(**xxx.loads(data))
  • there are numerous choices and sub choices, many of them requiring external libraries: json (std.json, ujson, simplejson, load vs. loads), msgpack (msgpack-python, msgpack, u-msgpack-python), yaml (PyYAML, yaml, python-yaml), parsing environment variables, maybe even xml
  • even with these options I often end up using other methods, for example with aiohttp I generally use Model.parse_obj(await request.json())
  • the exceptions are all different and you might well want to deal with them differently, eg. returning 400 or 415

So instead of this, I think it would be better to add a section to the help docs demonstrating how to load data from numerous different formats.

Does this make sense?

That makes perfect sense. I hadn't seen any discussion on adding YAML in the issue tracker or other PR's so I wanted to raise it here, but I definitely see your reasoning for not wanting to add it. Thanks for taking the time to explain.

If you remove parsing from pydantic, what will happen with BaseSettings which currently parses strings as json?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

samuelcolvin picture samuelcolvin  路  3Comments

sbv-trueenergy picture sbv-trueenergy  路  3Comments

nav picture nav  路  3Comments

engstrom picture engstrom  路  3Comments

timonbimon picture timonbimon  路  3Comments