diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 2c9eaffbcb..e0d0e45c45 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -63,6 +63,11 @@ class Callable : public function { using function::function; }; +template +class Type : public type { + using type::type; +}; + template class Union : public object { using object::object; @@ -131,6 +136,11 @@ struct handle_type_name> { + const_name("], ") + make_caster::name + const_name("]"); }; +template +struct handle_type_name> { + static constexpr auto name = const_name("type[") + make_caster::name + const_name("]"); +}; + template struct handle_type_name> { static constexpr auto name = const_name("Union[") diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index e97dceee3e..e5a318ad84 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -844,6 +844,7 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_iterator_int", [](const py::typing::Iterator &) {}); m.def("annotate_fn", [](const py::typing::Callable, py::str)> &) {}); + m.def("annotate_type", [](const py::typing::Type &) {}); m.def("annotate_union", [](py::typing::List> l, diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index ca2c51c405..72dac13b81 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -957,6 +957,10 @@ def test_fn_annotations(doc): ) +def test_type_annotation(doc): + assert doc(m.annotate_type) == "annotate_type(arg0: type[int]) -> None" + + def test_union_annotations(doc): assert ( doc(m.annotate_union)