mirror of
https://github.com/pybind/pybind11.git
synced 2026-05-24 23:07:00 +00:00
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:
@@ -1012,10 +1012,18 @@ template <>
|
||||
struct handle_type_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 <>
|
||||
struct handle_type_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 <>
|
||||
struct handle_type_name<obj_attr_accessor> {
|
||||
static constexpr auto name = const_name<obj_attr_accessor>();
|
||||
|
||||
@@ -2226,6 +2226,18 @@ class kwargs : public dict {
|
||||
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 {
|
||||
public:
|
||||
PYBIND11_OBJECT(anyset, object, PyAnySet_Check)
|
||||
|
||||
Reference in New Issue
Block a user