Correct way to add __class_getitem__
/ allow subscripting of type for purpose of type-hinting?
#4841
Unanswered
randolf-scholz
asked this question in
Q&A
Replies: 1 comment
-
So I came up with the following solution: template <typename... T>
void add_class_getitem_genericalias(py::class_<T...> &cls) {
static const py::type GenericAlias = py::module_::import("types").attr("GenericAlias");
cls.attr("__class_getitem__") = PyClassMethod_New(
py::cpp_function(
[](
const py::type &cls,
const py::args &args
){
return GenericAlias(cls, *args);
},
GenericAlias.attr("__doc__").cast<std::string>().c_str()
).ptr()
);
} which seems to work fine, but I noted that #1598 may be relevant, I observed segfault on exit for the related version for callable: template <typename... T>
void add_class_getitem_callable(py::class_<T...> &cls) {
static const py::type Callable = py::module_::import("collections.abc").attr("Callable");
static const py::function class_getitem = Callable.attr("__class_getitem__");
cls.attr("__class_getitem__") = PyClassMethod_New(
py::cpp_function(
[](
const py::type &cls,
const py::args &args
){
// py::function class_getitem = Callable.attr("__class_getitem__");
return class_getitem(*args);
},
// class_getitem.attr("__doc__").cast<std::string>().c_str()
Callable.attr("__class_getitem__").attr("__doc__").cast<std::string>().c_str()
).ptr()
);
} Empirically, the segfaults on exit appear to not happen if only |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am working on some type hints for
pytorch
, but an issue is that some of the C++ types likeScriptList
(exported in https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/python/python_list.cpp) do not allow subscripting:I couldn't find a tutorial on this, it seems
classmethod
is not supported (#1693). A common way to add it, if one is not subclassingtyping.Generic
, is to add__class_getitem__ = classmethod(types.GenericAlias)
.Also, I noticed that in https://github.com/bl-sdk/pyunrealsdk/blob/56fa4298822425d2517ccb5e4b387b9cc45e405b/src/pyunrealsdk/unreal_bindings/wrapped_array.cpp#L195, the
PyClassMethod_New
is wrapped inpy::reinterpret_borrow<py::object>
; what could that be for?Beta Was this translation helpful? Give feedback.
All reactions