Skip to content

Commit fca1f95

Browse files
bysiberTinche
authored andcommitted
Add regression test and changelog for namedtuple detailed_validation fix
Add a test that verifies the detailed_validation parameter passed to namedtuple_dict_structure_factory is actually used and not silently ignored. Also add a HISTORY.md entry for the fix.
1 parent 19b678f commit fca1f95

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

HISTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Our backwards-compatibility policy can be found [here](https://github.com/python
1515

1616
- Fix an `AttributeError` in `cattrs` internals that could be triggered by using the `include_subclasses` strategy in a `structure_hook_factory`
1717
([#721](https://github.com/python-attrs/cattrs/issues/721), [#722](https://github.com/python-attrs/cattrs/pull/722))
18+
- Fix the `detailed_validation` parameter being passed under the wrong name in {func}`namedtuple_dict_structure_factory <cattrs.cols.namedtuple_dict_structure_factory>`, causing it to be silently ignored.
19+
([#723](https://github.com/python-attrs/cattrs/pull/723))
1820

1921
## 26.1.0 (2026-02-18)
2022

tests/test_tuples.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,31 @@ class Test(NamedTuple):
141141

142142
assert isinstance(exc, ForbiddenExtraKeysError)
143143
assert exc.extra_fields == {"b"}
144+
145+
146+
def test_dict_namedtuples_detailed_validation():
147+
"""Passing detailed_validation to namedtuple_dict_structure_factory works.
148+
149+
Regression test for the parameter being passed under the wrong name.
150+
"""
151+
152+
class Test(NamedTuple):
153+
a: int
154+
155+
# Create a converter that does NOT use detailed validation by default.
156+
c = Converter(detailed_validation=False)
157+
158+
# But explicitly enable it in the factory.
159+
c.register_structure_hook_factory(
160+
lambda t: t is Test,
161+
lambda t, conv: namedtuple_dict_structure_factory(
162+
t, conv, detailed_validation=True
163+
),
164+
)
165+
166+
# With detailed validation, structuring errors should be wrapped
167+
# in a ClassValidationError instead of being raised directly.
168+
from cattrs.errors import ClassValidationError
169+
170+
with raises(ClassValidationError):
171+
c.structure({"a": "not_an_int"}, Test)

0 commit comments

Comments
 (0)