From d805e9967fbdd1b7cc68c95c960f401321449087 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Sun, 30 Jun 2024 12:52:37 -0400 Subject: [PATCH] feat(types) Adds special Case for empty C++ tuple type annotation (#5214) * add special case and unit test * add newline --- include/pybind11/cast.h | 7 +++++++ tests/test_builtin_casters.py | 2 ++ 2 files changed, 9 insertions(+) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 624b8ebaca..e41ad2abfa 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -740,6 +740,13 @@ class type_caster> : public tuple_caster {} template class type_caster> : public tuple_caster {}; +template <> +class type_caster> : public tuple_caster { +public: + // PEP 484 specifies this syntax for an empty tuple + static constexpr auto name = const_name("tuple[()]"); +}; + /// Helper class which abstracts away certain actions. Users can provide specializations for /// custom holders, but it's only necessary if the type has a non-standard interface. template diff --git a/tests/test_builtin_casters.py b/tests/test_builtin_casters.py index 9aa5926e9c..b37aacff1f 100644 --- a/tests/test_builtin_casters.py +++ b/tests/test_builtin_casters.py @@ -368,6 +368,8 @@ def test_tuple(doc): """ ) + assert doc(m.empty_tuple) == """empty_tuple() -> tuple[()]""" + assert m.rvalue_pair() == ("rvalue", "rvalue") assert m.lvalue_pair() == ("lvalue", "lvalue") assert m.rvalue_tuple() == ("rvalue", "rvalue", "rvalue")