From 65370f330ea9349bd9e996028b4cb7adfde37660 Mon Sep 17 00:00:00 2001 From: Jason Watson Date: Thu, 21 Mar 2024 08:24:19 +0100 Subject: [PATCH] Create handle_type_name specialization to type-hint variable length tuples (#5051) --- include/pybind11/typing.h | 7 +++++++ tests/test_pytypes.cpp | 2 ++ tests/test_pytypes.py | 7 +++++++ 3 files changed, 16 insertions(+) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index b7b1a4e54..0ee329d85 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -79,6 +79,13 @@ struct handle_type_name> { static constexpr auto name = const_name("tuple[()]"); }; +template +struct handle_type_name> { + // PEP 484 specifies this syntax for a variable-length tuple + static constexpr auto name + = const_name("tuple[") + make_caster::name + const_name(", ...]"); +}; + template struct handle_type_name> { static constexpr auto name = const_name("dict[") + make_caster::name + const_name(", ") diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 0cd480ebb..e840eb61f 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -825,6 +825,8 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_tuple_float_str", [](const py::typing::Tuple &) {}); m.def("annotate_tuple_empty", [](const py::typing::Tuple<> &) {}); + m.def("annotate_tuple_variable_length", + [](const py::typing::Tuple &) {}); m.def("annotate_dict_str_int", [](const py::typing::Dict &) {}); m.def("annotate_list_int", [](const py::typing::List &) {}); m.def("annotate_set_str", [](const py::typing::Set &) {}); diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 2b2027316..cfb144523 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -911,6 +911,13 @@ def test_tuple_empty_annotations(doc): ) +def test_tuple_variable_length_annotations(doc): + assert ( + doc(m.annotate_tuple_variable_length) + == "annotate_tuple_variable_length(arg0: tuple[float, ...]) -> None" + ) + + def test_dict_annotations(doc): assert ( doc(m.annotate_dict_str_int)