Skip to content

Commit 42cda75

Browse files
Fix *args/**kwargs return types. Add type hinting to py::make_tuple (#5881)
* Type hint make_tuple / fix *args/**kwargs return type Signed-off-by: Michael Carlstrom <[email protected]> * add back commented out panic * ignore return std move clang Signed-off-by: Michael Carlstrom <[email protected]> * fix for mingmw Signed-off-by: Michael Carlstrom <[email protected]> * added missing case Signed-off-by: Michael Carlstrom <[email protected]> --------- Signed-off-by: Michael Carlstrom <[email protected]>
1 parent 8ecf10e commit 42cda75

File tree

6 files changed

+54
-30
lines changed

6 files changed

+54
-30
lines changed

include/pybind11/cast.h

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,21 +1468,24 @@ template <>
14681468
struct handle_type_name<weakref> {
14691469
static constexpr auto name = const_name("weakref.ReferenceType");
14701470
};
1471+
// args/Args/kwargs/KWArgs have name as well as typehint included
14711472
template <>
14721473
struct handle_type_name<args> {
1473-
static constexpr auto name = const_name("*args");
1474+
static constexpr auto name = io_name("*args", "tuple");
14741475
};
14751476
template <typename T>
14761477
struct handle_type_name<Args<T>> {
1477-
static constexpr auto name = const_name("*args: ") + make_caster<T>::name;
1478+
static constexpr auto name
1479+
= io_name("*args: ", "tuple[") + make_caster<T>::name + io_name("", ", ...]");
14781480
};
14791481
template <>
14801482
struct handle_type_name<kwargs> {
1481-
static constexpr auto name = const_name("**kwargs");
1483+
static constexpr auto name = io_name("**kwargs", "dict[str, typing.Any]");
14821484
};
14831485
template <typename T>
14841486
struct handle_type_name<KWArgs<T>> {
1485-
static constexpr auto name = const_name("**kwargs: ") + make_caster<T>::name;
1487+
static constexpr auto name
1488+
= io_name("**kwargs: ", "dict[str, ") + make_caster<T>::name + io_name("", "]");
14861489
};
14871490
template <>
14881491
struct handle_type_name<obj_attr_accessor> {
@@ -1908,13 +1911,20 @@ inline cast_error cast_error_unable_to_convert_call_arg(const std::string &name,
19081911
}
19091912
#endif
19101913

1914+
namespace typing {
1915+
template <typename... Types>
1916+
class Tuple : public tuple {
1917+
using tuple::tuple;
1918+
};
1919+
} // namespace typing
1920+
19111921
template <return_value_policy policy = return_value_policy::automatic_reference>
1912-
tuple make_tuple() {
1922+
typing::Tuple<> make_tuple() {
19131923
return tuple(0);
19141924
}
19151925

19161926
template <return_value_policy policy = return_value_policy::automatic_reference, typename... Args>
1917-
tuple make_tuple(Args &&...args_) {
1927+
typing::Tuple<Args...> make_tuple(Args &&...args_) {
19181928
constexpr size_t size = sizeof...(Args);
19191929
std::array<object, size> args{{reinterpret_steal<object>(
19201930
detail::make_caster<Args>::cast(std::forward<Args>(args_), policy, nullptr))...}};
@@ -1933,7 +1943,12 @@ tuple make_tuple(Args &&...args_) {
19331943
for (auto &arg_value : args) {
19341944
PyTuple_SET_ITEM(result.ptr(), counter++, arg_value.release().ptr());
19351945
}
1946+
PYBIND11_WARNING_PUSH
1947+
#ifdef PYBIND11_DETECTED_CLANG_WITH_MISLEADING_CALL_STD_MOVE_EXPLICITLY_WARNING
1948+
PYBIND11_WARNING_DISABLE_CLANG("-Wreturn-std-move")
1949+
#endif
19361950
return result;
1951+
PYBIND11_WARNING_POP
19371952
}
19381953

19391954
/// \ingroup annotations

include/pybind11/detail/init.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,15 @@ template <typename Get,
501501
typename NewInstance,
502502
typename ArgState>
503503
struct pickle_factory<Get, Set, RetState(Self), NewInstance(ArgState)> {
504-
static_assert(std::is_same<intrinsic_t<RetState>, intrinsic_t<ArgState>>::value,
505-
"The type returned by `__getstate__` must be the same "
506-
"as the argument accepted by `__setstate__`");
504+
using Ret = intrinsic_t<RetState>;
505+
using Arg = intrinsic_t<ArgState>;
506+
507+
// Subclasses are now allowed for support between type hint and generic versions of types
508+
// (e.g.) typing::List <--> list
509+
static_assert(std::is_same<Ret, Arg>::value || std::is_base_of<Ret, Arg>::value
510+
|| std::is_base_of<Arg, Ret>::value,
511+
"The type returned by `__getstate__` must be the same or subclass of the "
512+
"argument accepted by `__setstate__`");
507513

508514
remove_reference_t<Get> get;
509515
remove_reference_t<Set> set;

include/pybind11/pybind11.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ inline std::string generate_function_signature(const char *type_caster_name_fiel
120120
const auto c = *pc;
121121
if (c == '{') {
122122
// Write arg name for everything except *args and **kwargs.
123-
is_starred = *(pc + 1) == '*';
123+
// Detect {@*args...} or {@**kwargs...}
124+
is_starred = *(pc + 1) == '@' && *(pc + 2) == '*';
124125
if (is_starred) {
125126
continue;
126127
}

include/pybind11/typing.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ PYBIND11_NAMESPACE_BEGIN(typing)
3434
There is no additional enforcement of types at runtime.
3535
*/
3636

37-
template <typename... Types>
38-
class Tuple : public tuple {
39-
using tuple::tuple;
40-
};
37+
// Tuple type hint defined in cast.h for use in py::make_tuple to avoid circular includes
4138

4239
template <typename K, typename V>
4340
class Dict : public dict {

tests/test_factory_constructors.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,14 @@ TEST_SUBMODULE(factory_constructors, m) {
376376
py::print("noisy placement new");
377377
return p;
378378
}
379-
static void operator delete(void *p, size_t) {
379+
static void operator delete(void *p) noexcept {
380380
py::print("noisy delete");
381381
::operator delete(p);
382382
}
383+
static void operator delete(void *p, size_t) {
384+
py::print("noisy delete size");
385+
::operator delete(p);
386+
}
383387
static void operator delete(void *, void *) { py::print("noisy placement delete"); }
384388
};
385389

tests/test_kwargs_and_defaults.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ def test_function_signatures(doc):
3434
)
3535
assert doc(m.args_function) == "args_function(*args) -> tuple"
3636
assert (
37-
doc(m.args_kwargs_function) == "args_kwargs_function(*args, **kwargs) -> tuple"
37+
doc(m.args_kwargs_function)
38+
== "args_kwargs_function(*args, **kwargs) -> tuple[tuple, dict[str, typing.Any]]"
3839
)
3940
assert (
4041
doc(m.args_kwargs_subclass_function)
41-
== "args_kwargs_subclass_function(*args: str, **kwargs: str) -> tuple"
42+
== "args_kwargs_subclass_function(*args: str, **kwargs: str) -> tuple[tuple[str, ...], dict[str, str]]"
4243
)
4344
assert (
4445
doc(m.KWClass.foo0)
@@ -138,7 +139,7 @@ def test_mixed_args_and_kwargs(msg):
138139
msg(excinfo.value)
139140
== """
140141
mixed_plus_args(): incompatible function arguments. The following argument types are supported:
141-
1. (arg0: typing.SupportsInt | typing.SupportsIndex, arg1: typing.SupportsFloat | typing.SupportsIndex, *args) -> tuple
142+
1. (arg0: typing.SupportsInt | typing.SupportsIndex, arg1: typing.SupportsFloat | typing.SupportsIndex, *args) -> tuple[int, float, tuple]
142143
143144
Invoked with: 1
144145
"""
@@ -149,7 +150,7 @@ def test_mixed_args_and_kwargs(msg):
149150
msg(excinfo.value)
150151
== """
151152
mixed_plus_args(): incompatible function arguments. The following argument types are supported:
152-
1. (arg0: typing.SupportsInt | typing.SupportsIndex, arg1: typing.SupportsFloat | typing.SupportsIndex, *args) -> tuple
153+
1. (arg0: typing.SupportsInt | typing.SupportsIndex, arg1: typing.SupportsFloat | typing.SupportsIndex, *args) -> tuple[int, float, tuple]
153154
154155
Invoked with:
155156
"""
@@ -183,7 +184,7 @@ def test_mixed_args_and_kwargs(msg):
183184
msg(excinfo.value)
184185
== """
185186
mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
186-
1. (i: typing.SupportsInt | typing.SupportsIndex = 1, j: typing.SupportsFloat | typing.SupportsIndex = 3.14159, *args, **kwargs) -> tuple
187+
1. (i: typing.SupportsInt | typing.SupportsIndex = 1, j: typing.SupportsFloat | typing.SupportsIndex = 3.14159, *args, **kwargs) -> tuple[int, float, tuple, dict[str, typing.Any]]
187188
188189
Invoked with: 1; kwargs: i=1
189190
"""
@@ -194,7 +195,7 @@ def test_mixed_args_and_kwargs(msg):
194195
msg(excinfo.value)
195196
== """
196197
mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
197-
1. (i: typing.SupportsInt | typing.SupportsIndex = 1, j: typing.SupportsFloat | typing.SupportsIndex = 3.14159, *args, **kwargs) -> tuple
198+
1. (i: typing.SupportsInt | typing.SupportsIndex = 1, j: typing.SupportsFloat | typing.SupportsIndex = 3.14159, *args, **kwargs) -> tuple[int, float, tuple, dict[str, typing.Any]]
198199
199200
Invoked with: 1, 2; kwargs: j=1
200201
"""
@@ -211,7 +212,7 @@ def test_mixed_args_and_kwargs(msg):
211212
msg(excinfo.value)
212213
== """
213214
args_kwonly(): incompatible function arguments. The following argument types are supported:
214-
1. (i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsFloat | typing.SupportsIndex, *args, z: typing.SupportsInt | typing.SupportsIndex) -> tuple
215+
1. (i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsFloat | typing.SupportsIndex, *args, z: typing.SupportsInt | typing.SupportsIndex) -> tuple[int, float, tuple, int]
215216
216217
Invoked with: 2, 2.5, 22
217218
"""
@@ -233,12 +234,12 @@ def test_mixed_args_and_kwargs(msg):
233234
)
234235
assert (
235236
m.args_kwonly_kwargs.__doc__
236-
== "args_kwonly_kwargs(i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsFloat | typing.SupportsIndex, *args, z: typing.SupportsInt | typing.SupportsIndex, **kwargs) -> tuple\n"
237+
== "args_kwonly_kwargs(i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsFloat | typing.SupportsIndex, *args, z: typing.SupportsInt | typing.SupportsIndex, **kwargs) -> tuple[int, float, tuple, int, dict[str, typing.Any]]\n"
237238
)
238239

239240
assert (
240241
m.args_kwonly_kwargs_defaults.__doc__
241-
== "args_kwonly_kwargs_defaults(i: typing.SupportsInt | typing.SupportsIndex = 1, j: typing.SupportsFloat | typing.SupportsIndex = 3.14159, *args, z: typing.SupportsInt | typing.SupportsIndex = 42, **kwargs) -> tuple\n"
242+
== "args_kwonly_kwargs_defaults(i: typing.SupportsInt | typing.SupportsIndex = 1, j: typing.SupportsFloat | typing.SupportsIndex = 3.14159, *args, z: typing.SupportsInt | typing.SupportsIndex = 42, **kwargs) -> tuple[int, float, tuple, int, dict[str, typing.Any]]\n"
242243
)
243244
assert m.args_kwonly_kwargs_defaults() == (1, 3.14159, (), 42, {})
244245
assert m.args_kwonly_kwargs_defaults(2) == (2, 3.14159, (), 42, {})
@@ -344,7 +345,7 @@ def test_positional_only_args():
344345
# Mix it with args and kwargs:
345346
assert (
346347
m.args_kwonly_full_monty.__doc__
347-
== "args_kwonly_full_monty(arg0: typing.SupportsInt | typing.SupportsIndex = 1, arg1: typing.SupportsInt | typing.SupportsIndex = 2, /, j: typing.SupportsFloat | typing.SupportsIndex = 3.14159, *args, z: typing.SupportsInt | typing.SupportsIndex = 42, **kwargs) -> tuple\n"
348+
== "args_kwonly_full_monty(arg0: typing.SupportsInt | typing.SupportsIndex = 1, arg1: typing.SupportsInt | typing.SupportsIndex = 2, /, j: typing.SupportsFloat | typing.SupportsIndex = 3.14159, *args, z: typing.SupportsInt | typing.SupportsIndex = 42, **kwargs) -> tuple[int, int, float, tuple, int, dict[str, typing.Any]]\n"
348349
)
349350
assert m.args_kwonly_full_monty() == (1, 2, 3.14159, (), 42, {})
350351
assert m.args_kwonly_full_monty(8) == (8, 2, 3.14159, (), 42, {})
@@ -394,23 +395,23 @@ def test_positional_only_args():
394395
def test_signatures():
395396
assert (
396397
m.kw_only_all.__doc__
397-
== "kw_only_all(*, i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsInt | typing.SupportsIndex) -> tuple\n"
398+
== "kw_only_all(*, i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsInt | typing.SupportsIndex) -> tuple[int, int]\n"
398399
)
399400
assert (
400401
m.kw_only_mixed.__doc__
401-
== "kw_only_mixed(i: typing.SupportsInt | typing.SupportsIndex, *, j: typing.SupportsInt | typing.SupportsIndex) -> tuple\n"
402+
== "kw_only_mixed(i: typing.SupportsInt | typing.SupportsIndex, *, j: typing.SupportsInt | typing.SupportsIndex) -> tuple[int, int]\n"
402403
)
403404
assert (
404405
m.pos_only_all.__doc__
405-
== "pos_only_all(i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsInt | typing.SupportsIndex, /) -> tuple\n"
406+
== "pos_only_all(i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsInt | typing.SupportsIndex, /) -> tuple[int, int]\n"
406407
)
407408
assert (
408409
m.pos_only_mix.__doc__
409-
== "pos_only_mix(i: typing.SupportsInt | typing.SupportsIndex, /, j: typing.SupportsInt | typing.SupportsIndex) -> tuple\n"
410+
== "pos_only_mix(i: typing.SupportsInt | typing.SupportsIndex, /, j: typing.SupportsInt | typing.SupportsIndex) -> tuple[int, int]\n"
410411
)
411412
assert (
412413
m.pos_kw_only_mix.__doc__
413-
== "pos_kw_only_mix(i: typing.SupportsInt | typing.SupportsIndex, /, j: typing.SupportsInt | typing.SupportsIndex, *, k: typing.SupportsInt | typing.SupportsIndex) -> tuple\n"
414+
== "pos_kw_only_mix(i: typing.SupportsInt | typing.SupportsIndex, /, j: typing.SupportsInt | typing.SupportsIndex, *, k: typing.SupportsInt | typing.SupportsIndex) -> tuple[int, int, int]\n"
414415
)
415416

416417

0 commit comments

Comments
 (0)