Open morrison12 opened 2 months ago
@morrison12 currently non-primitive enum value is not supported.
def enum(self, arg: DeField[Any]) -> str:
return f"{typename(arg.type)}({self.primitive(arg)})"
https://github.com/yukinarit/pyserde/blob/main/serde/de.py#L936-L937
I will take a look if I can easily support non-primitive enum value
hmm currently pyserde relies on enum constructor for deserialization, but to support non-primitive enum value such as tuple requires to generate (de)serialize functions for enum. It is a quite bit of work..
hmm currently pyserde relies on enum constructor for deserialization, but to support non-primitive enum value such as tuple requires to generate (de)serialize functions for enum. It is a quite bit of work..
The one thought I had was to establish the convention (for pyserde
) that only simple enum's serialize their value and the rest serialize their name and then on the deserialization side it would be only a matter of choosing Enum(x) or Enum[x] as the constructor depending on whether it was a "simple" enum or not. Simple would be presumably be int
, float
, complex
, str
, bool
: but I don't know how simple, pardon the pun, it is to determine the value type on the deserialization side (esp. with things like Flag
).
@morrison12 currently non-primitive enum value is not supported.
def enum(self, arg: DeField[Any]) -> str: return f"{typename(arg.type)}({self.primitive(arg)})"
https://github.com/yukinarit/pyserde/blob/main/serde/de.py#L936-L937
I will take a look if I can easily support non-primitive enum value
The workaround isn't too horrible, something like:
class A(Enum):
...
@serde
@dataclass
class B:
....
foo: A = serde_field(serializer=lambda x:x.name, deserializer=lambda x:A[x])
Enum
s with "complex" values, for example atuple
, can't be deserialized as shown from the example below. In the example below, it appears it is because the values are deserialized as alist
but only atuple
is permitted !?! If the name and not the value(s) were serialized forEnum
s this could be avoided but would break the cases where the value is desired. Perhaps serialize the name for all butIntEnum
andStrEnum
?Example
Output
Environment Details