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
|
# SPDX-License-Identifier: MIT
"""
Tests for `attr._funcs`.
"""
import re
from collections import OrderedDict
from typing import Generic, NamedTuple, TypeVar
import pytest
from hypothesis import assume, given
from hypothesis import strategies as st
import attr
from attr import asdict, assoc, astuple, evolve, fields, has
from attr._compat import Mapping, Sequence
from attr.exceptions import AttrsAttributeNotFoundError
from attr.validators import instance_of
from .strategies import nested_classes, simple_classes
MAPPING_TYPES = (dict, OrderedDict)
SEQUENCE_TYPES = (list, tuple)
@pytest.fixture(scope="session", name="C")
def _C():
"""
Return a simple but fully featured attrs class with an x and a y attribute.
"""
import attr
@attr.s
class C:
x = attr.ib()
y = attr.ib()
return C
class TestAsDict:
"""
Tests for `asdict`.
"""
@given(st.sampled_from(MAPPING_TYPES))
def test_shallow(self, C, dict_factory):
"""
Shallow asdict returns correct dict.
"""
assert {"x": 1, "y": 2} == asdict(
C(x=1, y=2), False, dict_factory=dict_factory
)
@given(st.sampled_from(MAPPING_TYPES))
def test_recurse(self, C, dict_class):
"""
Deep asdict returns correct dict.
"""
assert {"x": {"x": 1, "y": 2}, "y": {"x": 3, "y": 4}} == asdict(
C(C(1, 2), C(3, 4)), dict_factory=dict_class
)
def test_nested_lists(self, C):
"""
Test unstructuring deeply nested lists.
"""
inner = C(1, 2)
outer = C([[inner]], None)
assert {"x": [[{"x": 1, "y": 2}]], "y": None} == asdict(outer)
def test_nested_dicts(self, C):
"""
Test unstructuring deeply nested dictionaries.
"""
inner = C(1, 2)
outer = C({1: {2: inner}}, None)
assert {"x": {1: {2: {"x": 1, "y": 2}}}, "y": None} == asdict(outer)
@given(nested_classes, st.sampled_from(MAPPING_TYPES))
def test_recurse_property(self, cls, dict_class):
"""
Property tests for recursive asdict.
"""
obj = cls()
obj_dict = asdict(obj, dict_factory=dict_class)
def assert_proper_dict_class(obj, obj_dict):
assert isinstance(obj_dict, dict_class)
for field in fields(obj.__class__):
field_val = getattr(obj, field.name)
if has(field_val.__class__):
# This field holds a class, recurse the assertions.
assert_proper_dict_class(field_val, obj_dict[field.name])
elif isinstance(field_val, Sequence):
dict_val = obj_dict[field.name]
for item, item_dict in zip(field_val, dict_val):
if has(item.__class__):
assert_proper_dict_class(item, item_dict)
elif isinstance(field_val, Mapping):
# This field holds a dictionary.
assert isinstance(obj_dict[field.name], dict_class)
for key, val in field_val.items():
if has(val.__class__):
assert_proper_dict_class(
val, obj_dict[field.name][key]
)
assert_proper_dict_class(obj, obj_dict)
@given(st.sampled_from(MAPPING_TYPES))
def test_filter(self, C, dict_factory):
"""
Attributes that are supposed to be skipped are skipped.
"""
assert {"x": {"x": 1}} == asdict(
C(C(1, 2), C(3, 4)),
filter=lambda a, v: a.name != "y",
dict_factory=dict_factory,
)
@given(container=st.sampled_from(SEQUENCE_TYPES))
def test_lists_tuples(self, container, C):
"""
If recurse is True, also recurse into lists.
"""
assert {
"x": 1,
"y": [{"x": 2, "y": 3}, {"x": 4, "y": 5}, "a"],
} == asdict(C(1, container([C(2, 3), C(4, 5), "a"])))
@given(container=st.sampled_from(SEQUENCE_TYPES))
def test_lists_tuples_retain_type(self, container, C):
"""
If recurse and retain_collection_types are True, also recurse
into lists and do not convert them into list.
"""
assert {
"x": 1,
"y": container([{"x": 2, "y": 3}, {"x": 4, "y": 5}, "a"]),
} == asdict(
C(1, container([C(2, 3), C(4, 5), "a"])),
retain_collection_types=True,
)
@given(set_type=st.sampled_from((set, frozenset)))
def test_sets_no_retain(self, C, set_type):
"""
Set types are converted to lists if retain_collection_types=False.
"""
d = asdict(
C(1, set_type((1, 2, 3))),
retain_collection_types=False,
recurse=True,
)
assert {"x": 1, "y": [1, 2, 3]} == d
@given(st.sampled_from(MAPPING_TYPES))
def test_dicts(self, C, dict_factory):
"""
If recurse is True, also recurse into dicts.
"""
res = asdict(C(1, {"a": C(4, 5)}), dict_factory=dict_factory)
assert {"x": 1, "y": {"a": {"x": 4, "y": 5}}} == res
assert isinstance(res, dict_factory)
@given(simple_classes(private_attrs=False), st.sampled_from(MAPPING_TYPES))
def test_roundtrip(self, cls, dict_class):
"""
Test dumping to dicts and back for Hypothesis-generated classes.
Private attributes don't round-trip (the attribute name is different
than the initializer argument).
"""
instance = cls()
dict_instance = asdict(instance, dict_factory=dict_class)
assert isinstance(dict_instance, dict_class)
roundtrip_instance = cls(**dict_instance)
assert instance == roundtrip_instance
@given(simple_classes())
def test_asdict_preserve_order(self, cls):
"""
Field order should be preserved when dumping to an ordered_dict.
"""
instance = cls()
dict_instance = asdict(instance, dict_factory=dict)
assert [a.name for a in fields(cls)] == list(dict_instance.keys())
def test_retain_keys_are_tuples(self):
"""
retain_collect_types also retains keys.
"""
@attr.s
class A:
a = attr.ib()
instance = A({(1,): 1})
assert {"a": {(1,): 1}} == attr.asdict(
instance, retain_collection_types=True
)
def test_tuple_keys(self):
"""
If a key is collection type, retain_collection_types is False,
the key is serialized as a tuple.
See #646
"""
@attr.s
class A:
a = attr.ib()
instance = A({(1,): 1})
assert {"a": {(1,): 1}} == attr.asdict(instance)
def test_named_tuple_retain_type(self):
"""
Namedtuples can be serialized if retain_collection_types is True.
See #1164
"""
class Coordinates(NamedTuple):
lat: float
lon: float
@attr.s
class A:
coords: Coordinates = attr.ib()
instance = A(Coordinates(50.419019, 30.516225))
assert {"coords": Coordinates(50.419019, 30.516225)} == attr.asdict(
instance, retain_collection_types=True
)
def test_type_error_with_retain_type(self):
"""
Serialization that fails with TypeError leaves the error through if
they're not tuples.
See #1164
"""
message = "__new__() missing 1 required positional argument (asdict)"
class Coordinates(list):
def __init__(self, first, *rest):
if isinstance(first, list):
raise TypeError(message)
super().__init__([first, *rest])
@attr.s
class A:
coords: Coordinates = attr.ib()
instance = A(Coordinates(50.419019, 30.516225))
with pytest.raises(TypeError, match=re.escape(message)):
attr.asdict(instance, retain_collection_types=True)
class TestAsTuple:
"""
Tests for `astuple`.
"""
@given(st.sampled_from(SEQUENCE_TYPES))
def test_shallow(self, C, tuple_factory):
"""
Shallow astuple returns correct dict.
"""
assert tuple_factory([1, 2]) == astuple(
C(x=1, y=2), False, tuple_factory=tuple_factory
)
@given(st.sampled_from(SEQUENCE_TYPES))
def test_recurse(self, C, tuple_factory):
"""
Deep astuple returns correct tuple.
"""
assert tuple_factory(
[tuple_factory([1, 2]), tuple_factory([3, 4])]
) == astuple(C(C(1, 2), C(3, 4)), tuple_factory=tuple_factory)
@given(nested_classes, st.sampled_from(SEQUENCE_TYPES))
def test_recurse_property(self, cls, tuple_class):
"""
Property tests for recursive astuple.
"""
obj = cls()
obj_tuple = astuple(obj, tuple_factory=tuple_class)
def assert_proper_tuple_class(obj, obj_tuple):
assert isinstance(obj_tuple, tuple_class)
for index, field in enumerate(fields(obj.__class__)):
field_val = getattr(obj, field.name)
if has(field_val.__class__):
# This field holds a class, recurse the assertions.
assert_proper_tuple_class(field_val, obj_tuple[index])
assert_proper_tuple_class(obj, obj_tuple)
@given(nested_classes, st.sampled_from(SEQUENCE_TYPES))
def test_recurse_retain(self, cls, tuple_class):
"""
Property tests for asserting collection types are retained.
"""
obj = cls()
obj_tuple = astuple(
obj, tuple_factory=tuple_class, retain_collection_types=True
)
def assert_proper_col_class(obj, obj_tuple):
# Iterate over all attributes, and if they are lists or mappings
# in the original, assert they are the same class in the dumped.
for index, field in enumerate(fields(obj.__class__)):
field_val = getattr(obj, field.name)
if has(field_val.__class__):
# This field holds a class, recurse the assertions.
assert_proper_col_class(field_val, obj_tuple[index])
elif isinstance(field_val, (list, tuple)):
# This field holds a sequence of something.
expected_type = type(obj_tuple[index])
assert type(field_val) is expected_type
for obj_e, obj_tuple_e in zip(field_val, obj_tuple[index]):
if has(obj_e.__class__):
assert_proper_col_class(obj_e, obj_tuple_e)
elif isinstance(field_val, dict):
orig = field_val
tupled = obj_tuple[index]
assert type(orig) is type(tupled)
for obj_e, obj_tuple_e in zip(
orig.items(), tupled.items()
):
if has(obj_e[0].__class__): # Dict key
assert_proper_col_class(obj_e[0], obj_tuple_e[0])
if has(obj_e[1].__class__): # Dict value
assert_proper_col_class(obj_e[1], obj_tuple_e[1])
assert_proper_col_class(obj, obj_tuple)
@given(st.sampled_from(SEQUENCE_TYPES))
def test_filter(self, C, tuple_factory):
"""
Attributes that are supposed to be skipped are skipped.
"""
assert tuple_factory([tuple_factory([1])]) == astuple(
C(C(1, 2), C(3, 4)),
filter=lambda a, v: a.name != "y",
tuple_factory=tuple_factory,
)
@given(container=st.sampled_from(SEQUENCE_TYPES))
def test_lists_tuples(self, container, C):
"""
If recurse is True, also recurse into lists.
"""
assert (1, [(2, 3), (4, 5), "a"]) == astuple(
C(1, container([C(2, 3), C(4, 5), "a"]))
)
@given(st.sampled_from(SEQUENCE_TYPES))
def test_dicts(self, C, tuple_factory):
"""
If recurse is True, also recurse into dicts.
"""
res = astuple(C(1, {"a": C(4, 5)}), tuple_factory=tuple_factory)
assert tuple_factory([1, {"a": tuple_factory([4, 5])}]) == res
assert isinstance(res, tuple_factory)
@given(container=st.sampled_from(SEQUENCE_TYPES))
def test_lists_tuples_retain_type(self, container, C):
"""
If recurse and retain_collection_types are True, also recurse
into lists and do not convert them into list.
"""
assert (1, container([(2, 3), (4, 5), "a"])) == astuple(
C(1, container([C(2, 3), C(4, 5), "a"])),
retain_collection_types=True,
)
@given(container=st.sampled_from(MAPPING_TYPES))
def test_dicts_retain_type(self, container, C):
"""
If recurse and retain_collection_types are True, also recurse
into lists and do not convert them into list.
"""
assert (1, container({"a": (4, 5)})) == astuple(
C(1, container({"a": C(4, 5)})), retain_collection_types=True
)
@given(simple_classes(), st.sampled_from(SEQUENCE_TYPES))
def test_roundtrip(self, cls, tuple_class):
"""
Test dumping to tuple and back for Hypothesis-generated classes.
"""
instance = cls()
tuple_instance = astuple(instance, tuple_factory=tuple_class)
assert isinstance(tuple_instance, tuple_class)
roundtrip_instance = cls(*tuple_instance)
assert instance == roundtrip_instance
@given(set_type=st.sampled_from((set, frozenset)))
def test_sets_no_retain(self, C, set_type):
"""
Set types are converted to lists if retain_collection_types=False.
"""
d = astuple(
C(1, set_type((1, 2, 3))),
retain_collection_types=False,
recurse=True,
)
assert (1, [1, 2, 3]) == d
def test_named_tuple_retain_type(self):
"""
Namedtuples can be serialized if retain_collection_types is True.
See #1164
"""
class Coordinates(NamedTuple):
lat: float
lon: float
@attr.s
class A:
coords: Coordinates = attr.ib()
instance = A(Coordinates(50.419019, 30.516225))
assert (Coordinates(50.419019, 30.516225),) == attr.astuple(
instance, retain_collection_types=True
)
def test_type_error_with_retain_type(self):
"""
Serialization that fails with TypeError leaves the error through if
they're not tuples.
See #1164
"""
message = "__new__() missing 1 required positional argument (astuple)"
class Coordinates(list):
def __init__(self, first, *rest):
if isinstance(first, list):
raise TypeError(message)
super().__init__([first, *rest])
@attr.s
class A:
coords: Coordinates = attr.ib()
instance = A(Coordinates(50.419019, 30.516225))
with pytest.raises(TypeError, match=re.escape(message)):
attr.astuple(instance, retain_collection_types=True)
class TestHas:
"""
Tests for `has`.
"""
def test_positive(self, C):
"""
Returns `True` on decorated classes.
"""
assert has(C)
def test_positive_empty(self):
"""
Returns `True` on decorated classes even if there are no attributes.
"""
@attr.s
class D:
pass
assert has(D)
def test_negative(self):
"""
Returns `False` on non-decorated classes.
"""
assert not has(object)
def test_generics(self):
"""
Works with generic classes.
"""
T = TypeVar("T")
@attr.define
class A(Generic[T]):
a: T
assert has(A)
assert has(A[str])
# Verify twice, since there's caching going on.
assert has(A[str])
def test_generics_negative(self):
"""
Returns `False` on non-decorated generic classes.
"""
T = TypeVar("T")
class A(Generic[T]):
a: T
assert not has(A)
assert not has(A[str])
# Verify twice, since there's caching going on.
assert not has(A[str])
class TestAssoc:
"""
Tests for `assoc`.
"""
@given(slots=st.booleans(), frozen=st.booleans())
def test_empty(self, slots, frozen):
"""
Empty classes without changes get copied.
"""
@attr.s(slots=slots, frozen=frozen)
class C:
pass
i1 = C()
i2 = assoc(i1)
assert i1 is not i2
assert i1 == i2
@given(simple_classes())
def test_no_changes(self, C):
"""
No changes means a verbatim copy.
"""
i1 = C()
i2 = assoc(i1)
assert i1 is not i2
assert i1 == i2
@given(simple_classes(), st.data())
def test_change(self, C, data):
"""
Changes work.
"""
# Take the first attribute, and change it.
assume(fields(C)) # Skip classes with no attributes.
field_names = [a.name for a in fields(C)]
original = C()
chosen_names = data.draw(st.sets(st.sampled_from(field_names)))
change_dict = {name: data.draw(st.integers()) for name in chosen_names}
changed = assoc(original, **change_dict)
for k, v in change_dict.items():
assert getattr(changed, k) == v
@given(simple_classes())
def test_unknown(self, C):
"""
Wanting to change an unknown attribute raises an
AttrsAttributeNotFoundError.
"""
# No generated class will have a four letter attribute.
with pytest.raises(AttrsAttributeNotFoundError) as e:
assoc(C(), aaaa=2)
assert (f"aaaa is not an attrs attribute on {C!r}.",) == e.value.args
def test_frozen(self):
"""
Works on frozen classes.
"""
@attr.s(frozen=True)
class C:
x = attr.ib()
y = attr.ib()
assert C(3, 2) == assoc(C(1, 2), x=3)
class TestEvolve:
"""
Tests for `evolve`.
"""
@given(slots=st.booleans(), frozen=st.booleans())
def test_empty(self, slots, frozen):
"""
Empty classes without changes get copied.
"""
@attr.s(slots=slots, frozen=frozen)
class C:
pass
i1 = C()
i2 = evolve(i1)
assert i1 is not i2
assert i1 == i2
@given(simple_classes())
def test_no_changes(self, C):
"""
No changes means a verbatim copy.
"""
i1 = C()
i2 = evolve(i1)
assert i1 is not i2
assert i1 == i2
@given(simple_classes(), st.data())
def test_change(self, C, data):
"""
Changes work.
"""
# Take the first attribute, and change it.
assume(fields(C)) # Skip classes with no attributes.
field_names = [a.name for a in fields(C)]
original = C()
chosen_names = data.draw(st.sets(st.sampled_from(field_names)))
# We pay special attention to private attributes, they should behave
# like in `__init__`.
change_dict = {
name.replace("_", ""): data.draw(st.integers())
for name in chosen_names
}
changed = evolve(original, **change_dict)
for name in chosen_names:
assert getattr(changed, name) == change_dict[name.replace("_", "")]
@given(simple_classes())
def test_unknown(self, C):
"""
Wanting to change an unknown attribute raises an
AttrsAttributeNotFoundError.
"""
# No generated class will have a four letter attribute.
with pytest.raises(TypeError) as e:
evolve(C(), aaaa=2)
if hasattr(C, "__attrs_init__"):
expected = (
"__attrs_init__() got an unexpected keyword argument 'aaaa'"
)
else:
expected = "__init__() got an unexpected keyword argument 'aaaa'"
assert e.value.args[0].endswith(expected)
def test_validator_failure(self):
"""
TypeError isn't swallowed when validation fails within evolve.
"""
@attr.s
class C:
a = attr.ib(validator=instance_of(int))
with pytest.raises(TypeError) as e:
evolve(C(a=1), a="some string")
m = e.value.args[0]
assert m.startswith("'a' must be <class 'int'>")
def test_private(self):
"""
evolve() acts as `__init__` with regards to private attributes.
"""
@attr.s
class C:
_a = attr.ib()
assert evolve(C(1), a=2)._a == 2
with pytest.raises(TypeError):
evolve(C(1), _a=2)
with pytest.raises(TypeError):
evolve(C(1), a=3, _a=2)
def test_non_init_attrs(self):
"""
evolve() handles `init=False` attributes.
"""
@attr.s
class C:
a = attr.ib()
b = attr.ib(init=False, default=0)
assert evolve(C(1), a=2).a == 2
def test_regression_attrs_classes(self):
"""
evolve() can evolve fields that are instances of attrs classes.
Regression test for #804
"""
@attr.s
class Cls1:
param1 = attr.ib()
@attr.s
class Cls2:
param2 = attr.ib()
obj2a = Cls2(param2="a")
obj2b = Cls2(param2="b")
obj1a = Cls1(param1=obj2a)
assert Cls1(param1=Cls2(param2="b")) == attr.evolve(
obj1a, param1=obj2b
)
def test_dicts(self):
"""
evolve() can replace an attrs class instance with a dict.
See #806
"""
@attr.s
class Cls1:
param1 = attr.ib()
@attr.s
class Cls2:
param2 = attr.ib()
obj2a = Cls2(param2="a")
obj2b = {"foo": 42, "param2": 42}
obj1a = Cls1(param1=obj2a)
assert Cls1({"foo": 42, "param2": 42}) == attr.evolve(
obj1a, param1=obj2b
)
def test_no_inst(self):
"""
Missing inst argument raises a TypeError like Python would.
"""
with pytest.raises(
TypeError, match=r"evolve\(\) takes 1 positional argument"
):
evolve(x=1)
def test_too_many_pos_args(self):
"""
More than one positional argument raises a TypeError like Python would.
"""
with pytest.raises(
TypeError,
match=r"evolve\(\) takes 1 positional argument, but 2 were given",
):
evolve(1, 2)
def test_can_change_inst(self):
"""
If the instance is passed by positional argument, a field named `inst`
can be changed.
"""
@attr.define
class C:
inst: int
assert C(42) == evolve(C(23), inst=42)
|