From fb9313e43086928aa42db08894cae384979aaeef Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Tue, 25 Jun 2024 11:24:34 -0400 Subject: [PATCH 1/2] Adds support for TypeGuard and TypeIs --- include/pybind11/typing.h | 20 ++++++++++++++++++++ tests/test_pytypes.cpp | 6 ++++++ tests/test_pytypes.py | 11 +++++++++++ 3 files changed, 37 insertions(+) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index e0d0e45c45..7cc1d1b383 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -78,6 +78,16 @@ class Optional : public object { using object::object; }; +template +class TypeGuard : public bool_ { + using bool_::bool_; +}; + +template +class TypeIs : public bool_ { + using bool_::bool_; +}; + PYBIND11_NAMESPACE_END(typing) PYBIND11_NAMESPACE_BEGIN(detail) @@ -153,5 +163,15 @@ struct handle_type_name> { static constexpr auto name = const_name("Optional[") + make_caster::name + const_name("]"); }; +template +struct handle_type_name> { + static constexpr auto name = const_name("TypeGuard[") + make_caster::name + const_name("]"); +}; + +template +struct handle_type_name> { + static constexpr auto name = const_name("TypeIs[") + 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 e5a318ad84..b5fbcd39a1 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -867,4 +867,10 @@ TEST_SUBMODULE(pytypes, m) { list.append(py::none()); return list; }); + + m.def("annotate_type_guard", [](py::object &o) -> py::typing::TypeGuard { + return py::isinstance(o); + }); + m.def("annotate_type_is", + [](py::object &o) -> py::typing::TypeIs { return py::isinstance(o); }); } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 8e35c70733..35115834b5 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -982,3 +982,14 @@ def test_optional_annotations(doc): doc(m.annotate_optional) == "annotate_optional(arg0: list) -> list[Optional[str]]" ) + + +def test_type_guard_annotations(doc): + assert ( + doc(m.annotate_type_guard) + == "annotate_type_guard(arg0: object) -> TypeGuard[str]" + ) + + +def test_type_is_annotations(doc): + assert doc(m.annotate_type_is) == "annotate_type_is(arg0: object) -> TypeIs[str]" From 67ae0689d94733bce3b5fdef7a196edcde9320de Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 26 Jun 2024 01:15:41 +0000 Subject: [PATCH 2/2] style: pre-commit fixes --- include/pybind11/typing.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 052650c2ee..cf70b739ed 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -97,7 +97,7 @@ class NoReturn : public none { class Never : public none { using none::none; }; - + #if defined(__cpp_nontype_template_parameter_class) template struct StringLiteral { @@ -203,7 +203,7 @@ template struct handle_type_name> { static constexpr auto name = const_name("TypeIs[") + make_caster::name + const_name("]"); }; - + template <> struct handle_type_name { static constexpr auto name = const_name("NoReturn");