Skip to content

Commit 93d83f8

Browse files
authored
Added format_exception to recursive calls of transform_error. [#389] (#390)
1 parent 47628ec commit 93d83f8

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

HISTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## 23.2.0 (UNRELEASED)
44

5+
- Fix `format_exception` parameter working for recursive calls to `transform_error`
6+
([#389](https://github.com/python-attrs/cattrs/issues/389)
57
- Use [PDM](https://pdm.fming.dev/latest/) instead of Poetry.
68
- _cattrs_ is now linted with [Ruff](https://beta.ruff.rs/docs/).
79
- Fix TypedDicts with periods in their field names.

src/cattrs/v.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def transform_error(
8989
for exc, note in with_notes:
9090
p = f"{path}[{note.index!r}]"
9191
if isinstance(exc, (ClassValidationError, IterableValidationError)):
92-
errors.extend(transform_error(exc, p))
92+
errors.extend(transform_error(exc, p, format_exception))
9393
else:
9494
errors.append(f"{format_exception(exc, note.type)} @ {p}")
9595
for exc in without:
@@ -99,7 +99,7 @@ def transform_error(
9999
for exc, note in with_notes:
100100
p = f"{path}.{note.name}"
101101
if isinstance(exc, (ClassValidationError, IterableValidationError)):
102-
errors.extend(transform_error(exc, p))
102+
errors.extend(transform_error(exc, p, format_exception))
103103
else:
104104
errors.append(f"{format_exception(exc, note.type)} @ {p}")
105105
for exc in without:

tests/test_v.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,25 @@ class C:
227227
]
228228

229229

230+
def test_custom_error_fn_nested(c: Converter) -> None:
231+
def my_format(exc, type):
232+
if isinstance(exc, TypeError):
233+
return "Must be correct type"
234+
return format_exception(exc, type)
235+
236+
@define
237+
class C:
238+
a: Dict[str, int]
239+
240+
try:
241+
c.structure({"a": {"a": "str", "b": 1, "c": None}}, C)
242+
except Exception as exc:
243+
assert transform_error(exc, format_exception=my_format) == [
244+
"invalid value for type, expected int @ $.a['a']",
245+
"Must be correct type @ $.a['c']",
246+
]
247+
248+
230249
def test_typeddict_attribute_errors(c: Converter) -> None:
231250
"""TypedDict errors are correctly generated."""
232251

0 commit comments

Comments
 (0)