From 72732ee6d5c0199f45b5db46b90c914c5b72e0aa Mon Sep 17 00:00:00 2001 From: Jason J Watson Date: Fri, 8 Mar 2024 09:28:22 +0100 Subject: [PATCH 1/2] Create handle_type_name specialization to handle variable length tuples --- include/pybind11/typing.h | 6 ++++++ tests/test_pytypes.cpp | 1 + tests/test_pytypes.py | 6 ++++++ 3 files changed, 13 insertions(+) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index b7b1a4e54a..156b103d66 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -79,6 +79,12 @@ 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 0cd480ebba..7db08f84a2 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -825,6 +825,7 @@ 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 2b2027316c..fad1e403fb 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -911,6 +911,12 @@ 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) From 03c701cf81965b1961e220405bd0440c55edca9d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 8 Mar 2024 08:35:43 +0000 Subject: [PATCH 2/2] style: pre-commit fixes --- include/pybind11/typing.h | 3 ++- tests/test_pytypes.cpp | 3 ++- tests/test_pytypes.py | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 156b103d66..0ee329d85a 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -82,7 +82,8 @@ struct handle_type_name> { 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(", ...]"); + static constexpr auto name + = const_name("tuple[") + make_caster::name + const_name(", ...]"); }; template diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 7db08f84a2..e840eb61f8 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -825,7 +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_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 fad1e403fb..cfb144523a 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -913,7 +913,8 @@ 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" + doc(m.annotate_tuple_variable_length) + == "annotate_tuple_variable_length(arg0: tuple[float, ...]) -> None" )