Skip to content

Commit

Permalink
Added format_exception to recursive calls of transform_error. [#389] (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
BEllis committed Jun 21, 2023
1 parent 47628ec commit 93d83f8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 23.2.0 (UNRELEASED)

- Fix `format_exception` parameter working for recursive calls to `transform_error`
([#389](https://github.com/python-attrs/cattrs/issues/389)
- Use [PDM](https://pdm.fming.dev/latest/) instead of Poetry.
- _cattrs_ is now linted with [Ruff](https://beta.ruff.rs/docs/).
- Fix TypedDicts with periods in their field names.
Expand Down
4 changes: 2 additions & 2 deletions src/cattrs/v.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def transform_error(
for exc, note in with_notes:
p = f"{path}[{note.index!r}]"
if isinstance(exc, (ClassValidationError, IterableValidationError)):
errors.extend(transform_error(exc, p))
errors.extend(transform_error(exc, p, format_exception))
else:
errors.append(f"{format_exception(exc, note.type)} @ {p}")
for exc in without:
Expand All @@ -99,7 +99,7 @@ def transform_error(
for exc, note in with_notes:
p = f"{path}.{note.name}"
if isinstance(exc, (ClassValidationError, IterableValidationError)):
errors.extend(transform_error(exc, p))
errors.extend(transform_error(exc, p, format_exception))
else:
errors.append(f"{format_exception(exc, note.type)} @ {p}")
for exc in without:
Expand Down
19 changes: 19 additions & 0 deletions tests/test_v.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,25 @@ class C:
]


def test_custom_error_fn_nested(c: Converter) -> None:
def my_format(exc, type):
if isinstance(exc, TypeError):
return "Must be correct type"
return format_exception(exc, type)

@define
class C:
a: Dict[str, int]

try:
c.structure({"a": {"a": "str", "b": 1, "c": None}}, C)
except Exception as exc:
assert transform_error(exc, format_exception=my_format) == [
"invalid value for type, expected int @ $.a['a']",
"Must be correct type @ $.a['c']",
]


def test_typeddict_attribute_errors(c: Converter) -> None:
"""TypedDict errors are correctly generated."""

Expand Down

0 comments on commit 93d83f8

Please sign in to comment.