mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-22 05:05:11 +00:00
feat(types): adds support for Never and NoReturn from python Typing (#5193)
* Adds support for Never and NoReturn * lint
This commit is contained in:
parent
183059f9a4
commit
26281c7986
@ -80,6 +80,13 @@ class Optional : public object {
|
||||
using object::object;
|
||||
};
|
||||
|
||||
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 {
|
||||
@ -176,6 +183,15 @@ struct handle_type_name<typing::Optional<T>> {
|
||||
static constexpr auto name = const_name("Optional[") + make_caster<T>::name + const_name("]");
|
||||
};
|
||||
|
||||
template <>
|
||||
struct handle_type_name<typing::NoReturn> {
|
||||
static constexpr auto name = const_name("NoReturn");
|
||||
};
|
||||
|
||||
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...>> {
|
||||
|
@ -891,6 +891,9 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
list.append(py::none());
|
||||
return list;
|
||||
});
|
||||
|
||||
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; });
|
||||
|
||||
|
@ -991,6 +991,14 @@ def test_optional_annotations(doc):
|
||||
)
|
||||
|
||||
|
||||
def test_no_return_annotation(doc):
|
||||
assert doc(m.annotate_no_return) == "annotate_no_return() -> NoReturn"
|
||||
|
||||
|
||||
def test_never_annotation(doc):
|
||||
assert doc(m.annotate_never) == "annotate_never() -> Never"
|
||||
|
||||
|
||||
def test_optional_object_annotations(doc):
|
||||
assert (
|
||||
doc(m.annotate_optional_to_object)
|
||||
|
Loading…
Reference in New Issue
Block a user