Skip to content

Commit 8c7b8dd

Browse files
authored
fix: Missing typed variants of iterator and iterable (#4832)
1 parent b457367 commit 8c7b8dd

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

include/pybind11/typing.h

+20
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ class Set : public set {
4545
using set::set;
4646
};
4747

48+
template <typename T>
49+
class Iterable : public iterable {
50+
using iterable::iterable;
51+
};
52+
53+
template <typename T>
54+
class Iterator : public iterator {
55+
using iterator::iterator;
56+
};
57+
4858
template <typename Signature>
4959
class Callable;
5060

@@ -85,6 +95,16 @@ struct handle_type_name<typing::Set<T>> {
8595
static constexpr auto name = const_name("set[") + make_caster<T>::name + const_name("]");
8696
};
8797

98+
template <typename T>
99+
struct handle_type_name<typing::Iterable<T>> {
100+
static constexpr auto name = const_name("Iterable[") + make_caster<T>::name + const_name("]");
101+
};
102+
103+
template <typename T>
104+
struct handle_type_name<typing::Iterator<T>> {
105+
static constexpr auto name = const_name("Iterator[") + make_caster<T>::name + const_name("]");
106+
};
107+
88108
template <typename Return, typename... Args>
89109
struct handle_type_name<typing::Callable<Return(Args...)>> {
90110
using retval_type = conditional_t<std::is_same<Return, void>::value, void_type, Return>;

tests/test_pytypes.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,8 @@ TEST_SUBMODULE(pytypes, m) {
828828
m.def("annotate_dict_str_int", [](const py::typing::Dict<py::str, int> &) {});
829829
m.def("annotate_list_int", [](const py::typing::List<int> &) {});
830830
m.def("annotate_set_str", [](const py::typing::Set<std::string> &) {});
831+
m.def("annotate_iterable_str", [](const py::typing::Iterable<std::string> &) {});
832+
m.def("annotate_iterator_int", [](const py::typing::Iterator<int> &) {});
831833
m.def("annotate_fn",
832834
[](const py::typing::Callable<int(py::typing::List<py::str>, py::str)> &) {});
833835
}

tests/test_pytypes.py

+14
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,20 @@ def test_set_annotations(doc):
926926
assert doc(m.annotate_set_str) == "annotate_set_str(arg0: set[str]) -> None"
927927

928928

929+
def test_iterable_annotations(doc):
930+
assert (
931+
doc(m.annotate_iterable_str)
932+
== "annotate_iterable_str(arg0: Iterable[str]) -> None"
933+
)
934+
935+
936+
def test_iterator_annotations(doc):
937+
assert (
938+
doc(m.annotate_iterator_int)
939+
== "annotate_iterator_int(arg0: Iterator[int]) -> None"
940+
)
941+
942+
929943
def test_fn_annotations(doc):
930944
assert (
931945
doc(m.annotate_fn)

0 commit comments

Comments
 (0)