Validation errors from multiple fields are not in ClassValidationError
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 45/100
Direzione di ricerca
Start by running the provided Python reproducer against cattrs.structure and then inspect cattrs.transform_exception with the resulting ClassValidationError. Trace how validation failures from fields a and b are collected and transformed; done means both field errors appear in ClassValidationError and the transformed output identifies their locations.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
python version: 3.13.7
attrs version: 25.4.0
cattrs version 25.3.0
When I try to structure a dict into a class, I only see a validation error from one field, when I would expect to see multiple.
Example:
import attrs
import cattrs
@attrs.define()
class Foo:
a: str = attrs.field(validator=[attrs.validators.min_len(3)])
b: str = attrs.field(validator=[attrs.validators.min_len(3)])
cattrs.structure(
{"a": "a", "b": "b"},
Foo,
)
I am expecting to see validation errors from fields a and b, but only see a:
Traceback (most recent call last):
File "<cattrs generated structure __main__.Foo>", line 16, in structure_Foo
return __cl(
**res,
)
File "<attrs generated methods __main__.Foo>", line 28, in __init__
__attr_validator_a(self, __attr_a, self.a)
~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
File ".venv/lib/python3.13/site-packages/attr/_make.py", line 3279, in __call__
v(inst, attr, value)
~^^^^^^^^^^^^^^^^^^^
File ".venv/lib/python3.13/site-packages/attr/validators.py", line 575, in __call__
raise ValueError(msg)
ValueError: Length of 'a' must be >= 3: 1
During handling of the above exception, another exception occurred:
+ Exception Group Traceback (most recent call last):
| File "/home/lincoln/.config/JetBrains/PyCharm2025.2/scratches/scratch_2.py", line 12, in <module>
| cattrs.structure(
| ~~~~~~~~~~~~~~~~^
| {"a": "a", "b": "b"},
| ^^^^^^^^^^^^^^^^^^^^^
| Foo,
| ^^^^
| )
| ^
| File ".venv/lib/python3.13/site-packages/cattrs/converters.py", line 589, in structure
| return self._structure_func.dispatch(cl)(obj, cl)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
| File "<cattrs generated structure __main__.Foo>", line 19, in structure_Foo
| except Exception as exc: raise __c_cve('While structuring ' + 'Foo', [exc], __cl)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| cattrs.errors.ClassValidationError: While structuring Foo (1 sub-exception)
+-+---------------- 1 ----------------
| Traceback (most recent call last):
| File "<cattrs generated structure __main__.Foo>", line 16, in structure_Foo
| return __cl(
| **res,
| )
| File "<attrs generated methods __main__.Foo>", line 28, in __init__
| __attr_validator_a(self, __attr_a, self.a)
| ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
| File ".venv/lib/python3.13/site-packages/attr/_make.py", line 3279, in __call__
| v(inst, attr, value)
| ~^^^^^^^^^^^^^^^^^^^
| File ".venv/lib/python3.13/site-packages/attr/validators.py", line 575, in __call__
| raise ValueError(msg)
| ValueError: Length of 'a' must be >= 3: 1
+------------------------------------
Plus when I pass that exception to cattrs.transform_exception the output list is
['invalid value @ $']
which seems lacking on detail of where the validation error happened.
I am expecting to see multiple because the docs say
When structuring a class, cattrs will gather any exceptions on a field-by-field basis and raise them as a cattrs.ClassValidationError, which is a subclass of BaseValidationError.
I am doing something wrong, or this not a feature of cattrs?
- 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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di python-attrs/cattrs
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
python-attrs/cattrs#761 · 2 commenti ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 64/100
python-attrs/cattrs#513 · 4 commenti ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 35/100
python-attrs/cattrs#779 ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 85/100
python-attrs/cattrs#774 ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
python-attrs/cattrs#746 · 2 commenti ·
Tutte le issue di python-attrs/cattrs
Issue simili
-
area: harness bug status: needs-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
Human-Agent-Society/reef#625 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 80/100
learningequality/kolibri#15351 · 2 commenti ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
Name consistency Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
eellak/triplestore#65 · 1 commento ·