Skip to content

Commit c5c97f4

Browse files
InvincibleRMChenryiii
authored andcommitted
feat(types) Adds special Case for empty C++ tuple type annotation (#5214)
* add special case and unit test * add newline
1 parent 1ce5bc6 commit c5c97f4

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

include/pybind11/cast.h

+7
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,13 @@ class type_caster<std::pair<T1, T2>> : public tuple_caster<std::pair, T1, T2> {}
740740
template <typename... Ts>
741741
class type_caster<std::tuple<Ts...>> : public tuple_caster<std::tuple, Ts...> {};
742742

743+
template <>
744+
class type_caster<std::tuple<>> : public tuple_caster<std::tuple> {
745+
public:
746+
// PEP 484 specifies this syntax for an empty tuple
747+
static constexpr auto name = const_name("tuple[()]");
748+
};
749+
743750
/// Helper class which abstracts away certain actions. Users can provide specializations for
744751
/// custom holders, but it's only necessary if the type has a non-standard interface.
745752
template <typename T>

tests/test_builtin_casters.py

+2
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ def test_tuple(doc):
368368
"""
369369
)
370370

371+
assert doc(m.empty_tuple) == """empty_tuple() -> tuple[()]"""
372+
371373
assert m.rvalue_pair() == ("rvalue", "rvalue")
372374
assert m.lvalue_pair() == ("lvalue", "lvalue")
373375
assert m.rvalue_tuple() == ("rvalue", "rvalue", "rvalue")

0 commit comments

Comments
 (0)