Commit 7c4ac91d by Michael Carlstrom Committed by GitHub

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>
parent 68405a11
...@@ -63,6 +63,11 @@ class Callable<Return(Args...)> : public function { ...@@ -63,6 +63,11 @@ class Callable<Return(Args...)> : public function {
using function::function; using function::function;
}; };
template <typename T>
class Type : public type {
using type::type;
};
template <typename... Types> template <typename... Types>
class Union : public object { class Union : public object {
using object::object; using object::object;
...@@ -131,6 +136,11 @@ struct handle_type_name<typing::Callable<Return(Args...)>> { ...@@ -131,6 +136,11 @@ struct handle_type_name<typing::Callable<Return(Args...)>> {
+ const_name("], ") + make_caster<retval_type>::name + const_name("]"); + const_name("], ") + make_caster<retval_type>::name + const_name("]");
}; };
template <typename T>
struct handle_type_name<typing::Type<T>> {
static constexpr auto name = const_name("type[") + make_caster<T>::name + const_name("]");
};
template <typename... Types> template <typename... Types>
struct handle_type_name<typing::Union<Types...>> { struct handle_type_name<typing::Union<Types...>> {
static constexpr auto name = const_name("Union[") static constexpr auto name = const_name("Union[")
......
...@@ -844,6 +844,7 @@ TEST_SUBMODULE(pytypes, m) { ...@@ -844,6 +844,7 @@ TEST_SUBMODULE(pytypes, m) {
m.def("annotate_iterator_int", [](const py::typing::Iterator<int> &) {}); m.def("annotate_iterator_int", [](const py::typing::Iterator<int> &) {});
m.def("annotate_fn", m.def("annotate_fn",
[](const py::typing::Callable<int(py::typing::List<py::str>, py::str)> &) {}); [](const py::typing::Callable<int(py::typing::List<py::str>, py::str)> &) {});
m.def("annotate_type", [](const py::typing::Type<int> &) {});
m.def("annotate_union", m.def("annotate_union",
[](py::typing::List<py::typing::Union<py::str, py::int_, py::object>> l, [](py::typing::List<py::typing::Union<py::str, py::int_, py::object>> l,
......
...@@ -957,6 +957,10 @@ def test_fn_annotations(doc): ...@@ -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): def test_union_annotations(doc):
assert ( assert (
doc(m.annotate_union) doc(m.annotate_union)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment