Skip to content

Commit

Permalink
feat(types): adds support for TypeGuard and TypeIs (#5194)
Browse files Browse the repository at this point in the history
* Adds support for TypeGuard and TypeIs

* style: pre-commit fixes

---------

Co-authored-by: Henry Schreiner <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 26, 2024
1 parent b5ec7c7 commit 2be85c6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
22 changes: 22 additions & 0 deletions include/pybind11/typing.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,24 @@ class Optional : public object {
using object::object;
};

template <typename T>
class TypeGuard : public bool_ {
using bool_::bool_;
};

template <typename T>
class TypeIs : public bool_ {
using bool_::bool_;
};

class NoReturn : public none {
using none::none;
};

class Never : public none {
using none::none;
};

#if defined(__cpp_nontype_template_parameter_class)
template <size_t N>
struct StringLiteral {
Expand Down Expand Up @@ -183,6 +194,16 @@ struct handle_type_name<typing::Optional<T>> {
static constexpr auto name = const_name("Optional[") + make_caster<T>::name + const_name("]");
};

template <typename T>
struct handle_type_name<typing::TypeGuard<T>> {
static constexpr auto name = const_name("TypeGuard[") + make_caster<T>::name + const_name("]");
};

template <typename T>
struct handle_type_name<typing::TypeIs<T>> {
static constexpr auto name = const_name("TypeIs[") + make_caster<T>::name + const_name("]");
};

template <>
struct handle_type_name<typing::NoReturn> {
static constexpr auto name = const_name("NoReturn");
Expand All @@ -192,6 +213,7 @@ template <>
struct handle_type_name<typing::Never> {
static constexpr auto name = const_name("Never");
};

#if defined(__cpp_nontype_template_parameter_class)
template <typing::StringLiteral... Literals>
struct handle_type_name<typing::Literal<Literals...>> {
Expand Down
7 changes: 7 additions & 0 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -892,8 +892,15 @@ TEST_SUBMODULE(pytypes, m) {
return list;
});

m.def("annotate_type_guard", [](py::object &o) -> py::typing::TypeGuard<py::str> {
return py::isinstance<py::str>(o);
});
m.def("annotate_type_is",
[](py::object &o) -> py::typing::TypeIs<py::str> { return py::isinstance<py::str>(o); });

m.def("annotate_no_return", []() -> py::typing::NoReturn { throw 0; });
m.def("annotate_never", []() -> py::typing::Never { throw 0; });

m.def("annotate_optional_to_object",
[](py::typing::Optional<int> &o) -> py::object { return o; });

Expand Down
11 changes: 11 additions & 0 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,17 @@ def test_optional_annotations(doc):
)


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]"


def test_no_return_annotation(doc):
assert doc(m.annotate_no_return) == "annotate_no_return() -> NoReturn"

Expand Down

0 comments on commit 2be85c6

Please sign in to comment.