Open
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'
Metadata
Metadata
Assignees
Labels
No labels