-
Notifications
You must be signed in to change notification settings - Fork 50
Open
Labels
enhancementNew feature or requestNew feature or requesthelp wantedExtra attention is neededExtra attention is needed
Description
Enums with "complex" values, for example a tuple, can't be deserialized as shown from the example below.
In the example below, it appears it is because the values are deserialized as a list but only a tuple is permitted !?!
If the name and not the value(s) were serialized for Enums this could be avoided but would break the cases where the value is desired. Perhaps serialize the name for all but IntEnum and StrEnum ?
Example
"""
Example of serializing and deserializing an Enum with a "complex" value
"""
from dataclasses import dataclass
from enum import Enum, unique
from serde import serde
from serde.json import from_json, to_json
@unique
class B(Enum):
X = ("one", "some info on one")
Y = ("two", "some info on two")
Z = ("three", "some info on three")
@serde
@dataclass
class C:
m: B
original = C(B.Y)
as_json = to_json(original)
print(f"{as_json=}")
print(f"{B.Z.value=}")
nb = B(("three", "some info on three"))
print(f"{nb.value=}")
from_json = from_json(C, as_json)
print("de-serialized as:", as_json)
assert original == from_jsonOutput
as_json='{"m":["two","some info on two"]}'
B.Z.value=('three', 'some info on three')
nb.value=('three', 'some info on three')
Traceback (most recent call last):
File "/Users/james/Projects/spt/examples/ts9.py", line 32, in <module>
from_json = from_json(C, as_json)
^^^^^^^^^^^^^^^^^^^^^
File "/Users/james/.virtualenvs/spt/lib/python3.11/site-packages/serde/json.py", line 105, in from_json
return from_dict(c, de.deserialize(s, **opts), reuse_instances=False)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/james/.virtualenvs/spt/lib/python3.11/site-packages/serde/de.py", line 533, in from_dict
return from_obj(cls, o, named=True, reuse_instances=reuse_instances)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/james/.virtualenvs/spt/lib/python3.11/site-packages/serde/de.py", line 501, in from_obj
raise SerdeError(e) from None
serde.compat.SerdeError: failed to coerce the field C.m value ['two', 'some info on two'] into B: ['two', 'some info on two'] is not a valid B
Environment Details
pyserde==0.20.1
Python 3.11.9
Os: macOS-12.7.5-arm64-arm-64bit
Machine: arm64
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or requesthelp wantedExtra attention is neededExtra attention is needed