Skip to content

Commit 872f9ff

Browse files
committed
Updated STL type hints use support collections.abc
1 parent bb504dd commit 872f9ff

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

include/pybind11/stl.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ struct set_caster {
203203
return s.release();
204204
}
205205

206-
PYBIND11_TYPE_CASTER(type, const_name("set[") + key_conv::name + const_name("]"));
206+
PYBIND11_TYPE_CASTER(type,
207+
io_name("collections.abc.Set", "set") + const_name("[") + key_conv::name
208+
+ const_name("]"));
207209
};
208210

209211
template <typename Type, typename Key, typename Value>
@@ -274,7 +276,8 @@ struct map_caster {
274276
}
275277

276278
PYBIND11_TYPE_CASTER(Type,
277-
const_name("dict[") + key_conv::name + const_name(", ") + value_conv::name
279+
io_name("collections.abc.Mapping", "dict") + const_name("[")
280+
+ key_conv::name + const_name(", ") + value_conv::name
278281
+ const_name("]"));
279282
};
280283

@@ -340,7 +343,9 @@ struct list_caster {
340343
return l.release();
341344
}
342345

343-
PYBIND11_TYPE_CASTER(Type, const_name("list[") + value_conv::name + const_name("]"));
346+
PYBIND11_TYPE_CASTER(Type,
347+
io_name("collections.abc.Sequence", "list") + const_name("[")
348+
+ value_conv::name + const_name("]"));
344349
};
345350

346351
template <typename Type, typename Alloc>
@@ -474,8 +479,9 @@ struct array_caster {
474479
using cast_op_type = movable_cast_op_type<T_>;
475480

476481
static constexpr auto name
477-
= const_name<Resizable>(const_name(""), const_name("Annotated[")) + const_name("list[")
478-
+ value_conv::name + const_name("]")
482+
= const_name<Resizable>(const_name(""), const_name("Annotated["))
483+
+ io_name("collections.abc.Sequence", "list") + const_name("[") + value_conv::name
484+
+ const_name("]")
479485
+ const_name<Resizable>(
480486
const_name(""), const_name(", FixedSize(") + const_name<Size>() + const_name(")]"));
481487
};

tests/test_kwargs_and_defaults.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_function_signatures(doc):
2222
assert doc(m.kw_func3) == "kw_func3(data: str = 'Hello world!') -> None"
2323
assert (
2424
doc(m.kw_func4)
25-
== "kw_func4(myList: list[typing.SupportsInt] = [13, 17]) -> str"
25+
== "kw_func4(myList: collections.abc.Sequence[typing.SupportsInt] = [13, 17]) -> str"
2626
)
2727
assert (
2828
doc(m.kw_func_udl)

tests/test_pytypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1250,7 +1250,7 @@ def test_arg_return_type_hints(doc):
12501250
# std::vector<T>
12511251
assert (
12521252
doc(m.half_of_number_vector)
1253-
== "half_of_number_vector(arg0: list[Union[float, int]]) -> list[float]"
1253+
== "half_of_number_vector(arg0: collections.abc.Sequence[Union[float, int]]) -> list[float]"
12541254
)
12551255
# Tuple<T, T>
12561256
assert (

tests/test_stl.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ def test_vector(doc):
2020
assert m.load_bool_vector((True, False))
2121

2222
assert doc(m.cast_vector) == "cast_vector() -> list[int]"
23-
assert doc(m.load_vector) == "load_vector(arg0: list[typing.SupportsInt]) -> bool"
23+
assert (
24+
doc(m.load_vector)
25+
== "load_vector(arg0: collections.abc.Sequence[typing.SupportsInt]) -> bool"
26+
)
2427

2528
# Test regression caused by 936: pointers to stl containers weren't castable
2629
assert m.cast_ptr_vector() == ["lvalue", "lvalue"]
@@ -45,7 +48,7 @@ def test_array(doc):
4548
assert doc(m.cast_array) == "cast_array() -> Annotated[list[int], FixedSize(2)]"
4649
assert (
4750
doc(m.load_array)
48-
== "load_array(arg0: Annotated[list[typing.SupportsInt], FixedSize(2)]) -> bool"
51+
== "load_array(arg0: Annotated[collections.abc.Sequence[typing.SupportsInt], FixedSize(2)]) -> bool"
4952
)
5053

5154

@@ -65,7 +68,8 @@ def test_valarray(doc):
6568

6669
assert doc(m.cast_valarray) == "cast_valarray() -> list[int]"
6770
assert (
68-
doc(m.load_valarray) == "load_valarray(arg0: list[typing.SupportsInt]) -> bool"
71+
doc(m.load_valarray)
72+
== "load_valarray(arg0: collections.abc.Sequence[typing.SupportsInt]) -> bool"
6973
)
7074

7175

@@ -79,7 +83,9 @@ def test_map(doc):
7983
assert m.load_map(d)
8084

8185
assert doc(m.cast_map) == "cast_map() -> dict[str, str]"
82-
assert doc(m.load_map) == "load_map(arg0: dict[str, str]) -> bool"
86+
assert (
87+
doc(m.load_map) == "load_map(arg0: collections.abc.Mapping[str, str]) -> bool"
88+
)
8389

8490

8591
def test_set(doc):
@@ -91,7 +97,7 @@ def test_set(doc):
9197
assert m.load_set(frozenset(s))
9298

9399
assert doc(m.cast_set) == "cast_set() -> set[str]"
94-
assert doc(m.load_set) == "load_set(arg0: set[str]) -> bool"
100+
assert doc(m.load_set) == "load_set(arg0: collections.abc.Set[str]) -> bool"
95101

96102

97103
def test_recursive_casting():
@@ -273,7 +279,7 @@ def __fspath__(self):
273279
assert m.parent_paths(["foo/bar", "foo/baz"]) == [Path("foo"), Path("foo")]
274280
assert (
275281
doc(m.parent_paths)
276-
== "parent_paths(arg0: list[Union[os.PathLike, str, bytes]]) -> list[pathlib.Path]"
282+
== "parent_paths(arg0: collections.abc.Sequence[Union[os.PathLike, str, bytes]]) -> list[pathlib.Path]"
277283
)
278284
# py::typing::List
279285
assert m.parent_paths_list(["foo/bar", "foo/baz"]) == [Path("foo"), Path("foo")]
@@ -364,7 +370,7 @@ def test_stl_pass_by_pointer(msg):
364370
msg(excinfo.value)
365371
== """
366372
stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported:
367-
1. (v: list[typing.SupportsInt] = None) -> list[int]
373+
1. (v: collections.abc.Sequence[typing.SupportsInt] = None) -> list[int]
368374
369375
Invoked with:
370376
"""
@@ -376,7 +382,7 @@ def test_stl_pass_by_pointer(msg):
376382
msg(excinfo.value)
377383
== """
378384
stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported:
379-
1. (v: list[typing.SupportsInt] = None) -> list[int]
385+
1. (v: collections.abc.Sequence[typing.SupportsInt] = None) -> list[int]
380386
381387
Invoked with: None
382388
"""

0 commit comments

Comments
 (0)