Skip to content

Commit 13ceb09

Browse files
committed
Add specific optional test
1 parent 440ac9d commit 13ceb09

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

tests/test_pytypes.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,45 @@ TEST_SUBMODULE(pytypes, m) {
10341034
m.attr("defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL") = false;
10351035
#endif
10361036

1037+
#if defined(PYBIND11_TEST_PTYPES_HAS_OPTIONAL)
1038+
m.def("generic_T", []() -> void {}, pybind11::detail::typevar_record::from_name("T"));
1039+
1040+
m.def(
1041+
"generic_T_V",
1042+
[]() -> void {},
1043+
pybind11::detail::typevar_record::from_name("T"),
1044+
pybind11::detail::typevar_record::from_name("V"));
1045+
1046+
m.def(
1047+
"generic_bound_int",
1048+
[]() -> void {},
1049+
pybind11::detail::typevar_record::with_bound<int>("T"));
1050+
1051+
m.def(
1052+
"generic_constraints_int_str",
1053+
[]() -> void {},
1054+
pybind11::detail::typevar_record::with_constraints<py::int_, py::str>("T"));
1055+
1056+
m.def(
1057+
"generic_default_int",
1058+
[]() -> void {},
1059+
pybind11::detail::typevar_record::with_default<int>("T"));
1060+
1061+
m.def(
1062+
"generic_bound_and_default_int",
1063+
[]() -> void {},
1064+
pybind11::detail::typevar_record::with_default_and_bound<int, int>("T"));
1065+
1066+
m.def(
1067+
"generic_constraints_and_default",
1068+
[]() -> void {},
1069+
pybind11::detail::typevar_record::
1070+
with_default_and_constraints<py::str, py::typing::List<int>, py::str>("T"));
1071+
m.attr("defined_PYBIND11_TEST_PTYPES_HAS_OPTIONAL") = true;
1072+
#else
1073+
m.attr("defined_PYBIND11_TEST_PTYPES_HAS_OPTIONAL") = false;
1074+
#endif
1075+
10371076
#if defined(PYBIND11_TEST_PYTYPES_HAS_RANGES)
10381077

10391078
// test_tuple_ranges

tests/test_pytypes.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,38 @@ def test_literal(doc):
10851085
)
10861086

10871087

1088+
@pytest.mark.skipif(
1089+
not m.defined_PYBIND11_TEST_PTYPES_HAS_OPTIONAL,
1090+
reason="use of optional feature not available.",
1091+
)
1092+
def test_generic(doc):
1093+
assert doc(m.generic_T) == "generic_T[T]() -> None"
1094+
1095+
assert (
1096+
doc(m.generic_bound_int) == "generic_bound_int[T: typing.SupportsInt]() -> None"
1097+
)
1098+
1099+
assert (
1100+
doc(m.generic_constraints_int_str)
1101+
== "generic_constraints_int_str[T: (typing.SupportsInt, str)]() -> None"
1102+
)
1103+
1104+
assert (
1105+
doc(m.generic_default_int)
1106+
== "generic_default_int[T = typing.SupportsInt]() -> None"
1107+
)
1108+
1109+
assert (
1110+
doc(m.generic_bound_and_default_int)
1111+
== "generic_bound_and_default_int[T: typing.SupportsInt = typing.SupportsInt]() -> None"
1112+
)
1113+
1114+
assert (
1115+
doc(m.generic_constraints_and_default)
1116+
== "generic_constraints_and_default[T: (list[typing.SupportsInt], str) = str]() -> None"
1117+
)
1118+
1119+
10881120
@pytest.mark.skipif(
10891121
not m.defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL,
10901122
reason="C++20 non-type template args feature not available.",

0 commit comments

Comments
 (0)