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

Add explicit documentation regarding the ordering of Union types for serialization/deserialization #183

Open
john-bodley opened this issue Apr 18, 2022 · 1 comment

Comments

@john-bodley
Copy link
Contributor

john-bodley commented Apr 18, 2022

I was wondering whether there was merit in adding more documentation regarding the Union type, specifically related to the order of types is relevant—this is somewhat counter intuitive as the term union applies to set theory where sets are unordered.

For example say you have a field which could be a bool or int, then the following snippet,

from dataclasses import dataclass
from typing import Union

import marshmallow_dataclass


@dataclass
class Foo:
    bar: Union[bool, int]


foo_schema = marshmallow_dataclass.class_schema(Foo)()
foo = foo_schema.load({"bar": 1}) 

returns Foo(bar=True) rather than Foo(bar=1). Note for this specific example Union types aren't desirable given that if you switched to Union[int, bool] True would be deserialized to 1. Simply using Any seems preferred.

@lovasoa
Copy link
Owner

lovasoa commented Apr 19, 2022

Yes, you are right, the documentation is not very clear about how Unions work. PR welcome !

john-bodley added a commit to john-bodley/marshmallow_dataclass that referenced this issue Apr 20, 2022
lovasoa added a commit that referenced this issue Apr 21, 2022
* [README] Update README.md to address issue #183

* Update README.md

* fix typo in README.md

Co-authored-by: Ophir LOJKINE <[email protected]>
lovasoa added a commit that referenced this issue Apr 21, 2022
* [README] Update README.md to address issue #183

* Update README.md

* fix typo in README.md

Co-authored-by: Ophir LOJKINE <[email protected]>
lovasoa added a commit that referenced this issue Apr 21, 2022
* Fix forward references

* Fix forward references

* Add comment; Remove unused imports

* Remove unused imports

Fix flake8

Fix deprecated usage of 'self'

* Add usage inspect.stack to get locals from scope of class declaration

* Fix proper passing of class definition frame to class_schema of nested fields

* Change lambda to partial

* Add params im test_class_schema

* Fix type annotation in dataclass decorator. Add better doc

