Browse Source

fix: Missing typed variants of `iterator` and `iterable` (#4832)

pull/4846/head
Sergei Izmailov 2 years ago committed by GitHub
parent
commit
8c7b8dd0ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      include/pybind11/typing.h
  2. 2
      tests/test_pytypes.cpp
  3. 14
      tests/test_pytypes.py

20
include/pybind11/typing.h

@ -45,6 +45,16 @@ class Set : public set { @@ -45,6 +45,16 @@ class Set : public set {
using set::set;
};
template <typename T>
class Iterable : public iterable {
using iterable::iterable;
};
template <typename T>
class Iterator : public iterator {
using iterator::iterator;
};
template <typename Signature>
class Callable;
@ -85,6 +95,16 @@ struct handle_type_name<typing::Set<T>> { @@ -85,6 +95,16 @@ struct handle_type_name<typing::Set<T>> {
static constexpr auto name = const_name("set[") + make_caster<T>::name + const_name("]");
};
template <typename T>
struct handle_type_name<typing::Iterable<T>> {
static constexpr auto name = const_name("Iterable[") + make_caster<T>::name + const_name("]");
};
template <typename T>
struct handle_type_name<typing::Iterator<T>> {
static constexpr auto name = const_name("Iterator[") + make_caster<T>::name + const_name("]");
};
template <typename Return, typename... Args>
struct handle_type_name<typing::Callable<Return(Args...)>> {
using retval_type = conditional_t<std::is_same<Return, void>::value, void_type, Return>;

2
tests/test_pytypes.cpp

@ -828,6 +828,8 @@ TEST_SUBMODULE(pytypes, m) { @@ -828,6 +828,8 @@ TEST_SUBMODULE(pytypes, m) {
m.def("annotate_dict_str_int", [](const py::typing::Dict<py::str, int> &) {});
m.def("annotate_list_int", [](const py::typing::List<int> &) {});
m.def("annotate_set_str", [](const py::typing::Set<std::string> &) {});
m.def("annotate_iterable_str", [](const py::typing::Iterable<std::string> &) {});
m.def("annotate_iterator_int", [](const py::typing::Iterator<int> &) {});
m.def("annotate_fn",
[](const py::typing::Callable<int(py::typing::List<py::str>, py::str)> &) {});
}

14
tests/test_pytypes.py

@ -926,6 +926,20 @@ def test_set_annotations(doc): @@ -926,6 +926,20 @@ def test_set_annotations(doc):
assert doc(m.annotate_set_str) == "annotate_set_str(arg0: set[str]) -> None"
def test_iterable_annotations(doc):
assert (
doc(m.annotate_iterable_str)
== "annotate_iterable_str(arg0: Iterable[str]) -> None"
)
def test_iterator_annotations(doc):
assert (
doc(m.annotate_iterator_int)
== "annotate_iterator_int(arg0: Iterator[int]) -> None"
)
def test_fn_annotations(doc):
assert (
doc(m.annotate_fn)

Loading…
Cancel
Save