Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Validation errors from multiple fields are not in ClassValidationError

未关闭
#694 7 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
python
领域
backend

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

validation

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?

主要语言
Python
星标
1.1k
派生
171
平均合并
14 小时 46 分钟
30 天内合并 PR
5

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python-attrs/cattrs 的其他 Issue

查看 python-attrs/cattrs 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。