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

Clarification around usage of Union #594

Merged

Conversation

Projects
None yet
2 participants
@somada141
Copy link
Contributor

commented Jun 14, 2019

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 #514

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • HISTORY.rst has been updated
    • if this is the first change since a release, please add a new section
    • include the issue number or this pull request number #<number>
    • include your github username @<whomever>

somada141 added some commits Jun 14, 2019

@codecov

This comment has been minimized.

Copy link

commented Jun 14, 2019

Codecov Report

Merging #594 into master will decrease coverage by 2.71%.
The diff coverage is n/a.

@@            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
@somada141

This comment has been minimized.

Copy link
Contributor Author

commented Jun 14, 2019

@samuelcolvin please have a look when you get a chance

@samuelcolvin
Copy link
Owner

left a 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.

This comment has been minimized.

Copy link
@samuelcolvin

samuelcolvin Jun 14, 2019

Owner

please hard wrap this text.

Also models (classes) can changed to types.

This comment has been minimized.

Copy link
@somada141

somada141 Jun 14, 2019

Author Contributor

done


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:

This comment has been minimized.

Copy link
@samuelcolvin

This comment has been minimized.

Copy link
@somada141

somada141 Jun 14, 2019

Author Contributor

done

@@ -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

This comment has been minimized.

Copy link
@samuelcolvin

samuelcolvin Jun 14, 2019

Owner

can you move this above benchmarks.

This comment has been minimized.

Copy link
@somada141

somada141 Jun 14, 2019

Author Contributor

done

Show resolved Hide resolved docs/examples/union_type_correct.py Outdated
Show resolved Hide resolved docs/examples/union_type_incorrect.py Outdated
Show resolved Hide resolved docs/examples/union_type_incorrect.py Outdated
Show resolved Hide resolved docs/examples/union_type_incorrect.py Outdated

somada141 and others added some commits Jun 14, 2019

Update docs/examples/union_type_incorrect.py
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Update docs/examples/union_type_incorrect.py
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Update docs/examples/union_type_incorrect.py
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Update docs/examples/union_type_correct.py
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
@somada141

This comment has been minimized.

Copy link
Contributor Author

commented Jun 14, 2019

@samuelcolvin I updated HISTORY.rst but didn't set a version for the release, does the way I added to it make sense?

somada141 added some commits Jun 14, 2019

Merge pull request #1 from samuelcolvin/master
Add support for InitVar (#591)
@somada141

This comment has been minimized.

Copy link
Contributor Author

commented Jun 14, 2019

nvm realised you pushed a new version on it. updated

@samuelcolvin
Copy link
Owner

left a comment

otherwise fine.

Show resolved Hide resolved HISTORY.rst Outdated
Show resolved Hide resolved docs/index.rst Outdated
Show resolved Hide resolved docs/index.rst Outdated
Show resolved Hide resolved docs/index.rst

somada141 and others added some commits Jun 15, 2019

Update docs/index.rst
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Update HISTORY.rst
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Update docs/index.rst
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Update docs/index.rst
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
@somada141

This comment has been minimized.

Copy link
Contributor Author

commented Jun 15, 2019

@samuelcolvin updated

@samuelcolvin samuelcolvin merged commit 84820bd into samuelcolvin:master Jun 15, 2019

4 of 7 checks passed

Header rules No header rules processed
Details
Pages changed 3 new files uploaded
Details
Redirect rules No redirect rules processed
Details
Mixed content No mixed content detected
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
deploy/netlify Deploy preview ready!
Details
samuelcolvin.pydantic Build #20190615.15 succeeded
Details
@samuelcolvin

This comment has been minimized.

Copy link
Owner

commented Jun 15, 2019

merged, thanks so much.

Docs will be updated on the next release.

@somada141

This comment has been minimized.

Copy link
Contributor Author

commented Jun 15, 2019

beautiful thanks a lot for the help @samuelcolvin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.