Skip to content

Commit

Permalink
More coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinche committed Oct 19, 2024
1 parent 191dd33 commit 8621cd3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
4 changes: 3 additions & 1 deletion tests/test_gen_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,18 +336,20 @@ def test_overriding_struct_hook(converter: BaseConverter) -> None:
class A:
a: int
b: str
c: int = 0

converter.register_structure_hook(
A,
make_dict_structure_fn(
A,
converter,
a=override(struct_hook=lambda v, _: ceil(v)),
c=override(struct_hook=lambda v, _: ceil(v)),
_cattrs_detailed_validation=converter.detailed_validation,
),
)

assert converter.structure({"a": 0.5, "b": 1}, A) == A(1, "1")
assert converter.structure({"a": 0.5, "b": 1, "c": 0.5}, A) == A(1, "1", 1)


def test_overriding_unstruct_hook(converter: BaseConverter) -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_gen_dict_563.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from dataclasses import dataclass

from attr import define
from attrs import define

from cattrs import Converter
from cattrs.gen import make_dict_structure_fn, make_dict_unstructure_fn
Expand Down
22 changes: 12 additions & 10 deletions tests/test_generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

T = TypeVar("T")
T2 = TypeVar("T2")
T3 = TypeVar("T3", bound=int)


def test_deep_copy():
Expand All @@ -31,9 +32,10 @@ def test_deep_copy():


@define
class TClass(Generic[T, T2]):
class TClass(Generic[T, T2, T3]):
a: T
b: T2
c: T3 = 0


@define
Expand All @@ -44,15 +46,15 @@ class GenericCols(Generic[T]):


@pytest.mark.parametrize(
("t", "t2", "result"),
("t", "t2", "t3", "result"),
(
(int, str, TClass(1, "a")),
(str, str, TClass("1", "a")),
(List[int], str, TClass([1, 2, 3], "a")),
(int, str, int, TClass(1, "a")),
(str, str, int, TClass("1", "a")),
(List[int], str, int, TClass([1, 2, 3], "a")),
),
)
def test_able_to_structure_generics(converter: BaseConverter, t, t2, result):
res = converter.structure(asdict(result), TClass[t, t2])
def test_able_to_structure_generics(converter: BaseConverter, t, t2, t3, result):
res = converter.structure(asdict(result), TClass[t, t2, t3])

assert res == result

Expand Down Expand Up @@ -105,8 +107,8 @@ class GenericCols(Generic[T]):
@pytest.mark.parametrize(
("t", "t2", "result"),
(
(TClass[int, int], str, TClass(TClass(1, 2), "a")),
(List[TClass[int, int]], str, TClass([TClass(1, 2)], "a")),
(TClass[int, int, int], str, TClass(TClass(1, 2), "a")),
(List[TClass[int, int, int]], str, TClass([TClass(1, 2)], "a")),
),
)
def test_structure_nested_generics(converter: BaseConverter, t, t2, result):
Expand All @@ -116,7 +118,7 @@ def test_structure_nested_generics(converter: BaseConverter, t, t2, result):


def test_able_to_structure_deeply_nested_generics_gen(converter):
cl = TClass[TClass[TClass[int, int], int], int]
cl = TClass[TClass[TClass[int, int, int], int, int], int, int]
result = TClass(TClass(TClass(1, 2), 3), 4)

res = converter.structure(asdict(result), cl)
Expand Down

0 comments on commit 8621cd3

Please sign in to comment.