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 |
samuelcolvin
left a comment
There was a problem hiding this comment.
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.
|
|
||
| (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.
please hard wrap this text.
Also models (classes) can changed to types.
|
|
||
| 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: |
| * **django-restful-framework** ``v3.9.4`` | ||
|
|
||
|
|
||
| Usage of ``Union`` in Annotations and Type Order |
There was a problem hiding this comment.
can you move this above benchmarks.
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 |
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
Unionwhen defining type annotations with multiple types and updated documentation. This is discussed further under #514Checklist
HISTORY.rsthas been updated#<number>@<whomever>