-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Do not set docstring for function when it's empty #2745
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
Conversation
cf33e61
to
abe59a9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interferes with situations where users want to manually add docstring to the bound function later directly through the Python C API
Could you elaborate on this? How exactly does it interfere?
Just curious to know what you mean. Apart from that, obviously, I'm not against not doing work when it's not necessary, so this looks good to me :-)
I find it difficult to extend this to work with pybind11-bound functions without touching pybind11's implementation details
Agreed. I hope that on the medium/long-term, pybind11 would allow a more fine-grained control over customizing this.
I have been working on a series of pull requests for PyTorch and one of them (pytorch/pytorch#49771) is replacing legacy manual bindings of Previously, With this pull request, the |
That's very elaborate, thanks! :-)
And yep, I had completely forgotten about this! Ofc, you could set the
Yep, though expecting that pybind11 will create |
Will this is actually impossible :( Trying to set the BTW I just pushed another commit to remove the redundant pointer check logic. Now it looks cleaner :) |
Argh, yes, sorry. I should have known that. Those silly C-function wrappers.
I saw; thanks! Looks good, now :-) I had already approved, but I'll invite a few others, to see if we can get one or a few more sanity checks :-) |
include/pybind11/pybind11.h
Outdated
std::free(const_cast<char *>(func->m_ml->ml_doc)); | ||
func->m_ml->ml_doc = strdup(signatures.c_str()); | ||
std::free(const_cast<char *>(func->m_ml->ml_doc)); | ||
func->m_ml->ml_doc = nullptr; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn’t this be in an else branch below the next line? Or maybe oven better, make it a ternary operator?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I just made it a ternary operator... and I almost forget that ternary operator is lazily evaluated like the if statement in C++. Haven't been writing C++ for some time...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, as-is or with the ternary operator as suggested by Henry. Thanks Qifan!
Just pushed another commit to address @henryiii 's comment. If there's no more change needed I can squash the commits. |
We'll automatically squash when merging ;-) @henryiii, if there's nothing left, go ahead and push that button. |
We only need nice history if there's a reason to keep more than one commit for a PR, like a large one or one where a file changed names and also was edited. Thanks! |
Description
In pybind11, automatic docstring generated from function signature and user-provided documents can be suppressed by disabling all docstring-related options. However, this currently produces an empty docstring, which is unnecessary and interferes with situations where users want to manually add docstring to the bound function later directly through the Python C API (for example, PyTorch currently does this to its manual C bindings and I find it difficult to extend this to work with pybind11-bound functions without touching pybind11's implementation details). This pull request modifies this behavior, such that when all docstring-related options are disabled and the resulting docstring is empty, no docstring is set on the function.
Suggested changelog entry: