Skip to content

Commit 304e370

Browse files
bysiberTinche
andauthored
Fix wrong parameter name in namedtuple_dict_structure_factory (#723)
* Fix wrong parameter name in namedtuple_dict_structure_factory The call to make_dict_structure_fn_from_attrs passes _cattrs_use_detailed_validation but the function expects _cattrs_detailed_validation. Because **kwargs catches the misnamed parameter as an attribute override, detailed_validation is silently ignored and always falls back to the converter default. * 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. * Fix namedtuple validation test --------- Co-authored-by: Tin Tvrtkovic <tinchester@gmail.com>
1 parent 573915b commit 304e370

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
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

src/cattrs/cols.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def namedtuple_dict_structure_factory(
277277
cl,
278278
converter,
279279
_cattrs_forbid_extra_keys=forbid_extra_keys,
280-
_cattrs_use_detailed_validation=detailed_validation,
280+
_cattrs_detailed_validation=detailed_validation,
281281
_cattrs_use_linecache=use_linecache,
282282
**kwargs,
283283
)

tests/test_tuples.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namedtuple_dict_unstructure_factory,
1212
)
1313
from cattrs.converters import Converter
14-
from cattrs.errors import ForbiddenExtraKeysError
14+
from cattrs.errors import ClassValidationError, ForbiddenExtraKeysError
1515

1616

1717
def test_simple_hetero_tuples(genconverter: Converter):
@@ -141,3 +141,28 @@ 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(t, conv, True),
162+
)
163+
164+
# With detailed validation, structuring errors should be wrapped
165+
# in a ClassValidationError instead of being raised directly.
166+
167+
with raises(ClassValidationError):
168+
c.structure({"a": "not_an_int"}, Test)

0 commit comments

Comments
 (0)