Skip to content

An enum used as another enum's value is not unstructured by the json converter #679

@reversefold

Description

@reversefold

Trivial example:

import cattrs
import enum
import cattr.preconf.json

c = cattr.preconf.json.make_converter()
class TE(enum.Enum):
    A = 1
class TE2(enum.Enum):
    C = TE.A
print(repr(c.unstructure(TE2.C)))

Outputs:

<TE.A: 1>

Should output:

1

The actual use-case I have for this is an enum with a value that has a tuple with another enum's value and an additional integer value so it isn't quite this trivial. Nevertheless, the problem is the same. An enum's value may be one which needs to be recursively unstructured. In fact, the same issue arises if an attrs class instance is used as an enum's value:

import attrs
@attrs.define
class C:
    a: int
class EC(enum.Enum):
    D = C(1)
print(repr(c.unstructure(EC.D)))

The reverse issue also happens when trying to use structure on what the jsonable result should be:

c.structure({"a": 1}, EC)
c.structure(1, TE2)

Both fail.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions