-
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
Allow __doc__ to be settable on enums #2275
Comments
This is an interesting issue. I tried a few things and I'll quote myself.
Result:
|
At the moment pybind doesn't allow to overwrite docstrings of functions, methods or class/module attributes:
Although class and module |
Except that |
All prohibiting cases boils down to assign-to-property problem. This can be hacked using e.def_property_readonly_static("__doc__", [](py::object &) { return "test"; }); |
Do you mean that when pybind is declaring |
It turns out that overwriting class A:
doc = ""
class B(A):
# remove "-> Any" to make mypy happy
@property
def doc(self) -> Any: ... |
I agree. It doesn't seem right that mypy would be unhappy at that. |
@AWhetter Strictly speaking |
Issue filed on mypy python/mypy#9134. @AWhetter Do you think there is anything can be done about this issue on pybind11 side? |
Here's three options I can think of:
It's also worth noting that enums implement |
I've spent some time trying to make docstrings writable (the route 1 in your list). The enums were the easiest part and I came up with this implementation. Setter on first invocation removes property and assigns bare value. This can be improved further to zero or even negative overhead. I think I'll submit a PR next week. The second route seems to be too complex and non-generic to be part of main repo. The third option can be done right now on user side, but the syntax is a bit odd: PYBIND11_MODULE(numbers, m) {
py::enum_<Numbers> e(m, "Numbers");
e
.value("zero", zero)
.value("one", one)
.export_values()
;
std::string default_docstring = e.attr("__doc__");
e.def_property_readonly_static("__doc__", [default_docstring](py::object &self) { return ... ; });
}
I disagree. |
@sizmailov is right: docstrings are set on the type, and have got nothing to do with the state of the instance. |
Ah you're both right 🤦 |
I've been doing some quick digging, and found this:
Conclusion: the current issue that doesn't allow Routes forward, ranging from conservative to probably crazy:
Meta-conclusion: we need @wjakob's input as to how attached we should be to the old listing-of-enum-values behavior and its associated static read-only property to see a) how much this current is expected by users and b) what's an acceptable solution. |
Fixed via #2768 |
Issue description
Currently when declaring an enum, the
__doc__
attribute of the enum is not settable. However the__doc__
onobject()
is settable so it makes sense for the__doc__
of an enum to be settable also.The use case where this is coming up:
I'm generating type stubs of a pybind module using stubgen. When running mypy with the generated stubs I see the following error:
Potential Fix
I'm not sure what the right fix is here. I can of course make the
__doc__
property writable on all enum objects, but implementing that properly would mean adding a new attribute to enum objects and that seems wasteful.Reproducible example code
The text was updated successfully, but these errors were encountered: