Skip to content

Commit 66e6644

Browse files
committed
Added tests for Set, Iterable, Iterator, Union, and Optional
1 parent 413d685 commit 66e6644

File tree

2 files changed

+69
-9
lines changed

2 files changed

+69
-9
lines changed

tests/test_pytypes.cpp

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,12 +1037,14 @@ TEST_SUBMODULE(pytypes, m) {
10371037
m.attr("defined_PYBIND11_TEST_PYTYPES_HAS_RANGES") = false;
10381038
#endif
10391039
m.def("half_of_number", [](const RealNumber &x) { return RealNumber{x.value / 2}; });
1040+
// Tuple<T, T>
10401041
m.def("half_of_number_tuple", [](const py::typing::Tuple<RealNumber, RealNumber> &x) {
10411042
py::typing::Tuple<RealNumber, RealNumber> result
10421043
= py::make_tuple(RealNumber{x[0].cast<RealNumber>().value / 2},
10431044
RealNumber{x[1].cast<RealNumber>().value / 2});
10441045
return result;
10451046
});
1047+
// Tuple<T, ...>
10461048
m.def("half_of_number_tuple_ellipsis",
10471049
[](const py::typing::Tuple<RealNumber, py::ellipsis> &x) {
10481050
py::typing::Tuple<RealNumber, py::ellipsis> result(x.size());
@@ -1051,13 +1053,23 @@ TEST_SUBMODULE(pytypes, m) {
10511053
}
10521054
return result;
10531055
});
1056+
// Dict<K, V>
1057+
m.def("half_of_number_dict", [](const py::typing::Dict<std::string, RealNumber> &x) {
1058+
py::typing::Dict<std::string, RealNumber> result;
1059+
for (auto it : x) {
1060+
result[it.first] = RealNumber{it.second.cast<RealNumber>().value / 2};
1061+
}
1062+
return result;
1063+
});
1064+
// List<T>
10541065
m.def("half_of_number_list", [](const py::typing::List<RealNumber> &x) {
10551066
py::typing::List<RealNumber> result;
10561067
for (auto num : x) {
10571068
result.append(RealNumber{num.cast<RealNumber>().value / 2});
10581069
}
10591070
return result;
10601071
});
1072+
// List<List<T>>
10611073
m.def("half_of_number_nested_list",
10621074
[](const py::typing::List<py::typing::List<RealNumber>> &x) {
10631075
py::typing::List<py::typing::List<RealNumber>> result_lists;
@@ -1070,11 +1082,25 @@ TEST_SUBMODULE(pytypes, m) {
10701082
}
10711083
return result_lists;
10721084
});
1073-
m.def("half_of_number_dict", [](const py::typing::Dict<std::string, RealNumber> &x) {
1074-
py::typing::Dict<std::string, RealNumber> result;
1075-
for (auto it : x) {
1076-
result[it.first] = RealNumber{it.second.cast<RealNumber>().value / 2};
1077-
}
1078-
return result;
1079-
});
1085+
// Set<T>
1086+
m.def("identity_set", [](const py::typing::Set<RealNumber> &x) { return x; });
1087+
// Iterable<T>
1088+
m.def("identity_iterable", [](const py::typing::Iterable<RealNumber> &x) { return x; });
1089+
// Iterator<T>
1090+
m.def("identity_iterator", [](const py::typing::Iterator<RealNumber> &x) { return x; });
1091+
// Callable<R(A)>
1092+
// m.def("get_identity_callable", []() -> py::typing::Callable<RealNumber(const RealNumber &)>
1093+
// { return [](const RealNumber &x) { return x; };
1094+
// });
1095+
// Callable<R(...)>
1096+
// m.def("get_identity_callable_only_return",
1097+
// []() -> py::typing::Callable<RealNumber(py::ellipsis)> {
1098+
// return [](const RealNumber &x) { return x; };
1099+
// });
1100+
// Union<T1, T2>
1101+
m.def("identity_union", [](const py::typing::Union<RealNumber, std::string> &x) { return x; });
1102+
// Optional<T>
1103+
m.def("identity_optional", [](const py::typing::Optional<RealNumber> &x) { return x; });
1104+
// TypeGuard<T>
1105+
// TypeIs<T>
10801106
}

tests/test_pytypes.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,23 +1110,57 @@ def test_arg_return_type_hints(doc):
11101110
assert m.half_of_number(0) == 0
11111111
assert isinstance(m.half_of_number(0), float)
11121112
assert not isinstance(m.half_of_number(0), int)
1113+
# Tuple<T, T>
11131114
assert (
11141115
doc(m.half_of_number_tuple)
11151116
== "half_of_number_tuple(arg0: tuple[Union[float, int], Union[float, int]]) -> tuple[float, float]"
11161117
)
1118+
# Tuple<T, ...>
11171119
assert (
11181120
doc(m.half_of_number_tuple_ellipsis)
11191121
== "half_of_number_tuple_ellipsis(arg0: tuple[Union[float, int], ...]) -> tuple[float, ...]"
11201122
)
1123+
# Dict<K, V>
1124+
assert (
1125+
doc(m.half_of_number_dict)
1126+
== "half_of_number_dict(arg0: dict[str, Union[float, int]]) -> dict[str, float]"
1127+
)
1128+
# List<T>
11211129
assert (
11221130
doc(m.half_of_number_list)
11231131
== "half_of_number_list(arg0: list[Union[float, int]]) -> list[float]"
11241132
)
1133+
# List<List<T>>
11251134
assert (
11261135
doc(m.half_of_number_nested_list)
11271136
== "half_of_number_nested_list(arg0: list[list[Union[float, int]]]) -> list[list[float]]"
11281137
)
1138+
# Set<T>
11291139
assert (
1130-
doc(m.half_of_number_dict)
1131-
== "half_of_number_dict(arg0: dict[str, Union[float, int]]) -> dict[str, float]"
1140+
doc(m.identity_set)
1141+
== "identity_set(arg0: set[Union[float, int]]) -> set[float]"
1142+
)
1143+
# Iterable<T>
1144+
assert (
1145+
doc(m.identity_iterable)
1146+
== "identity_iterable(arg0: Iterable[Union[float, int]]) -> Iterable[float]"
1147+
)
1148+
# Iterator<T>
1149+
assert (
1150+
doc(m.identity_iterator)
1151+
== "identity_iterator(arg0: Iterator[Union[float, int]]) -> Iterator[float]"
1152+
)
1153+
# Callable<R(A)>
1154+
# Callable<R(...)>
1155+
# Union<T1, T2>
1156+
assert (
1157+
doc(m.identity_union)
1158+
== "identity_union(arg0: Union[Union[float, int], str]) -> Union[float, str]"
1159+
)
1160+
# Optional<T>
1161+
assert (
1162+
doc(m.identity_optional)
1163+
== "identity_optional(arg0: Optional[Union[float, int]]) -> Optional[float]"
11321164
)
1165+
# TypeGuard<T>
1166+
# TypeIs<T>

0 commit comments

Comments
 (0)