Skip to content

feat(types): adds support for Never and NoReturn from python Typing #5193

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

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'origin/master' into typing-no-return-never
  • Loading branch information
InvincibleRMC committed Jun 25, 2024
commit 7c313c99c86169148f5f5c2bba086ed1a580a6fe
21 changes: 21 additions & 0 deletions include/pybind11/typing.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ class NoReturn : public none {
class Never : public none {
using none::none;
};
#if defined(__cpp_nontype_template_parameter_class)
template <size_t N>
struct StringLiteral {
constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, value); }
char value[N];
};

// Example syntax for creating a TypeVar.
// typedef typing::TypeVar<"T"> TypeVarT;
template <StringLiteral>
class TypeVar : public object {
PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type)
using object::object;
};
#endif

PYBIND11_NAMESPACE_END(typing)

Expand Down Expand Up @@ -172,6 +187,12 @@ template <>
struct handle_type_name<typing::Never> {
static constexpr auto name = const_name("Never");
};
#if defined(__cpp_nontype_template_parameter_class)
template <typing::StringLiteral StrLit>
struct handle_type_name<typing::TypeVar<StrLit>> {
static constexpr auto name = const_name(StrLit.value);
};
#endif

PYBIND11_NAMESPACE_END(detail)
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
16 changes: 16 additions & 0 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -880,4 +880,20 @@ TEST_SUBMODULE(pytypes, m) {

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; });

#if defined(__cpp_nontype_template_parameter_class)
m.def("annotate_generic_containers",
[](const py::typing::List<typevar::TypeVarT> &l) -> py::typing::List<typevar::TypeVarV> {
return l;
});

m.def("annotate_listT_to_T",
[](const py::typing::List<typevar::TypeVarT> &l) -> typevar::TypeVarT { return l[0]; });
m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; });
m.attr("if_defined__cpp_nontype_template_parameter_class") = true;
#else
m.attr("if_defined__cpp_nontype_template_parameter_class") = false;
#endif
}
20 changes: 20 additions & 0 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,3 +997,23 @@ def test_no_return_annotation(doc):

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)
== "annotate_optional_to_object(arg0: Optional[int]) -> object"
)


@pytest.mark.skipif(
not m.if_defined__cpp_nontype_template_parameter_class,
reason="C++20 feature not available.",
)
def test_typevar(doc):
assert (
doc(m.annotate_generic_containers)
== "annotate_generic_containers(arg0: list[T]) -> list[V]"
)

assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T"

assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T"
You are viewing a condensed version of this merge commit. You can view the full changes here.