* [README] Add documentation about Union types (#184)

* [README] Update README.md to address issue #183

* Update README.md

* fix typo in README.md

Co-authored-by: Ophir LOJKINE <[email protected]>

* Fix use of typing.NewType in Python 3.10 (#180)

* Fix use of typing.NewType in Python 3.10

https://docs.python.org/3.10/library/typing.html#typing.NewType

"Changed in version 3.10: NewType is now a class rather than a function."

* Add Python 3.10 to GitHub workflow test matrix

Note the use of strings: otherwise YAML thinks we want to test Python 3.1

* Pin types-dataclasses version for Python 3.6

It looks broken for Python 3.6 due to python/typeshed@a40d79a

* fix ci (#185)

* Update dev dependencies
* mypy has gotten smarter, and it tries to do an automatic cast that breaks our code

* drop support for pre-commit hooks on 3.6

* downgrade types-dataclass

Co-authored-by: Adrian Dankiv <[email protected]>
Co-authored-by: Adrian Dankiv <[email protected]>
Co-authored-by: Aleksander Pawlak <[email protected]>
Co-authored-by: John Bodley <[email protected]>
Co-authored-by: Nicolas Noirbent <[email protected]>
lovasoa added a commit to epenet/marshmallow_dataclass that referenced this issue Apr 21, 2022
* [README] Update README.md to address issue lovasoa#183

* Update README.md

* fix typo in README.md

Co-authored-by: Ophir LOJKINE <[email protected]>
lovasoa added a commit to epenet/marshmallow_dataclass that referenced this issue Apr 21, 2022
* Fix forward references

* Fix forward references

* Add comment; Remove unused imports

* Remove unused imports

Fix flake8

Fix deprecated usage of 'self'

* Add usage inspect.stack to get locals from scope of class declaration

* Fix proper passing of class definition frame to class_schema of nested fields

* Change lambda to partial

* Add params im test_class_schema

* Fix type annotation in dataclass decorator. Add better doc

* [README] Add documentation about Union types (lovasoa#184)

* [README] Update README.md to address issue lovasoa#183

* Update README.md

* fix typo in README.md

Co-authored-by: Ophir LOJKINE <[email protected]>

* Fix use of typing.NewType in Python 3.10 (lovasoa#180)

* Fix use of typing.NewType in Python 3.10

https://docs.python.org/3.10/library/typing.html#typing.NewType

"Changed in version 3.10: NewType is now a class rather than a function."

* Add Python 3.10 to GitHub workflow test matrix

Note the use of strings: otherwise YAML thinks we want to test Python 3.1

* Pin types-dataclasses version for Python 3.6

It looks broken for Python 3.6 due to python/typeshed@a40d79a

* fix ci (lovasoa#185)

* Update dev dependencies
* mypy has gotten smarter, and it tries to do an automatic cast that breaks our code

* drop support for pre-commit hooks on 3.6

* downgrade types-dataclass

Co-authored-by: Adrian Dankiv <[email protected]>
Co-authored-by: Adrian Dankiv <[email protected]>
Co-authored-by: Aleksander Pawlak <[email protected]>
Co-authored-by: John Bodley <[email protected]>
Co-authored-by: Nicolas Noirbent <[email protected]>
lovasoa added a commit that referenced this issue Apr 21, 2022
* Relax constraints on typing-extensions

* [README] Add documentation about Union types (#184)

* [README] Update README.md to address issue #183

* Update README.md

* fix typo in README.md

Co-authored-by: Ophir LOJKINE <[email protected]>

* Fix use of typing.NewType in Python 3.10 (#180)

* Fix use of typing.NewType in Python 3.10

https://docs.python.org/3.10/library/typing.html#typing.NewType

"Changed in version 3.10: NewType is now a class rather than a function."

* Add Python 3.10 to GitHub workflow test matrix

Note the use of strings: otherwise YAML thinks we want to test Python 3.1

* Pin types-dataclasses version for Python 3.6

It looks broken for Python 3.6 due to python/typeshed@a40d79a

* fix ci (#185)

* Update dev dependencies
* mypy has gotten smarter, and it tries to do an automatic cast that breaks our code

* drop support for pre-commit hooks on 3.6

* downgrade types-dataclass

* Pr/aleksander pawlak/170 (#186)

* Fix forward references

* Fix forward references

* Add comment; Remove unused imports

* Remove unused imports

Fix flake8

Fix deprecated usage of 'self'

* Add usage inspect.stack to get locals from scope of class declaration

* Fix proper passing of class definition frame to class_schema of nested fields

* Change lambda to partial

* Add params im test_class_schema

* Fix type annotation in dataclass decorator. Add better doc

* [README] Add documentation about Union types (#184)

* [README] Update README.md to address issue #183

* Update README.md

* fix typo in README.md

Co-authored-by: Ophir LOJKINE <[email protected]>

* Fix use of typing.NewType in Python 3.10 (#180)

* Fix use of typing.NewType in Python 3.10

https://docs.python.org/3.10/library/typing.html#typing.NewType

"Changed in version 3.10: NewType is now a class rather than a function."

* Add Python 3.10 to GitHub workflow test matrix

Note the use of strings: otherwise YAML thinks we want to test Python 3.1

* Pin types-dataclasses version for Python 3.6

It looks broken for Python 3.6 due to python/typeshed@a40d79a

* fix ci (#185)

* Update dev dependencies
* mypy has gotten smarter, and it tries to do an automatic cast that breaks our code

* drop support for pre-commit hooks on 3.6

* downgrade types-dataclass

Co-authored-by: Adrian Dankiv <[email protected]>
Co-authored-by: Adrian Dankiv <[email protected]>
Co-authored-by: Aleksander Pawlak <[email protected]>
Co-authored-by: John Bodley <[email protected]>
Co-authored-by: Nicolas Noirbent <[email protected]>

Co-authored-by: John Bodley <[email protected]>
Co-authored-by: Ophir LOJKINE <[email protected]>
Co-authored-by: Nicolas Noirbent <[email protected]>
Co-authored-by: Adrian Dankiv <[email protected]>
Co-authored-by: Adrian Dankiv <[email protected]>
Co-authored-by: Aleksander Pawlak <[email protected]>
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

2 participants