(doc)
| 690 | |
| 691 | |
| 692 | def test_set_caster_protocol(doc): |
| 693 | from collections.abc import Set |
| 694 | |
| 695 | # Implements the Set protocol without explicitly inheriting from collections.abc.Set. |
| 696 | class BareSetLike: |
| 697 | def __init__(self, *args): |
| 698 | self.data = set(args) |
| 699 | |
| 700 | def __len__(self): |
| 701 | return len(self.data) |
| 702 | |
| 703 | def __contains__(self, item): |
| 704 | return item in self.data |
| 705 | |
| 706 | def __iter__(self): |
| 707 | yield from self.data |
| 708 | |
| 709 | # Implements the Set protocol by reusing BareSetLike's implementation. |
| 710 | # Additionally, inherits from collections.abc.Set. |
| 711 | class FormalSetLike(BareSetLike, Set): |
| 712 | pass |
| 713 | |
| 714 | # convert mode |
| 715 | assert ( |
| 716 | doc(m.roundtrip_std_set_int) |
| 717 | == "roundtrip_std_set_int(arg0: collections.abc.Set[typing.SupportsInt | typing.SupportsIndex]) -> set[int]" |
| 718 | ) |
| 719 | assert m.roundtrip_std_set_int({1, 2, 3}) == {1, 2, 3} |
| 720 | assert m.roundtrip_std_set_int(FormalSetLike(1, 2, 3)) == {1, 2, 3} |
| 721 | assert m.roundtrip_std_set_int(set()) == set() |
| 722 | assert m.roundtrip_std_set_int(FormalSetLike()) == set() |
| 723 | with pytest.raises(TypeError): |
| 724 | m.roundtrip_std_set_int(BareSetLike(1, 2, 3)) |
| 725 | # noconvert mode |
| 726 | assert ( |
| 727 | doc(m.roundtrip_std_set_int_noconvert) |
| 728 | == "roundtrip_std_set_int_noconvert(s: set[int]) -> set[int]" |
| 729 | ) |
| 730 | assert m.roundtrip_std_set_int_noconvert({1, 2, 3}) == {1, 2, 3} |
| 731 | assert m.roundtrip_std_set_int_noconvert(set()) == set() |
| 732 | with pytest.raises(TypeError): |
| 733 | m.roundtrip_std_set_int_noconvert(FormalSetLike(1, 2, 3)) |
| 734 | with pytest.raises(TypeError): |
| 735 | m.roundtrip_std_set_int_noconvert(BareSetLike(1, 2, 3)) |
nothing calls this directly
no test coverage detected