-
Notifications
You must be signed in to change notification settings - Fork 4
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
docs: add docs about Union types and casting #383
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! I approve, but if you want you can adjust the comment according (kind of) to my suggestion.
docs/serialization.rst
Outdated
******************************************* | ||
|
||
When using Union types, pydantic will cast the value to the first type in the Union. This can cause unintended type casting. For example, if you have a field | ||
of type Union[float, int], and you set the value to 1, pydantic will cast the value to a float 1.0. This is an issue with Pydantic. More info can be found in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also worth noting here, that in the other direction, for x: Union[int, float]
, having x=1.99
in the constructor , not only the type gets cast into int, but the value actually changes to 1 (as a necessary consequence).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andrewthetechie : I'm not used to reviewing in github, but I saw that in commit 70b9bd2
, the actual tests are added. But this didn't appear in the review pane above. When I click the "Add your review" in the link below this message, I get the same view as that I already approved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the example, I added that as well.
The tests don't show up in the review pane because I added them in another branch without a PR.
@all-contributors add @david-wahlstedt for doc, review |
I've put up a pull request to add @david-wahlstedt! 🎉 |
#379