Skip to content

Commit ee270ab

Browse files
committed
Added tests for mapping and set casters for noconvert mode
1 parent e06dc33 commit ee270ab

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

tests/test_stl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,4 +651,12 @@ TEST_SUBMODULE(stl, m) {
651651
m.def("roundtrip_std_vector_int", [](const std::vector<int> &v) { return v; });
652652
m.def("roundtrip_std_map_str_int", [](const std::map<std::string, int> &m) { return m; });
653653
m.def("roundtrip_std_set_int", [](const std::set<int> &s) { return s; });
654+
m.def(
655+
"roundtrip_std_map_str_int_noconvert",
656+
[](const std::map<std::string, int> &m) { return m; },
657+
py::arg("m").noconvert());
658+
m.def(
659+
"roundtrip_std_set_int_noconvert",
660+
[](const std::set<int> &s) { return s; },
661+
py::arg("s").noconvert());
654662
}

tests/test_stl.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,17 +632,29 @@ def __iter__(self):
632632
class FormalMappingLike(BareMappingLike, Mapping):
633633
pass
634634

635+
a1b2c3 = {"a": 1, "b": 2, "c": 3}
636+
# convert mode
635637
assert (
636638
doc(m.roundtrip_std_map_str_int)
637639
== "roundtrip_std_map_str_int(arg0: collections.abc.Mapping[str, typing.SupportsInt]) -> dict[str, int]"
638640
)
639-
a1b2c3 = {"a": 1, "b": 2, "c": 3}
640641
assert m.roundtrip_std_map_str_int(a1b2c3) == a1b2c3
641642
assert m.roundtrip_std_map_str_int(FormalMappingLike(**a1b2c3)) == a1b2c3
642643
assert m.roundtrip_std_map_str_int({}) == {}
643644
assert m.roundtrip_std_map_str_int(FormalMappingLike()) == {}
644645
with pytest.raises(TypeError):
645646
m.roundtrip_std_map_str_int(BareMappingLike(**a1b2c3))
647+
# noconvert mode
648+
assert (
649+
doc(m.roundtrip_std_map_str_int_noconvert)
650+
== "roundtrip_std_map_str_int_noconvert(m: dict[str, int]) -> dict[str, int]"
651+
)
652+
assert m.roundtrip_std_map_str_int_noconvert(a1b2c3) == a1b2c3
653+
assert m.roundtrip_std_map_str_int_noconvert({}) == {}
654+
with pytest.raises(TypeError):
655+
m.roundtrip_std_map_str_int_noconvert(FormalMappingLike(**a1b2c3))
656+
with pytest.raises(TypeError):
657+
m.roundtrip_std_map_str_int_noconvert(BareMappingLike(**a1b2c3))
646658

647659

648660
def test_set_caster_protocol(doc):
@@ -667,6 +679,7 @@ def __iter__(self):
667679
class FormalSetLike(BareSetLike, Set):
668680
pass
669681

682+
# convert mode
670683
assert (
671684
doc(m.roundtrip_std_set_int)
672685
== "roundtrip_std_set_int(arg0: collections.abc.Set[typing.SupportsInt]) -> set[int]"
@@ -677,3 +690,14 @@ class FormalSetLike(BareSetLike, Set):
677690
assert m.roundtrip_std_set_int(FormalSetLike()) == set()
678691
with pytest.raises(TypeError):
679692
m.roundtrip_std_set_int(BareSetLike(1, 2, 3))
693+
# noconvert mode
694+
assert (
695+
doc(m.roundtrip_std_set_int_noconvert)
696+
== "roundtrip_std_set_int_noconvert(s: set[int]) -> set[int]"
697+
)
698+
assert m.roundtrip_std_set_int_noconvert({1, 2, 3}) == {1, 2, 3}
699+
assert m.roundtrip_std_set_int_noconvert(set()) == set()
700+
with pytest.raises(TypeError):
701+
m.roundtrip_std_set_int_noconvert(FormalSetLike(1, 2, 3))
702+
with pytest.raises(TypeError):
703+
m.roundtrip_std_set_int_noconvert(BareSetLike(1, 2, 3))

0 commit comments

Comments
 (0)