Skip to content

Commit

Permalink
More BaseValidationErrors tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinche committed Nov 24, 2024
1 parent 1be42eb commit 7010e32
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/cattrs/errors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from collections.abc import Sequence
from typing import Any, Optional, Union

from typing_extensions import Self

from cattrs._compat import ExceptionGroup


Expand All @@ -17,13 +20,13 @@ def __init__(self, message: str, type_: type) -> None:
class BaseValidationError(ExceptionGroup):
cl: type

def __new__(cls, message, excs, cl: type):
def __new__(cls, message: str, excs: Sequence[Exception], cl: type):
obj = super().__new__(cls, message, excs)
obj.cl = cl
return obj

def derive(self, excs):
return ClassValidationError(self.message, excs, self.cl)
def derive(self, excs: Sequence[Exception]) -> Self:
return self.__class__(self.message, excs, self.cl)


class IterableValidationNote(str):
Expand Down
22 changes: 22 additions & 0 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,25 @@ def test_notes_pickling():
assert note == "foo"
assert note.name == "name"
assert note.type is int


def test_error_derive():
"""Our ExceptionGroups should derive properly."""
c = Converter(detailed_validation=True)

@define
class Test:
a: int
b: str = field(validator=in_(["a", "b"]))
c: str

with pytest.raises(ClassValidationError) as exc:
c.structure({"a": "a", "b": "c"}, Test)

match, rest = exc.value.split(KeyError)

assert len(match.exceptions) == 1
assert len(rest.exceptions) == 1

assert match.cl == exc.value.cl
assert rest.cl == exc.value.cl

0 comments on commit 7010e32

Please sign in to comment.