-
Notifications
You must be signed in to change notification settings - Fork 76
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
Comments
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': [{}, {}]} |
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. |
Actually it seems like there are multiple issues, the one above with @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 |
It is actually probably linked to adamboche/python-marshmallow-union#27 not this repo |
marshmallow-union is not supported anymore and have some known issues see: lovasoa#67 Author advise to switch to marshmallow-polyfield
marshmallow-union is not supported anymore and have some known issues see: lovasoa#67 Author advise to switch to marshmallow-polyfield
marshmallow-union is not supported anymore and have some known issues see: lovasoa#67 Author advise to switch to marshmallow-polyfield
marshmallow-union is not supported anymore and have some known issues see: lovasoa#67 Author advise to switch to marshmallow-polyfield
marshmallow-union is not supported anymore and have some known issues see: lovasoa#67 Author advise to switch to marshmallow-polyfield
marshmallow-union is not supported anymore and have some known issues see: lovasoa#67 Author advise to switch to marshmallow-polyfield
This issue is caused by a bug in marshmallow-union. I opened a pull request to fix it: adamboche/python-marshmallow-union#33 |
I tried the example from @YBadiss after upgrading to @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 |
Hi,
I have noticed the dumping of a Union type is returning an empty object:
gives
{"elements": [{}, {}]}
but should again return
The text was updated successfully, but these errors were encountered: