Skip to content

Commit 6742435

Browse files
fix(types): add typing and collections.abc module prefix (#5663)
* Fix Python 3.8 type hints and add module prefix These type hints are invalid in Python 3.8. Add `typing.` prefix to remove ambiguity. * style: pre-commit fixes * Add module prefix to Union * Rename macros * Improve comment * Comment out 3.8 type hint macros Fixing this issue in Python 3.8 will require updating lots of tests. This can be added in a further pull request. * Add Iterable module prefix * Add module prefix to Iterator * Add module prefix to Callable * Re-add accidentally deleted brackets * Add module prefix to Optional * Add module prefix to Final * Add module prefix to ClassVar * Add module prefix to TypeGuard * Add module prefix to TypeIs * Add module prefix to NoReturn * Add module prefix to Never * Add module prefix to Literal * Add module prefix to Callable * Add module prefix to Sequence * Add module prefix to Iterator * style: pre-commit fixes * Remove type hint macros * style: pre-commit fixes --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 4587d33 commit 6742435

13 files changed

+126
-94
lines changed

include/pybind11/cast.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,7 @@ struct handle_type_name<dict> {
13591359
};
13601360
template <>
13611361
struct handle_type_name<anyset> {
1362-
static constexpr auto name = const_name("Union[set, frozenset]");
1362+
static constexpr auto name = const_name("typing.Union[set, frozenset]");
13631363
};
13641364
template <>
13651365
struct handle_type_name<set> {
@@ -1395,19 +1395,19 @@ struct handle_type_name<int_> {
13951395
};
13961396
template <>
13971397
struct handle_type_name<iterable> {
1398-
static constexpr auto name = const_name("Iterable");
1398+
static constexpr auto name = const_name("collections.abc.Iterable");
13991399
};
14001400
template <>
14011401
struct handle_type_name<iterator> {
1402-
static constexpr auto name = const_name("Iterator");
1402+
static constexpr auto name = const_name("collections.abc.Iterator");
14031403
};
14041404
template <>
14051405
struct handle_type_name<float_> {
14061406
static constexpr auto name = io_name("typing.SupportsFloat", "float");
14071407
};
14081408
template <>
14091409
struct handle_type_name<function> {
1410-
static constexpr auto name = const_name("Callable");
1410+
static constexpr auto name = const_name("collections.abc.Callable");
14111411
};
14121412
template <>
14131413
struct handle_type_name<handle> {
@@ -1419,7 +1419,7 @@ struct handle_type_name<none> {
14191419
};
14201420
template <>
14211421
struct handle_type_name<sequence> {
1422-
static constexpr auto name = const_name("Sequence");
1422+
static constexpr auto name = const_name("collections.abc.Sequence");
14231423
};
14241424
template <>
14251425
struct handle_type_name<bytearray> {

include/pybind11/functional.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ struct type_caster<std::function<Return(Args...)>> {
137137

138138
PYBIND11_TYPE_CASTER(
139139
type,
140-
const_name("Callable[[")
140+
const_name("collections.abc.Callable[[")
141141
+ ::pybind11::detail::concat(::pybind11::detail::arg_descr(make_caster<Args>::name)...)
142142
+ const_name("], ") + ::pybind11::detail::return_descr(make_caster<retval_type>::name)
143143
+ const_name("]"));

include/pybind11/pybind11.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ PYBIND11_NAMESPACE_END(function_record_PyTypeObject_methods)
12521252

12531253
template <>
12541254
struct handle_type_name<cpp_function> {
1255-
static constexpr auto name = const_name("Callable");
1255+
static constexpr auto name = const_name("collections.abc.Callable");
12561256
};
12571257

12581258
PYBIND11_NAMESPACE_END(detail)

include/pybind11/stl.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,8 @@ struct optional_caster {
557557
return true;
558558
}
559559

560-
PYBIND11_TYPE_CASTER(Type, const_name("Optional[") + value_conv::name + const_name("]"));
560+
PYBIND11_TYPE_CASTER(Type,
561+
const_name("typing.Optional[") + value_conv::name + const_name("]"));
561562
};
562563

563564
#if defined(PYBIND11_HAS_OPTIONAL)
@@ -642,7 +643,7 @@ struct variant_caster<V<Ts...>> {
642643

643644
using Type = V<Ts...>;
644645
PYBIND11_TYPE_CASTER(Type,
645-
const_name("Union[")
646+
const_name("typing.Union[")
646647
+ ::pybind11::detail::concat(make_caster<Ts>::name...)
647648
+ const_name("]"));
648649
};

include/pybind11/stl/filesystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ struct path_caster {
9696
return true;
9797
}
9898

99-
PYBIND11_TYPE_CASTER(T, io_name("Union[os.PathLike, str, bytes]", "pathlib.Path"));
99+
PYBIND11_TYPE_CASTER(T, io_name("typing.Union[os.PathLike, str, bytes]", "pathlib.Path"));
100100
};
101101

102102
#endif // PYBIND11_HAS_FILESYSTEM || defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)

include/pybind11/typing.h

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,21 @@ struct handle_type_name<typing::Set<T>> {
182182

183183
template <typename T>
184184
struct handle_type_name<typing::Iterable<T>> {
185-
static constexpr auto name = const_name("Iterable[") + make_caster<T>::name + const_name("]");
185+
static constexpr auto name
186+
= const_name("collections.abc.Iterable[") + make_caster<T>::name + const_name("]");
186187
};
187188

188189
template <typename T>
189190
struct handle_type_name<typing::Iterator<T>> {
190-
static constexpr auto name = const_name("Iterator[") + make_caster<T>::name + const_name("]");
191+
static constexpr auto name
192+
= const_name("collections.abc.Iterator[") + make_caster<T>::name + const_name("]");
191193
};
192194

193195
template <typename Return, typename... Args>
194196
struct handle_type_name<typing::Callable<Return(Args...)>> {
195197
using retval_type = conditional_t<std::is_same<Return, void>::value, void_type, Return>;
196198
static constexpr auto name
197-
= const_name("Callable[[")
199+
= const_name("collections.abc.Callable[[")
198200
+ ::pybind11::detail::concat(::pybind11::detail::arg_descr(make_caster<Args>::name)...)
199201
+ const_name("], ") + ::pybind11::detail::return_descr(make_caster<retval_type>::name)
200202
+ const_name("]");
@@ -204,7 +206,7 @@ template <typename Return>
204206
struct handle_type_name<typing::Callable<Return(ellipsis)>> {
205207
// PEP 484 specifies this syntax for defining only return types of callables
206208
using retval_type = conditional_t<std::is_same<Return, void>::value, void_type, Return>;
207-
static constexpr auto name = const_name("Callable[..., ")
209+
static constexpr auto name = const_name("collections.abc.Callable[..., ")
208210
+ ::pybind11::detail::return_descr(make_caster<retval_type>::name)
209211
+ const_name("]");
210212
};
@@ -216,46 +218,50 @@ struct handle_type_name<typing::Type<T>> {
216218

217219
template <typename... Types>
218220
struct handle_type_name<typing::Union<Types...>> {
219-
static constexpr auto name = const_name("Union[")
221+
static constexpr auto name = const_name("typing.Union[")
220222
+ ::pybind11::detail::concat(make_caster<Types>::name...)
221223
+ const_name("]");
222224
};
223225

224226
template <typename T>
225227
struct handle_type_name<typing::Optional<T>> {
226-
static constexpr auto name = const_name("Optional[") + make_caster<T>::name + const_name("]");
228+
static constexpr auto name
229+
= const_name("typing.Optional[") + make_caster<T>::name + const_name("]");
227230
};
228231

229232
template <typename T>
230233
struct handle_type_name<typing::Final<T>> {
231-
static constexpr auto name = const_name("Final[")
234+
static constexpr auto name = const_name("typing.Final[")
232235
+ ::pybind11::detail::return_descr(make_caster<T>::name)
233236
+ const_name("]");
234237
};
235238

236239
template <typename T>
237240
struct handle_type_name<typing::ClassVar<T>> {
238-
static constexpr auto name = const_name("ClassVar[") + make_caster<T>::name + const_name("]");
241+
static constexpr auto name
242+
= const_name("typing.ClassVar[") + make_caster<T>::name + const_name("]");
239243
};
240244

241245
template <typename T>
242246
struct handle_type_name<typing::TypeGuard<T>> {
243-
static constexpr auto name = const_name("TypeGuard[") + make_caster<T>::name + const_name("]");
247+
static constexpr auto name
248+
= const_name("typing.TypeGuard[") + make_caster<T>::name + const_name("]");
244249
};
245250

246251
template <typename T>
247252
struct handle_type_name<typing::TypeIs<T>> {
248-
static constexpr auto name = const_name("TypeIs[") + make_caster<T>::name + const_name("]");
253+
static constexpr auto name
254+
= const_name("typing.TypeIs[") + make_caster<T>::name + const_name("]");
249255
};
250256

251257
template <>
252258
struct handle_type_name<typing::NoReturn> {
253-
static constexpr auto name = const_name("NoReturn");
259+
static constexpr auto name = const_name("typing.NoReturn");
254260
};
255261

256262
template <>
257263
struct handle_type_name<typing::Never> {
258-
static constexpr auto name = const_name("Never");
264+
static constexpr auto name = const_name("typing.Never");
259265
};
260266

261267
#if defined(PYBIND11_TYPING_H_HAS_STRING_LITERAL)
@@ -281,7 +287,7 @@ consteval auto sanitize_string_literal() {
281287
template <typing::StringLiteral... Literals>
282288
struct handle_type_name<typing::Literal<Literals...>> {
283289
static constexpr auto name
284-
= const_name("Literal[")
290+
= const_name("typing.Literal[")
285291
+ pybind11::detail::concat(const_name(sanitize_string_literal<Literals>().name)...)
286292
+ const_name("]");
287293
};

tests/test_callbacks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ def test_cpp_function_roundtrip():
140140
def test_function_signatures(doc):
141141
assert (
142142
doc(m.test_callback3)
143-
== "test_callback3(arg0: Callable[[typing.SupportsInt], int]) -> str"
143+
== "test_callback3(arg0: collections.abc.Callable[[typing.SupportsInt], int]) -> str"
144144
)
145145
assert (
146146
doc(m.test_callback4)
147-
== "test_callback4() -> Callable[[typing.SupportsInt], int]"
147+
== "test_callback4() -> collections.abc.Callable[[typing.SupportsInt], int]"
148148
)
149149

150150

@@ -231,7 +231,7 @@ def test_custom_func2():
231231
def test_callback_docstring():
232232
assert (
233233
m.test_tuple_unpacking.__doc__.strip()
234-
== "test_tuple_unpacking(arg0: Callable) -> object"
234+
== "test_tuple_unpacking(arg0: collections.abc.Callable) -> object"
235235
)
236236

237237

tests/test_docs_advanced_cast_custom.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ struct type_caster<user_space::Point2D> {
2323
// This macro inserts a lot of boilerplate code and sets the type hint.
2424
// `io_name` is used to specify different type hints for arguments and return values.
2525
// The signature of our negate function would then look like:
26-
// `negate(Sequence[float]) -> tuple[float, float]`
27-
PYBIND11_TYPE_CASTER(user_space::Point2D, io_name("Sequence[float]", "tuple[float, float]"));
26+
// `negate(collections.abc.Sequence[float]) -> tuple[float, float]`
27+
PYBIND11_TYPE_CASTER(user_space::Point2D,
28+
io_name("collections.abc.Sequence[float]", "tuple[float, float]"));
2829

2930
// C++ -> Python: convert `Point2D` to `tuple[float, float]`. The second and third arguments
3031
// are used to indicate the return value policy and parent object (for

tests/test_docs_advanced_cast_custom.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ def assert_negate_function(
2121

2222

2323
def test_negate(doc: SanitizedString) -> None:
24-
assert doc(m.negate) == "negate(arg0: Sequence[float]) -> tuple[float, float]"
24+
assert (
25+
doc(m.negate)
26+
== "negate(arg0: collections.abc.Sequence[float]) -> tuple[float, float]"
27+
)
2528
assert_negate_function([1.0, -1.0], (-1.0, 1.0))
2629
assert_negate_function((1.0, -1.0), (-1.0, 1.0))
2730
assert_negate_function([1, -1], (-1.0, 1.0))

tests/test_pytypes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ namespace detail {
155155

156156
template <>
157157
struct type_caster<RealNumber> {
158-
PYBIND11_TYPE_CASTER(RealNumber, io_name("Union[float, int]", "float"));
158+
PYBIND11_TYPE_CASTER(RealNumber, io_name("typing.Union[float, int]", "float"));
159159

160160
static handle cast(const RealNumber &number, return_value_policy, handle) {
161161
return py::float_(number.value).release();

0 commit comments

Comments
 (0)