Skip to content

Commit 05c42bd

Browse files
committed
Create handle_type_name specialization to handle variable length tuples
1 parent 8b48ff8 commit 05c42bd

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

include/pybind11/typing.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ struct handle_type_name<typing::Tuple<>> {
7979
static constexpr auto name = const_name("tuple[()]");
8080
};
8181

82+
template <typename T>
83+
struct handle_type_name<typing::Tuple<T, ellipsis>> {
84+
// PEP 484 specifies this syntax for a variable-length tuple
85+
static constexpr auto name = const_name("tuple[") + make_caster<T>::name + const_name(", ...]");
86+
};
87+
8288
template <typename K, typename V>
8389
struct handle_type_name<typing::Dict<K, V>> {
8490
static constexpr auto name = const_name("dict[") + make_caster<K>::name + const_name(", ")

tests/test_pytypes.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,7 @@ TEST_SUBMODULE(pytypes, m) {
825825

826826
m.def("annotate_tuple_float_str", [](const py::typing::Tuple<py::float_, py::str> &) {});
827827
m.def("annotate_tuple_empty", [](const py::typing::Tuple<> &) {});
828+
m.def("annotate_tuple_variable_length", [](const py::typing::Tuple<py::float_, py::ellipsis> &) {});
828829
m.def("annotate_dict_str_int", [](const py::typing::Dict<py::str, int> &) {});
829830
m.def("annotate_list_int", [](const py::typing::List<int> &) {});
830831
m.def("annotate_set_str", [](const py::typing::Set<std::string> &) {});

tests/test_pytypes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,12 @@ def test_tuple_empty_annotations(doc):
911911
)
912912

913913

914+
def test_tuple_variable_length_annotations(doc):
915+
assert (
916+
doc(m.annotate_tuple_variable_length) == "annotate_tuple_variable_length(arg0: tuple[float, ...]) -> None"
917+
)
918+
919+
914920
def test_dict_annotations(doc):
915921
assert (
916922
doc(m.annotate_dict_str_int)

0 commit comments

Comments
 (0)