Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(types): adds support for TypeGuard and TypeIs #5194

Merged
merged 4 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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