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

Union dump not working #67

Closed
elvinlemmens opened this issue Jan 29, 2020 · 6 comments · Fixed by #93
Closed

Union dump not working #67

elvinlemmens opened this issue Jan 29, 2020 · 6 comments · Fixed by #93
Labels
bug Something isn't working

Comments

@elvinlemmens
Copy link

elvinlemmens commented Jan 29, 2020

Hi,
I have noticed the dumping of a Union type is returning an empty object:

from marshmallow_dataclass import dataclass
from typing import List, Union

@dataclass
class Vector:
    x: float
    y: float

@dataclass
class Point:
    x: float
    z: float


@dataclass
class Geometries:
    elements: List[Union[Point, Vector]]

pts = Geometries.Schema().load({"elements": [
    {"x": 1, "y": 1},
    {"x": 1, "z": 1},
]})
schema=Geometries.Schema()
print(schema.dump(schema.load({"elements": [
    {"x": 1, "y": 1},
    {"x": 1, "z": 1},
]})))

gives

{"elements": [{}, {}]}

but should again return

{"elements": [
    {"x": 1, "y": 1},
    {"x": 1, "z": 1},
]}
@YBadiss
Copy link
Contributor

YBadiss commented Jan 30, 2020

As far as I can tell, the issue is not the Union but the List:

from marshmallow_dataclass import dataclass
from typing import Union, List

@dataclass
class Vector:
    x: float
    y: float

@dataclass
class Point:
    x: float
    z: float


@dataclass
class Geometries:
    single: Union[Point, Vector]
    multi: List[Union[Point, Vector]]

schema=Geometries.Schema()
pts = schema.load({"multi": [
    {"x": 1, "y": 1},
    {"x": 1, "z": 1}
], "single": {"x": 1, "z": 1}})
print(schema.dump(pts))  # {'single': {'z': 1.0, 'x': 1.0}, 'multi': [{}, {}]}

@lovasoa lovasoa added the bug Something isn't working label Jan 30, 2020
@elvinlemmens
Copy link
Author

Indeed, thanks for noticing. A normal List[Point] works fine for example. It's the combo of Union and List that is causing th eissue.

@YBadiss
Copy link
Contributor

YBadiss commented Jan 30, 2020

Actually it seems like there are multiple issues, the one above with List[Union], but also with simple Union. For example doing the following will fail on the second assert

@dataclass
class Geometries:
    single: Union[Point, Vector]

schema = Geometries.Schema()

res = schema.dump(Geometries(single=Point(x=1, z=2)))
# OK
assert res == {"single": {"x": 1.0, "z": 2.0}}, res

res = schema.dump(Geometries(single=Vector(x=1, y=2)))
# NOK, only "x" is dumped but that's just because both Vector and Point have an "x" attribute
assert res == {"single": {"x": 1.0, "y": 2.0}}, res

Changing the order of Vector and Point in the Union will change which assert succeeds and which one fails. So it's always the first type that is used in the Union when dumping.

@YBadiss
Copy link
Contributor

YBadiss commented Jan 30, 2020

It is actually probably linked to adamboche/python-marshmallow-union#27 not this repo

CedricCabessa added a commit to CedricCabessa/marshmallow_dataclass that referenced this issue Apr 27, 2020
marshmallow-union is not supported anymore and have some known issues
see:
lovasoa#67

Author advise to switch to marshmallow-polyfield
CedricCabessa added a commit to CedricCabessa/marshmallow_dataclass that referenced this issue Apr 27, 2020
marshmallow-union is not supported anymore and have some known issues
see:
lovasoa#67

Author advise to switch to marshmallow-polyfield
CedricCabessa added a commit to LedgerHQ/marshmallow_dataclass that referenced this issue May 4, 2020
marshmallow-union is not supported anymore and have some known issues
see:
lovasoa#67

Author advise to switch to marshmallow-polyfield
CedricCabessa added a commit to LedgerHQ/marshmallow_dataclass that referenced this issue May 13, 2020
marshmallow-union is not supported anymore and have some known issues
see:
lovasoa#67

Author advise to switch to marshmallow-polyfield
CedricCabessa added a commit to LedgerHQ/marshmallow_dataclass that referenced this issue May 13, 2020
marshmallow-union is not supported anymore and have some known issues
see:
lovasoa#67

Author advise to switch to marshmallow-polyfield
CedricCabessa added a commit to LedgerHQ/marshmallow_dataclass that referenced this issue May 13, 2020
marshmallow-union is not supported anymore and have some known issues
see:
lovasoa#67

Author advise to switch to marshmallow-polyfield
@lovasoa
Copy link
Owner

lovasoa commented May 23, 2020

This issue is caused by a bug in marshmallow-union. I opened a pull request to fix it: adamboche/python-marshmallow-union#33

@ammerzon
Copy link

ammerzon commented Jun 24, 2020

I tried the example from @YBadiss after upgrading to marshmallow-union v0.1.15.post1 but the serialization is still not working.

@dataclass
class Vector:
    x: float
    y: float


@dataclass
class Point:
    x: float
    z: float


@dataclass
class Geometries:
    single: Union[Point, Vector]


GeometriesSchema = marshmallow_dataclass.class_schema(Geometries)

res = GeometriesSchema().dump(Geometries(Point(x=1, z=2)))
# OK
assert res == {"single": {"x": 1.0, "z": 2.0}}, res

res = GeometriesSchema().dump(Geometries(Vector(x=1, y=2)))
# NOK, only "x" is dumped but that's just because both Vector and Point have an "x" attribute
assert res == {"single": {"x": 1.0, "y": 2.0}}, res

requirements.txt

marshmallow-dataclass[enum,union]==7.6.0
marshmallow-enum==1.5.1
marshmallow-union==0.1.15.post1

lovasoa added a commit that referenced this issue Jul 19, 2020
lovasoa added a commit that referenced this issue Jul 28, 2020
* Improve how union fields are handled

See #86
See #67

* Fix the tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants