Skip to content

Commit 7c4ac91

Browse files
Add type[T] support to typing.h (#5166)
* add type[T] * style: pre-commit fixes * fix merge --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 68405a1 commit 7c4ac91

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

include/pybind11/typing.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ class Callable<Return(Args...)> : public function {
6363
using function::function;
6464
};
6565

66+
template <typename T>
67+
class Type : public type {
68+
using type::type;
69+
};
70+
6671
template <typename... Types>
6772
class Union : public object {
6873
using object::object;
@@ -131,6 +136,11 @@ struct handle_type_name<typing::Callable<Return(Args...)>> {
131136
+ const_name("], ") + make_caster<retval_type>::name + const_name("]");
132137
};
133138

139+
template <typename T>
140+
struct handle_type_name<typing::Type<T>> {
141+
static constexpr auto name = const_name("type[") + make_caster<T>::name + const_name("]");
142+
};
143+
134144
template <typename... Types>
135145
struct handle_type_name<typing::Union<Types...>> {
136146
static constexpr auto name = const_name("Union[")

tests/test_pytypes.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,7 @@ TEST_SUBMODULE(pytypes, m) {
844844
m.def("annotate_iterator_int", [](const py::typing::Iterator<int> &) {});
845845
m.def("annotate_fn",
846846
[](const py::typing::Callable<int(py::typing::List<py::str>, py::str)> &) {});
847+
m.def("annotate_type", [](const py::typing::Type<int> &) {});
847848

848849
m.def("annotate_union",
849850
[](py::typing::List<py::typing::Union<py::str, py::int_, py::object>> l,

tests/test_pytypes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,10 @@ def test_fn_annotations(doc):
957957
)
958958

959959

960+
def test_type_annotation(doc):
961+
assert doc(m.annotate_type) == "annotate_type(arg0: type[int]) -> None"
962+
963+
960964
def test_union_annotations(doc):
961965
assert (
962966
doc(m.annotate_union)

0 commit comments

Comments
 (0)