Skip to content

Commit 26281c7

Browse files
feat(types): adds support for Never and NoReturn from python Typing (#5193)
* Adds support for Never and NoReturn * lint
1 parent 183059f commit 26281c7

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

include/pybind11/typing.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ class Optional : public object {
8080
using object::object;
8181
};
8282

83+
class NoReturn : public none {
84+
using none::none;
85+
};
86+
87+
class Never : public none {
88+
using none::none;
89+
};
8390
#if defined(__cpp_nontype_template_parameter_class)
8491
template <size_t N>
8592
struct StringLiteral {
@@ -176,6 +183,15 @@ struct handle_type_name<typing::Optional<T>> {
176183
static constexpr auto name = const_name("Optional[") + make_caster<T>::name + const_name("]");
177184
};
178185

186+
template <>
187+
struct handle_type_name<typing::NoReturn> {
188+
static constexpr auto name = const_name("NoReturn");
189+
};
190+
191+
template <>
192+
struct handle_type_name<typing::Never> {
193+
static constexpr auto name = const_name("Never");
194+
};
179195
#if defined(__cpp_nontype_template_parameter_class)
180196
template <typing::StringLiteral... Literals>
181197
struct handle_type_name<typing::Literal<Literals...>> {

tests/test_pytypes.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,9 @@ TEST_SUBMODULE(pytypes, m) {
891891
list.append(py::none());
892892
return list;
893893
});
894+
895+
m.def("annotate_no_return", []() -> py::typing::NoReturn { throw 0; });
896+
m.def("annotate_never", []() -> py::typing::Never { throw 0; });
894897
m.def("annotate_optional_to_object",
895898
[](py::typing::Optional<int> &o) -> py::object { return o; });
896899

tests/test_pytypes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,14 @@ def test_optional_annotations(doc):
991991
)
992992

993993

994+
def test_no_return_annotation(doc):
995+
assert doc(m.annotate_no_return) == "annotate_no_return() -> NoReturn"
996+
997+
998+
def test_never_annotation(doc):
999+
assert doc(m.annotate_never) == "annotate_never() -> Never"
1000+
1001+
9941002
def test_optional_object_annotations(doc):
9951003
assert (
9961004
doc(m.annotate_optional_to_object)

0 commit comments

Comments
 (0)