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

Open
#679 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
data

Research direction

Start by reproducing the examples through cattr.preconf.json.make_converter(), focusing on unstructure and structure for enums whose values are other enums or attrs instances. Done means nested enum values and attrs instances are recursively converted in both directions, including the tuple-valued case described in the issue.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Python
Stars
1.1k
Forks
159
Avg merge
12h 21m
Merged PRs (30d)
6

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python-attrs/cattrs

All issues in python-attrs/cattrs

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.