cattrs icon indicating copy to clipboard operation
cattrs copied to clipboard

The value of enums should be unstructured and structured too

Open reversefold opened this issue 1 month ago • 4 comments

I'm running into an interesting issue where the value of an enum is a structured value which isn't being properly unstructured and that's causing issues with serialization.

Here's a simple example:

import attrs
import cattrs
import enum

class E(enum.Enum):
    A = 0
    B = 1

class EE(enum.Enum):
    A0 = (E.A, 0)
    A1 = (E.A, 1)
    B0 = (E.B, 0)
    B1 = (E.B, 1)

@attrs.define
class C:
    e: E
    ee: EE

c = C(e=E.B, ee=EE.A1)
print(cattrs.unstructure(c))

The e attribute of the C class is properly unstructured to a 1 value and the ee attribute is unstructured to the tuple (E.A, 1), but the E.A in that resulting tuple is not unstructured, which causes, for instance, JSON serialization to fail.

reversefold avatar Nov 24 '25 07:11 reversefold