1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
|
"""Test both structuring and unstructuring."""
from collections import deque
from typing import (
Annotated,
Any,
Deque,
FrozenSet,
List,
MutableSequence,
Optional,
Sequence,
Set,
Tuple,
Type,
Union,
)
import pytest
from attrs import Factory, define, field, fields, has, make_class
from hypothesis import assume, given
from hypothesis.strategies import booleans, just, lists, one_of, sampled_from
from cattrs import BaseConverter, Converter, UnstructureStrategy
from cattrs.dispatch import StructureHook, UnstructureHook
from cattrs.errors import (
ClassValidationError,
ForbiddenExtraKeysError,
StructureHandlerNotFoundError,
)
from cattrs.fns import raise_error
from cattrs.gen import make_dict_structure_fn, override
from ._compat import is_py310_plus
from .helpers import assert_only_unstructured
from .typed import (
nested_typed_classes,
simple_typed_attrs,
simple_typed_classes,
simple_typed_dataclasses,
)
unstructure_strats = one_of(just(s) for s in UnstructureStrategy)
@given(
simple_typed_classes(allow_nan=False) | simple_typed_dataclasses(allow_nan=False),
booleans(),
)
def test_simple_roundtrip(cls_and_vals, detailed_validation):
"""
Simple classes with metadata can be unstructured and restructured.
"""
converter = Converter(detailed_validation=detailed_validation)
cl, vals, kwargs = cls_and_vals
inst = cl(*vals, **kwargs)
unstructured = converter.unstructure(inst)
assert_only_unstructured(unstructured)
assert inst == converter.structure(unstructured, cl)
@given(
simple_typed_classes(kw_only="never", newtypes=False, allow_nan=False)
| simple_typed_dataclasses(newtypes=False, allow_nan=False),
booleans(),
)
def test_simple_roundtrip_tuple(cls_and_vals, dv: bool):
"""
Simple classes with metadata can be unstructured and restructured.
"""
converter = Converter(
unstruct_strat=UnstructureStrategy.AS_TUPLE, detailed_validation=dv
)
cl, vals, _ = cls_and_vals
inst = cl(*vals)
unstructured = converter.unstructure(inst)
assert_only_unstructured(unstructured)
assert inst == converter.structure(unstructured, cl)
@given(simple_typed_attrs(defaults="always", allow_nan=False))
def test_simple_roundtrip_defaults(attr_and_vals):
"""
Simple classes with metadata can be unstructured and restructured.
"""
a, _ = attr_and_vals
cl = make_class("HypClass", {"a": a})
converter = Converter()
inst = cl()
assert converter.unstructure(converter.structure({}, cl)) == converter.unstructure(
inst
)
assert inst == converter.structure(converter.unstructure(inst), cl)
@given(
simple_typed_attrs(
defaults="always", kw_only="never", newtypes=False, allow_nan=False
)
)
def test_simple_roundtrip_defaults_tuple(attr_and_vals):
"""
Simple classes with metadata can be unstructured and restructured.
"""
a, _ = attr_and_vals
cl = make_class("HypClass", {"a": a})
converter = Converter(unstruct_strat=UnstructureStrategy.AS_TUPLE)
inst = cl()
assert converter.unstructure(converter.structure({}, cl)) == converter.unstructure(
inst
)
assert inst == converter.structure(converter.unstructure(inst), cl)
@given(
simple_typed_classes(newtypes=False, allow_nan=False)
| simple_typed_dataclasses(newtypes=False, allow_nan=False),
unstructure_strats,
)
def test_simple_roundtrip_with_extra_keys_forbidden(cls_and_vals, strat):
"""
Simple classes can be unstructured and restructured with forbid_extra_keys=True.
"""
converter = Converter(unstruct_strat=strat, forbid_extra_keys=True)
cl, vals, kwargs = cls_and_vals
assume(strat is UnstructureStrategy.AS_DICT or not kwargs)
inst = cl(*vals, **kwargs)
unstructured = converter.unstructure(inst)
assert_only_unstructured(unstructured)
assert inst == converter.structure(unstructured, cl)
@given(simple_typed_classes() | simple_typed_dataclasses())
def test_forbid_extra_keys(cls_and_vals):
"""
Restructuring fails when extra keys are present (when configured)
"""
converter = Converter(forbid_extra_keys=True)
cl, vals, kwargs = cls_and_vals
inst = cl(*vals, **kwargs)
unstructured = converter.unstructure(inst)
bad_key = next(iter(unstructured)) + "_" if unstructured else "Hyp"
unstructured[bad_key] = 1
with pytest.raises(ClassValidationError) as cve:
converter.structure(unstructured, cl)
assert len(cve.value.exceptions) == 1
assert isinstance(cve.value.exceptions[0], ForbiddenExtraKeysError)
assert cve.value.exceptions[0].cl is cl
assert cve.value.exceptions[0].extra_fields == {bad_key}
@pytest.mark.parametrize("use_alias", [True, False])
def test_use_alias(use_alias):
"""
(Un)structuring with use_alias=True generates/uses aliased keys.
"""
@define
class C:
a: int = field(default=0, alias="_alias")
b: int = field(default=0)
inst = C()
converter = Converter(use_alias=use_alias)
unstructured = converter.unstructure(inst)
for fld in fields(C):
if use_alias:
assert fld.alias in unstructured
if fld.name != fld.alias:
assert fld.name not in unstructured
else:
assert fld.name in unstructured
if fld.name != fld.alias:
assert fld.alias not in unstructured
converter.structure(unstructured, C)
@given(simple_typed_attrs(defaults="always"))
def test_forbid_extra_keys_defaults(attr_and_vals):
"""
Restructuring fails when a dict key is renamed (if forbid_extra_keys set)
"""
a, _ = attr_and_vals
cl = make_class("HypClass", {"a": a})
converter = Converter(forbid_extra_keys=True)
inst = cl()
unstructured = converter.unstructure(inst)
unstructured["aa"] = unstructured.pop("a")
with pytest.raises(ClassValidationError) as cve:
converter.structure(unstructured, cl)
assert len(cve.value.exceptions) == 1
assert isinstance(cve.value.exceptions[0], ForbiddenExtraKeysError)
assert cve.value.exceptions[0].cl is cl
assert cve.value.exceptions[0].extra_fields == {"aa"}
def test_forbid_extra_keys_nested_override():
@define
class C:
a: int = 1
@define
class A:
c: C
a: int = 2
converter = Converter(forbid_extra_keys=True)
unstructured = {"a": 3, "c": {"a": 4}}
# at this point, structuring should still work
converter.structure(unstructured, A)
# if we break it in the subclass, we need it to raise
unstructured["c"]["aa"] = 5
with pytest.raises(ClassValidationError) as cve:
converter.structure(unstructured, A)
assert len(cve.value.exceptions) == 1
assert isinstance(cve.value.exceptions[0], ClassValidationError)
assert len(cve.value.exceptions[0].exceptions) == 1
assert isinstance(cve.value.exceptions[0].exceptions[0], ForbiddenExtraKeysError)
assert cve.value.exceptions[0].exceptions[0].cl is C
assert cve.value.exceptions[0].exceptions[0].extra_fields == {"aa"}
# we can "fix" that by disabling forbid_extra_keys on the subclass
hook = make_dict_structure_fn(C, converter, _cattrs_forbid_extra_keys=False)
converter.register_structure_hook(C, hook)
converter.structure(unstructured, A)
# but we should still raise at the top level
unstructured["b"] = 6
with pytest.raises(ClassValidationError) as cve:
converter.structure(unstructured, A)
assert len(cve.value.exceptions) == 1
assert isinstance(cve.value.exceptions[0], ForbiddenExtraKeysError)
assert cve.value.exceptions[0].cl is A
assert cve.value.exceptions[0].extra_fields == {"b"}
@given(
nested_typed_classes(defaults="always", min_attrs=1, allow_nan=False), booleans()
)
def test_nested_roundtrip(cls_and_vals, omit_if_default):
"""
Nested classes with metadata can be unstructured and restructured.
"""
converter = Converter(omit_if_default=omit_if_default)
cl, vals, kwargs = cls_and_vals
# Vals are a tuple, convert into a dictionary.
inst = cl(*vals, **kwargs)
unstructured = converter.unstructure(inst)
assert inst == converter.structure(unstructured, cl)
@given(
nested_typed_classes(
defaults="always", min_attrs=1, kw_only="never", newtypes=False, allow_nan=False
),
booleans(),
)
def test_nested_roundtrip_tuple(cls_and_vals, omit_if_default: bool):
"""
Nested classes with metadata can be unstructured and restructured.
"""
converter = Converter(
unstruct_strat=UnstructureStrategy.AS_TUPLE, omit_if_default=omit_if_default
)
cl, vals, _ = cls_and_vals
# Vals are a tuple, convert into a dictionary.
inst = cl(*vals)
unstructured = converter.unstructure(inst)
assert inst == converter.structure(unstructured, cl)
@given(simple_typed_classes(defaults=False, allow_nan=False))
def test_optional_field_roundtrip(cl_and_vals):
"""
Classes with optional fields can be unstructured and structured.
"""
converter = Converter()
cl, vals, kwargs = cl_and_vals
@define
class C:
a: Optional[cl]
inst = C(a=cl(*vals, **kwargs))
assert inst == converter.structure(converter.unstructure(inst), C)
inst = C(a=None)
unstructured = converter.unstructure(inst)
assert inst == converter.structure(unstructured, C)
@pytest.mark.skipif(not is_py310_plus, reason="3.10+ union syntax")
@given(simple_typed_classes(defaults="never", allow_nan=False))
def test_310_optional_field_roundtrip(cl_and_vals):
"""
Classes with optional fields can be unstructured and structured.
"""
converter = Converter()
cl, vals, kwargs = cl_and_vals
@define
class C:
a: cl | None
inst = C(a=cl(*vals, **kwargs))
assert inst == converter.structure(converter.unstructure(inst), C)
inst = C(a=None)
unstructured = converter.unstructure(inst)
assert inst == converter.structure(unstructured, C)
@given(simple_typed_classes(defaults="always", allow_nan=False))
def test_omit_default_roundtrip(cl_and_vals):
"""
Omit default on the converter works.
"""
converter = Converter(omit_if_default=True)
cl, vals, kwargs = cl_and_vals
@define
class C:
a: int = 1
b: cl = Factory(lambda: cl(*vals, **kwargs))
inst = C()
unstructured = converter.unstructure(inst)
assert unstructured == {}
assert inst == converter.structure(unstructured, C)
inst = C(0)
unstructured = converter.unstructure(inst)
assert unstructured == {"a": 0}
assert inst == converter.structure(unstructured, C)
def test_dict_roundtrip_with_alias():
"""
A class with an aliased attribute can be unstructured and structured.
"""
converter = BaseConverter()
@define
class C:
_a: int
inst = C(a=0)
unstructured = converter.unstructure(inst)
assert unstructured == {"_a": 0}
assert converter.structure(unstructured, C) == inst
@given(simple_typed_classes(defaults="always"))
def test_type_overrides(cl_and_vals):
"""
Type overrides on the GenConverter work.
"""
converter = Converter(type_overrides={int: override(omit_if_default=True)})
cl, vals, kwargs = cl_and_vals
inst = cl(*vals, **kwargs)
unstructured = converter.unstructure(inst)
for attr, val in zip(fields(cl), vals):
if attr.type is int and attr.default is not None and attr.default == val:
assert attr.name not in unstructured
def test_calling_back():
"""
Calling unstructure_attrs_asdict from a hook should not override a manual hook.
"""
converter = Converter()
@define
class C:
a: int = 1
def handler(obj):
return {
"type_tag": obj.__class__.__name__,
**converter.unstructure_attrs_asdict(obj),
}
converter.register_unstructure_hook(C, handler)
inst = C()
expected = {"type_tag": "C", "a": 1}
unstructured1 = converter.unstructure(inst)
unstructured2 = converter.unstructure(inst)
assert unstructured1 == expected, repr(unstructured1)
assert unstructured2 == unstructured1, repr(unstructured2)
def test_overriding_generated_unstructure():
"""Test overriding a generated unstructure hook works."""
converter = Converter()
@define
class Inner:
a: int
@define
class Outer:
i: Inner
inst = Outer(Inner(1))
converter.unstructure(inst)
converter.register_unstructure_hook(Inner, lambda _: {"a": 2})
r = converter.structure(converter.unstructure(inst), Outer)
assert r.i.a == 2
def test_overriding_generated_unstructure_hook_func():
"""Test overriding a generated unstructure hook works."""
converter = Converter()
@define
class Inner:
a: int
@define
class Outer:
i: Inner
inst = Outer(Inner(1))
converter.unstructure(inst)
converter.register_unstructure_hook_func(lambda t: t is Inner, lambda _: {"a": 2})
r = converter.structure(converter.unstructure(inst), Outer)
assert r.i.a == 2
def test_overriding_generated_structure():
"""Test overriding a generated structure hook works."""
converter = Converter()
@define
class Inner:
a: int
@define
class Outer:
i: Inner
inst = Outer(Inner(1))
raw = converter.unstructure(inst)
converter.structure(raw, Outer)
converter.register_structure_hook(Inner, lambda p, _: Inner(p["a"] + 1))
r = converter.structure(raw, Outer)
assert r.i.a == 2
def test_overriding_generated_structure_hook_func():
"""Test overriding a generated structure hook works."""
converter = Converter()
@define
class Inner:
a: int
@define
class Outer:
i: Inner
inst = Outer(Inner(1))
raw = converter.unstructure(inst)
converter.structure(raw, Outer)
converter.register_structure_hook_func(
lambda t: t is Inner, lambda p, _: Inner(p["a"] + 1)
)
r = converter.structure(raw, Outer)
assert r.i.a == 2
@given(
lists(simple_typed_classes(frozen=True), min_size=1),
sampled_from(
[
(tuple, Tuple),
(tuple, tuple),
(list, list),
(list, List),
(deque, Deque),
(set, Set),
(set, set),
(frozenset, frozenset),
(frozenset, FrozenSet),
(list, MutableSequence),
(deque, MutableSequence),
(tuple, Sequence),
]
),
)
def test_seq_of_simple_classes_unstructure(cls_and_vals, seq_type_and_annotation):
"""Dumping a sequence of primitives is a simple copy operation."""
converter = Converter()
test_val = ("test", 1)
for cl, _, _ in cls_and_vals:
converter.register_unstructure_hook(cl, lambda _: test_val)
break # Just register the first class.
seq_type, annotation = seq_type_and_annotation
inputs = seq_type(cl(*vals, **kwargs) for cl, vals, kwargs in cls_and_vals)
outputs = converter.unstructure(
inputs,
unstructure_as=(
annotation[cl] if annotation not in (Tuple, tuple) else annotation[cl, ...]
),
)
assert all(e == test_val for e in outputs)
@given(
sampled_from(
[
(tuple, Tuple),
(tuple, tuple),
(list, list),
(list, List),
(deque, deque),
(deque, Deque),
(set, Set),
(set, set),
(frozenset, frozenset),
(frozenset, FrozenSet),
]
)
)
def test_seq_of_bare_classes_structure(seq_type_and_annotation):
"""Structure iterable of values to a sequence of primitives."""
converter = Converter()
bare_classes = ((int, (1,)), (float, (1.0,)), (str, ("test",)), (bool, (True,)))
seq_type, annotation = seq_type_and_annotation
for cl, vals in bare_classes:
@define(frozen=True)
class C:
a: cl
b: cl
inputs = [{"a": cl(*vals), "b": cl(*vals)} for _ in range(5)]
outputs = converter.structure(
inputs,
cl=(
annotation[C]
if annotation not in (Tuple, tuple)
else annotation[C, ...]
),
)
expected = seq_type(C(a=cl(*vals), b=cl(*vals)) for _ in range(5))
assert type(outputs) is seq_type
assert outputs == expected
def test_annotated_attrs():
"""Annotation support works for attrs classes."""
converter = Converter()
@define
class Inner:
a: int
@define
class Outer:
i: Annotated[Inner, "test"]
j: list[Annotated[Inner, "test"]]
orig = Outer(Inner(1), [Inner(1)])
raw = converter.unstructure(orig)
assert raw == {"i": {"a": 1}, "j": [{"a": 1}]}
structured = converter.structure(raw, Outer)
assert structured == orig
# Now register a hook and rerun the test.
converter.register_unstructure_hook(Inner, lambda v: {"a": 2})
raw = converter.unstructure(Outer(Inner(1), [Inner(1)]))
assert raw == {"i": {"a": 2}, "j": [{"a": 2}]}
structured = converter.structure(raw, Outer)
assert structured == Outer(Inner(2), [Inner(2)])
def test_annotated_with_typing_extensions_attrs():
"""Annotation support works for attrs classes."""
converter = Converter()
@define
class Inner:
a: int
@define
class Outer:
i: Annotated[Inner, "test"]
j: List[Annotated[Inner, "test"]]
k: Annotated[Union[Inner, None], "test"]
orig = Outer(Inner(1), [Inner(1)], Inner(1))
raw = converter.unstructure(orig)
assert raw == {"i": {"a": 1}, "j": [{"a": 1}], "k": {"a": 1}}
structured = converter.structure(raw, Outer)
assert structured == orig
# Now register a hook and rerun the test.
converter.register_unstructure_hook(Inner, lambda _: {"a": 2})
raw = converter.unstructure(Outer(Inner(1), [Inner(1)], Inner(1)))
assert raw == {"i": {"a": 2}, "j": [{"a": 2}], "k": {"a": 2}}
structured = converter.structure(raw, Outer)
assert structured == Outer(Inner(2), [Inner(2)], Inner(2))
def test_default_structure_fallback(converter_cls: Type[BaseConverter]):
"""The default structure fallback hook factory eagerly errors."""
class Test:
"""Unsupported by default."""
c = converter_cls()
with pytest.raises(StructureHandlerNotFoundError):
c.get_structure_hook(Test)
def test_unstructure_fallbacks(converter_cls: Type[BaseConverter]):
"""Unstructure fallback factories work."""
class Test:
"""Unsupported by default."""
c = converter_cls()
assert isinstance(c.unstructure(Test()), Test)
c = converter_cls(
unstructure_fallback_factory=lambda _: lambda v: v.__class__.__name__
)
assert c.unstructure(Test()) == "Test"
def test_structure_fallbacks(converter_cls: Type[BaseConverter]):
"""Structure fallback factories work."""
class Test:
"""Unsupported by default."""
c = converter_cls()
with pytest.raises(StructureHandlerNotFoundError):
c.structure({}, Test)
c = converter_cls(structure_fallback_factory=lambda _: lambda v, _: Test())
assert isinstance(c.structure({}, Test), Test)
def test_legacy_structure_fallbacks(converter_cls: Type[BaseConverter]):
"""Restoring legacy behavior works."""
class Test:
"""Unsupported by default."""
def __init__(self, a):
self.a = a
c = converter_cls(
structure_fallback_factory=lambda _: raise_error, detailed_validation=False
)
# We can get the hook, but...
hook = c.get_structure_hook(Test)
# it won't work.
with pytest.raises(StructureHandlerNotFoundError):
hook({}, Test)
# If a field has a converter, we honor that instead.
@define
class Container:
a: Test = field(converter=Test)
hook = c.get_structure_hook(Container)
hook({"a": 1}, Container)
def test_fallback_chaining(converter_cls: Type[BaseConverter]):
"""Converters can be chained using fallback hooks."""
class Test:
"""Unsupported by default."""
parent = converter_cls()
parent.register_unstructure_hook(Test, lambda _: "Test")
parent.register_structure_hook(Test, lambda v, _: Test())
# No chaining first.
child = converter_cls()
with pytest.raises(StructureHandlerNotFoundError):
child.structure(child.unstructure(Test()), Test)
child = converter_cls(
unstructure_fallback_factory=parent.get_unstructure_hook,
structure_fallback_factory=parent.get_structure_hook,
)
assert isinstance(child.structure(child.unstructure(Test()), Test), Test)
def test_hook_getting(converter: BaseConverter):
"""Converters can produce their hooks."""
@define
class Test:
a: int
hook = converter.get_unstructure_hook(Test)
assert hook(Test(1)) == {"a": 1}
structure = converter.get_structure_hook(Test)
assert structure({"a": 1}, Test) == Test(1)
def test_decorators(converter: BaseConverter):
"""The decorator versions work."""
@define
class Test:
a: int
@converter.register_unstructure_hook
def my_hook(value: Test) -> dict:
res = {"a": value.a}
res["a"] += 1
return res
assert converter.unstructure(Test(1)) == {"a": 2}
@converter.register_structure_hook
def my_structure_hook(value, _) -> Test:
value["a"] += 1
return Test(**value)
assert converter.structure({"a": 5}, Test) == Test(6)
def test_hook_factory_decorators(converter: BaseConverter):
"""Hook factory decorators work."""
@define
class Test:
a: int
@converter.register_unstructure_hook_factory(has)
def my_hook_factory(type: Any) -> UnstructureHook:
return lambda v: v.a
assert converter.unstructure(Test(1)) == 1
@converter.register_structure_hook_factory(has)
def my_structure_hook_factory(type: Any) -> StructureHook:
return lambda v, _: Test(v)
assert converter.structure(1, Test) == Test(1)
def test_hook_factory_decorators_with_converters(converter: BaseConverter):
"""Hook factory decorators with converters work."""
@define
class Test:
a: int
converter.register_unstructure_hook(int, lambda v: v + 1)
@converter.register_unstructure_hook_factory(has)
def my_hook_factory(type: Any, converter: BaseConverter) -> UnstructureHook:
int_handler = converter.get_unstructure_hook(int)
return lambda v: (int_handler(v.a),)
assert converter.unstructure(Test(1)) == (2,)
converter.register_structure_hook(int, lambda v: v - 1)
@converter.register_structure_hook_factory(has)
def my_structure_hook_factory(type: Any, converter: BaseConverter) -> StructureHook:
int_handler = converter.get_structure_hook(int)
return lambda v, _: Test(int_handler(v[0]))
assert converter.structure((2,), Test) == Test(1)
def test_hook_factories_with_converters(converter: BaseConverter):
"""Hook factories with converters work."""
@define
class Test:
a: int
converter.register_unstructure_hook(int, lambda v: v + 1)
def my_hook_factory(type: Any, converter: BaseConverter) -> UnstructureHook:
int_handler = converter.get_unstructure_hook(int)
return lambda v: (int_handler(v.a),)
converter.register_unstructure_hook_factory(has, my_hook_factory)
assert converter.unstructure(Test(1)) == (2,)
converter.register_structure_hook(int, lambda v: v - 1)
def my_structure_hook_factory(type: Any, converter: BaseConverter) -> StructureHook:
int_handler = converter.get_structure_hook(int)
return lambda v, _: Test(int_handler(v[0]))
converter.register_structure_hook_factory(has, my_structure_hook_factory)
assert converter.structure((2,), Test) == Test(1)
def test_hook_factories_with_converter_methods(converter: BaseConverter):
"""What if the hook factories are methods (have `self`)?"""
@define
class Test:
a: int
converter.register_unstructure_hook(int, lambda v: v + 1)
class Converters:
@classmethod
def my_hook_factory(
cls, type: Any, converter: BaseConverter
) -> UnstructureHook:
int_handler = converter.get_unstructure_hook(int)
return lambda v: (int_handler(v.a),)
def my_structure_hook_factory(
self, type: Any, converter: BaseConverter
) -> StructureHook:
int_handler = converter.get_structure_hook(int)
return lambda v, _: Test(int_handler(v[0]))
converter.register_unstructure_hook_factory(has, Converters.my_hook_factory)
assert converter.unstructure(Test(1)) == (2,)
converter.register_structure_hook(int, lambda v: v - 1)
converter.register_structure_hook_factory(
has, Converters().my_structure_hook_factory
)
assert converter.structure((2,), Test) == Test(1)
|