(doc)
| 643 | |
| 644 | |
| 645 | def test_mapping_caster_protocol(doc): |
| 646 | from collections.abc import Mapping |
| 647 | |
| 648 | # Implements the Mapping protocol without explicitly inheriting from collections.abc.Mapping. |
| 649 | class BareMappingLike: |
| 650 | def __init__(self, **kwargs): |
| 651 | self.data = dict(kwargs) |
| 652 | |
| 653 | def __len__(self): |
| 654 | return len(self.data) |
| 655 | |
| 656 | def __getitem__(self, key): |
| 657 | return self.data[key] |
| 658 | |
| 659 | def __iter__(self): |
| 660 | yield from self.data |
| 661 | |
| 662 | # Implements the Mapping protocol by reusing BareMappingLike's implementation. |
| 663 | # Additionally, inherits from collections.abc.Mapping. |
| 664 | class FormalMappingLike(BareMappingLike, Mapping): |
| 665 | pass |
| 666 | |
| 667 | a1b2c3 = {"a": 1, "b": 2, "c": 3} |
| 668 | # convert mode |
| 669 | assert ( |
| 670 | doc(m.roundtrip_std_map_str_int) |
| 671 | == "roundtrip_std_map_str_int(arg0: collections.abc.Mapping[str, typing.SupportsInt | typing.SupportsIndex]) -> dict[str, int]" |
| 672 | ) |
| 673 | assert m.roundtrip_std_map_str_int(a1b2c3) == a1b2c3 |
| 674 | assert m.roundtrip_std_map_str_int(FormalMappingLike(**a1b2c3)) == a1b2c3 |
| 675 | assert m.roundtrip_std_map_str_int({}) == {} |
| 676 | assert m.roundtrip_std_map_str_int(FormalMappingLike()) == {} |
| 677 | with pytest.raises(TypeError): |
| 678 | m.roundtrip_std_map_str_int(BareMappingLike(**a1b2c3)) |
| 679 | # noconvert mode |
| 680 | assert ( |
| 681 | doc(m.roundtrip_std_map_str_int_noconvert) |
| 682 | == "roundtrip_std_map_str_int_noconvert(m: dict[str, int]) -> dict[str, int]" |
| 683 | ) |
| 684 | assert m.roundtrip_std_map_str_int_noconvert(a1b2c3) == a1b2c3 |
| 685 | assert m.roundtrip_std_map_str_int_noconvert({}) == {} |
| 686 | with pytest.raises(TypeError): |
| 687 | m.roundtrip_std_map_str_int_noconvert(FormalMappingLike(**a1b2c3)) |
| 688 | with pytest.raises(TypeError): |
| 689 | m.roundtrip_std_map_str_int_noconvert(BareMappingLike(**a1b2c3)) |
| 690 | |
| 691 | |
| 692 | def test_set_caster_protocol(doc): |
nothing calls this directly
no test coverage detected