From 068dea60b41b0ad44dd659c39ee8650a86ed01cf Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Thu, 13 Jun 2024 21:00:03 -0400 Subject: [PATCH 1/3] add type[T] --- include/pybind11/typing.h | 10 ++++++++++ tests/test_pytypes.cpp | 1 + tests/test_pytypes.py | 7 +++++++ 3 files changed, 18 insertions(+) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index bc275fc50b..e125d0f0d7 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; +}; + PYBIND11_NAMESPACE_END(typing) PYBIND11_NAMESPACE_BEGIN(detail) @@ -121,5 +126,10 @@ 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("]"); +}; + PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index f3709d40c6..96cac24d67 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -844,4 +844,5 @@ 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 &) {}); } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 38edfd9998..4fd0e042a8 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -955,3 +955,10 @@ def test_fn_annotations(doc): doc(m.annotate_fn) == "annotate_fn(arg0: Callable[[list[str], str], int]) -> None" ) + + +def test_type_annotation(doc): + assert ( + doc(m.annotate_type) + == "annotate_type(arg0: type[int]) -> None" + ) From 746cbba2806858be47d84fe55dc9635a4dd68722 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 01:01:21 +0000 Subject: [PATCH 2/3] style: pre-commit fixes --- tests/test_pytypes.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 4fd0e042a8..1278c7fc2b 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -958,7 +958,4 @@ def test_fn_annotations(doc): def test_type_annotation(doc): - assert ( - doc(m.annotate_type) - == "annotate_type(arg0: type[int]) -> None" - ) + assert doc(m.annotate_type) == "annotate_type(arg0: type[int]) -> None" From fd47e28afb980c0b2f85eed699781e293607e9c8 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Sat, 15 Jun 2024 08:28:12 -0400 Subject: [PATCH 3/3] fix merge --- include/pybind11/typing.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index c4bc8d0bf8..e0d0e45c45 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -66,7 +66,9 @@ class Callable : public function { template class Type : public type { using type::type; -} template +}; + +template class Union : public object { using object::object; }; @@ -137,7 +139,9 @@ struct handle_type_name> { template struct handle_type_name> { static constexpr auto name = const_name("type[") + make_caster::name + const_name("]"); -} template +}; + +template struct handle_type_name> { static constexpr auto name = const_name("Union[") + ::pybind11::detail::concat(make_caster::name...)