Skip to content

Enum deserialization doesn't work with non-simple enum values #585

@morrison12

Description

@morrison12

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_json

Output

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

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions