Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for parsing from YAML file #136

Closed
drpoggi opened this issue Mar 2, 2018 · 6 comments
Closed

Support for parsing from YAML file #136

drpoggi opened this issue Mar 2, 2018 · 6 comments

Comments

@drpoggi
Copy link

drpoggi commented Mar 2, 2018

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)

@samuelcolvin
Copy link
Member

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?

@drpoggi
Copy link
Author

drpoggi commented Mar 4, 2018

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.

@stereobutter
Copy link

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

@NowanIlfideme
Copy link

NowanIlfideme commented Mar 12, 2021

Since this feature is specifically out-of-scope for Pydantic, and I couldn't find a good implementation of YAML parsing/dumping, I made the pydantic-yaml library. I made it to suit my needs, but if something's missing - feel free to make a feature request or PR.

@pcotton-intech
Copy link

Informative issue thanks @samuelcolvin
Thanks @NowanIlfideme for pydantic-yaml Mixins.

@NowanIlfideme
Copy link

Informative issue thanks @samuelcolvin
Thanks @NowanIlfideme for pydantic-yaml Mixins.

FYI, you can look at 1.0.0-alpha.1 version which removes mixins and works with normal Pydantic models. I would also appreciate the feedback on usability vs the stable version.

alexdrydew pushed a commit to alexdrydew/pydantic that referenced this issue Dec 23, 2023
* testing with hypothesis

* add failing case

* 3.7 compat

* 3.7 compat, take 2

* use hypothesis settings profiles

* add failing hypothesis cycles

* suggested branch_models_with_cycles

* hypothesis generating cyclic references

* fix recursive hypothesis tests

* check errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants