Add type hints for args and kwargs (#5357)

* Allow subclasses of args and kwargs

The current implementation disallows subclasses of args and kwargs

* Added object type hint to args and kwargs

* Added type hinted args and kwargs classes

* Changed default type hint to typing.Any

* Removed args and kwargs type hint

* Updated tests

Modified the tests from #5381 to use the real Args and KWArgs classes

* Added comment
This commit is contained in:
gentlegiantJGC 2024-11-11 22:51:01 +00:00 committed by GitHub
parent a90e2af88d
commit 6d98d4d8d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 22 additions and 22 deletions

View File

@ -1012,10 +1012,18 @@ template <>
struct handle_type_name<args> { struct handle_type_name<args> {
static constexpr auto name = const_name("*args"); static constexpr auto name = const_name("*args");
}; };
template <typename T>
struct handle_type_name<Args<T>> {
static constexpr auto name = const_name("*args: ") + make_caster<T>::name;
};
template <> template <>
struct handle_type_name<kwargs> { struct handle_type_name<kwargs> {
static constexpr auto name = const_name("**kwargs"); static constexpr auto name = const_name("**kwargs");
}; };
template <typename T>
struct handle_type_name<KWArgs<T>> {
static constexpr auto name = const_name("**kwargs: ") + make_caster<T>::name;
};
template <> template <>
struct handle_type_name<obj_attr_accessor> { struct handle_type_name<obj_attr_accessor> {
static constexpr auto name = const_name<obj_attr_accessor>(); static constexpr auto name = const_name<obj_attr_accessor>();

View File

@ -2226,6 +2226,18 @@ class kwargs : public dict {
PYBIND11_OBJECT_DEFAULT(kwargs, dict, PyDict_Check) PYBIND11_OBJECT_DEFAULT(kwargs, dict, PyDict_Check)
}; };
// Subclasses of args and kwargs to support type hinting
// as defined in PEP 484. See #5357 for more info.
template <typename T>
class Args : public args {
using args::args;
};
template <typename T>
class KWArgs : public kwargs {
using kwargs::kwargs;
};
class anyset : public object { class anyset : public object {
public: public:
PYBIND11_OBJECT(anyset, object, PyAnySet_Check) PYBIND11_OBJECT(anyset, object, PyAnySet_Check)

View File

@ -14,26 +14,6 @@
#include <utility> #include <utility>
// Classes needed for subclass test.
class ArgsSubclass : public py::args {
using py::args::args;
};
class KWArgsSubclass : public py::kwargs {
using py::kwargs::kwargs;
};
namespace pybind11 {
namespace detail {
template <>
struct handle_type_name<ArgsSubclass> {
static constexpr auto name = const_name("*Args");
};
template <>
struct handle_type_name<KWArgsSubclass> {
static constexpr auto name = const_name("**KWArgs");
};
} // namespace detail
} // namespace pybind11
TEST_SUBMODULE(kwargs_and_defaults, m) { TEST_SUBMODULE(kwargs_and_defaults, m) {
auto kw_func auto kw_func
= [](int x, int y) { return "x=" + std::to_string(x) + ", y=" + std::to_string(y); }; = [](int x, int y) { return "x=" + std::to_string(x) + ", y=" + std::to_string(y); };
@ -345,7 +325,7 @@ TEST_SUBMODULE(kwargs_and_defaults, m) {
// Test support for args and kwargs subclasses // Test support for args and kwargs subclasses
m.def("args_kwargs_subclass_function", m.def("args_kwargs_subclass_function",
[](const ArgsSubclass &args, const KWArgsSubclass &kwargs) { [](const py::Args<std::string> &args, const py::KWArgs<std::string> &kwargs) {
return py::make_tuple(args, kwargs); return py::make_tuple(args, kwargs);
}); });
} }

View File

@ -20,7 +20,7 @@ def test_function_signatures(doc):
) )
assert ( assert (
doc(m.args_kwargs_subclass_function) doc(m.args_kwargs_subclass_function)
== "args_kwargs_subclass_function(*Args, **KWArgs) -> tuple" == "args_kwargs_subclass_function(*args: str, **kwargs: str) -> tuple"
) )
assert ( assert (
doc(m.KWClass.foo0) doc(m.KWClass.foo0)