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
Clarification around usage of Union #594
Clarification around usage of Union #594
Conversation
Codecov Report
@@ Coverage Diff @@
## master #594 +/- ##
==========================================
- Coverage 100% 97.28% -2.72%
==========================================
Files 15 15
Lines 2542 2542
Branches 504 504
==========================================
- Hits 2542 2473 -69
- Misses 0 58 +58
- Partials 0 11 +11 |
@samuelcolvin please have a look when you get a chance |
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.
Can you also add an entry to HISTORY.rst
, I know this isn't a code change but it's still useful in the changelog and it gets your name into contributors.
docs/index.rst
Outdated
|
||
(This script is complete, it should run "as is") | ||
|
||
However, as can be seen above, *pydantic* will attempt to 'match' any of the models (classes) defined under ``Union`` and will use the first one that matches. In the above example the ``id`` of ``user_03`` was defined as a ``uuid.UUID`` class (which is defined under the attribute's ``Union`` annotation) but as the ``uuid.UUID`` can be marshalled into an ``int`` it chose to match against the ``int`` type and disregarded the other types. |
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.
please hard wrap this text.
Also models (classes)
can changed to types
.
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.
done
docs/index.rst
Outdated
|
||
However, as can be seen above, *pydantic* will attempt to 'match' any of the models (classes) defined under ``Union`` and will use the first one that matches. In the above example the ``id`` of ``user_03`` was defined as a ``uuid.UUID`` class (which is defined under the attribute's ``Union`` annotation) but as the ``uuid.UUID`` can be marshalled into an ``int`` it chose to match against the ``int`` type and disregarded the other types. | ||
|
||
As such, it is recommended that when defining ``Union`` annotations that the most specific model (class) is defined first and followed by less specific models (classes). In the above example, the ``UUID`` class should precede the ``int`` and ``str`` classes to preclude the unexpected representation as such: |
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.
same.
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.
done
docs/index.rst
Outdated
@@ -949,6 +949,24 @@ Benchmarks were run with python 3.7.2 and the following package versions: | |||
* **django-restful-framework** ``v3.9.4`` | |||
|
|||
|
|||
Usage of ``Union`` in Annotations and Type Order |
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.
can you move this above benchmarks.
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.
done
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
…ada141/pydantic into fix/add_union_gotcha_to_doco
@samuelcolvin I updated |
Add support for InitVar (pydantic#591)
…a_to_doco # Conflicts: # HISTORY.rst
nvm realised you pushed a new version on it. updated |
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.
otherwise fine.
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
@samuelcolvin updated |
merged, thanks so much. Docs will be updated on the next release. |
beautiful thanks a lot for the help @samuelcolvin |
Change Summary
Adds examples of the incorrect and correct usage of
Union
when defining type annotations with multiple types and updated documentation. This is discussed further under #514Checklist
HISTORY.rst
has been updated#<number>
@<whomever>