-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] "__doc__" attribute of static method is not set. #2403
Comments
I'm not at my computer right now, but I think this is a duplicate of a very old bug report. I might be wrong. |
This seems quite complex too me. Wouldn't this work just as well? template <typename Func, typename... Extra> class_ &
def_static(const char *name_, Func &&f, const Extra&... extra) {
static_assert(!std::is_member_function_pointer<Func>::value,
"def_static(...) called with a non-static member function pointer");
cpp_function cf(std::forward<Func>(f), name(name_), scope(*this),
sibling(getattr(*this, name_, none())), extra...);
auto static_cf = staticmethod(cf);
if (py::hasattr(cf, "__doc__"))
cf.attr("__doc__") = static_cf.attr("__doc__");
attr(cf.name()) = static_cf;
return *this;
} But I'd still like to figure out why |
@pkerichang, is this on Python 2 or Python 3 or both, btw? |
Hello, I only tested on Python 3 (3.7 FYI). as for using |
Hi @pkerichang. I can't reproduce this, though. What version of pybind11 are you on? I just tried this: #include <pybind11/pybind11.h>
namespace py = pybind11;
struct X {
static bool f(int x, float b) { return true; }
};
PYBIND11_MODULE(example, m)
{
py::class_<X>(m, "X")
.def_static("f", &X::f, "This is an amazing docstring");
} $ python3.7
Python 3.7.6 (default, Dec 19 2019, 23:50:13)
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import example
>>> example.X
<class 'example.X'>
>>> example.X.f
<built-in method f of PyCapsule object at 0x7f97e8638f30>
>>> example.X.f.__doc__
'f(arg0: int, arg1: float) -> bool\n\nThis is an amazing docstring\n' And
Am I doing something different than you are? |
Ah I figure out the problem, my bad, this is not a bug after all. In python, if I'm closing this issue now, thanks for all the quick responses! |
Yes, the But glad to hear I'm not going crazy and it's not a bug in pybind11; thanks! :-) |
The staticmethod wrapper class makes pybind11-generated static methods behave similarly to Python's static methods, which is a great improvement. However, for some reason the docstring of the underlying function is not passed to the staticmethod.
To reproduce, simply define a static method with any docstring (such as "Hello World!"), build the pybind11 library and import in python, then you'll see the docstring is no longer there when you call the
help()
method on the static method object.After some trial and error, modifying the
def_static()
method inpybind11.h
as follows solves the issue:I'm hesitant to submit a PR as I don't know if this is the proper way to set the doc string of a staticmethod (I can't find any documentations on manipulating Python objects in C/C++ online).
If this turns out to be a good fix then I can submit a PR. Otherwise, just letting you know this bug exists. Having good docstring is important to me as I use a stub generation script (similar to the one in Mypy object) to generator python stub files for pybind11 libraries to make IDE programming easier.
The text was updated successfully, but these errors were encountered: