Enum misuse causes unhelpful error
Maintainers usually reply within 2 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 35/100
Research direction
The traceback names cattrs/converters.py and _unstructure_enum, with the generated unstructure_Site path showing where the failure surfaces. First reproduce the reduced attrs example and inspect how enum unstructuring is dispatched. The issue does not define the desired replacement behavior, so confirm the expected developer-facing outcome before deciding what test or changed failure would count as done.
Written by the indexing model from the issue text.
Description
- cattrs version: 24.1.2
- Python version: 3.11
- Operating System: Linux
Description
Use an enum's value as the default of a class (instead of the enum reference) causes an unhelpful backtrace.
tbh, I'm not entirely sure what the fix here is, but based on how much effort I put into finding this mistake, I think there's room for some improvement in developer experience.
What I Did
This code:
class SiteFlavors(enum.Enum):
SIMPLE = "simple"
@attrs.define
class Site():
flavor: SiteFlavors = "simple"
attrs.unstructure(Site())
Causes this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/astraluma/code/teahouse/barista/.venv/lib/python3.12/site-packages/cattrs/converters.py", line 238, in unstructure
return self._unstructure_func.dispatch(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<cattrs generated unstructure __main__.Site>", line 3, in unstructure_Site
File "/home/astraluma/code/teahouse/barista/.venv/lib/python3.12/site-packages/cattrs/converters.py", line 359, in _unstructure_enum
return obj.value
^^^^^^^^^
AttributeError: 'str' object has no attribute 'value'
which is obvious when reduced, but in my case, the usage was spread out between a definition, usage, and a wrapping library, so it looked more like:
File "/app/src/barista/routes/sites.py", line 44, in delete_site
await sitedb.attempt_delete(site)
File "/usr/local/lib/python3.13/site-packages/chaise/__init__.py", line 441, in attempt_delete
_, db, docid, etag = self._doc2blob(doc)
~~~~~~~~~~~~~~^^^^^
File "/usr/local/lib/python3.13/site-packages/chaise/__init__.py", line 308, in _doc2blob
blob = self._session.loader().dumpj(doc)
File "/usr/local/lib/python3.13/site-packages/chaise/__init__.py", line 129, in dumpj
blob = self.dump_doc(doc)
File "/usr/local/lib/python3.13/site-packages/chaise/attrs.py", line 194, in dump_doc
return converter.unstructure(doc)
~~~~~~~~~~~~~~~~~~~~~^^^^^
File "/usr/local/lib/python3.13/site-packages/cattrs/converters.py", line 238, in unstructure
return self._unstructure_func.dispatch(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
obj.__class__ if unstructure_as is None else unstructure_as
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
)(obj)
~^^^^^
File "<cattrs generated unstructure barista.models.Site>", line 4, in unstructure_Site
'flavor': __c_unstr_flavor(instance.flavor),
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.13/site-packages/cattrs/converters.py", line 359, in _unstructure_enum
return obj.value
^^^^^^^^^
AttributeError: 'str' object has no attribute 'value'
- Dominant language
- Python
- Stars
- 1.1k
- Forks
- 165
- Avg merge
- 3d 11h
- Merged PRs (30d)
- 5
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python-attrs/cattrs
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
python-attrs/cattrs#761 · 2 comments ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
python-attrs/cattrs#513 · 4 comments ·
Maintainers usually reply within 2 days
-
Difficulty 3/5 1-2 days Newbie friendliness 85/100
python-attrs/cattrs#774 ·
Maintainers usually reply within 2 days
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
python-attrs/cattrs#746 · 2 comments ·
Maintainers usually reply within 2 days
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
python-attrs/cattrs#743 · 2 comments ·
Maintainers usually reply within 2 days
All issues in python-attrs/cattrs
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
raullenchai/Rapid-MLX#4042 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
LearningCircuit/local-deep-research#7067 ·
Maintainers usually reply within 1 day
-
#bug
Difficulty 1/5 Under an hour Newbie friendliness 92/100
apache/superset#44923 · 1 comment ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
lawndoc/stack-back#123 ·