MCPcopy Create free account
hub / github.com/pybind/pybind11 / test_sequence_caster_protocol

Function test_sequence_caster_protocol

tests/test_stl.py:596–642  ·  view source on GitHub ↗
(doc)

Source from the content-addressed store, hash-verified

594
595
596def test_sequence_caster_protocol(doc):
597 from collections.abc import Sequence
598
599 # Implements the Sequence protocol without explicitly inheriting from collections.abc.Sequence.
600 class BareSequenceLike:
601 def __init__(self, *args):
602 self.data = tuple(args)
603
604 def __len__(self):
605 return len(self.data)
606
607 def __getitem__(self, index):
608 return self.data[index]
609
610 # Implements the Sequence protocol by reusing BareSequenceLike's implementation.
611 # Additionally, inherits from collections.abc.Sequence.
612 class FormalSequenceLike(BareSequenceLike, Sequence):
613 pass
614
615 # convert mode
616 assert (
617 doc(m.roundtrip_std_vector_int)
618 == "roundtrip_std_vector_int(arg0: collections.abc.Sequence[typing.SupportsInt | typing.SupportsIndex]) -> list[int]"
619 )
620 assert m.roundtrip_std_vector_int([1, 2, 3]) == [1, 2, 3]
621 assert m.roundtrip_std_vector_int((1, 2, 3)) == [1, 2, 3]
622 assert m.roundtrip_std_vector_int(FormalSequenceLike(1, 2, 3)) == [1, 2, 3]
623 assert m.roundtrip_std_vector_int(BareSequenceLike(1, 2, 3)) == [1, 2, 3]
624 assert m.roundtrip_std_vector_int([]) == []
625 assert m.roundtrip_std_vector_int(()) == []
626 assert m.roundtrip_std_vector_int(BareSequenceLike()) == []
627 # noconvert mode
628 assert (
629 doc(m.roundtrip_std_vector_int_noconvert)
630 == "roundtrip_std_vector_int_noconvert(v: list[int]) -> list[int]"
631 )
632 assert m.roundtrip_std_vector_int_noconvert([1, 2, 3]) == [1, 2, 3]
633 assert m.roundtrip_std_vector_int_noconvert((1, 2, 3)) == [1, 2, 3]
634 assert m.roundtrip_std_vector_int_noconvert(FormalSequenceLike(1, 2, 3)) == [
635 1,
636 2,
637 3,
638 ]
639 assert m.roundtrip_std_vector_int_noconvert(BareSequenceLike(1, 2, 3)) == [1, 2, 3]
640 assert m.roundtrip_std_vector_int_noconvert([]) == []
641 assert m.roundtrip_std_vector_int_noconvert(()) == []
642 assert m.roundtrip_std_vector_int_noconvert(BareSequenceLike()) == []
643
644
645def test_mapping_caster_protocol(doc):

Callers

nothing calls this directly

Calls 3

FormalSequenceLikeClass · 0.85
BareSequenceLikeClass · 0.85
docFunction · 0.70

Tested by

no test coverage detected