Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Enum misuse causes unhelpful error

Aperta
#601 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

  • 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'
Lingua principale
Python
Stelle
1.1k
Fork
159
Merge medio
12h 21m
PR unite (30g)
6

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di python-attrs/cattrs

Tutte le issue di python-attrs/cattrs

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.