Add docstrings for static fields and properties#308
Add docstrings for static fields and properties#308markuspi wants to merge 4 commits intopybind:mainfrom
Conversation
| self, path: QualifiedName, class_: type, obj: Any | ||
| ) -> Docstring | Alias | Class | list[Method] | Field | Property | None: | ||
| result = super().handle_class_member(path, class_, obj) | ||
| if isinstance(result, Field): |
There was a problem hiding this comment.
Is there any specific reason static properties fall under Field in the class processor? Without too much in-depth analysis, this looks like a bug that we are trying to circumvent here.
There was a problem hiding this comment.
I guess since class-level/static properties don't really exist in plain python, pybind11-stubgen's Property might not be suited since it is rendered using @property. So Field might be the next-best choice.
ParserDispatchMixin._iter_module_members mimics inspect.getmembers (before #238 it directly used inspect.getmembers), which already resolves def_property_readonly_static and def_property_static to their final values, thus losing the information about a descriptor. Presumably, we could change the logic of ParserDispatchMixin._iter_module_members to mimic inspect.getmembers_static, but I don't know which problems this will cause with other function types.
There was a problem hiding this comment.
They are usually done as a combo of @classmethod and @Property decorators in plain Python. I don't know what pybind11 uses internally for those (as alternatively, this can be a metaclass as well). I think we can leave this as is, but a comment in the code explaining this logic (why we check against Field) would be nice.
|
|
Before cpython 3.12, the documentation for static properties is not accessiblethrough pybind. In those cases, my code would just skip the inner if-statement and effectively be a no-op. Is this behavior in line with this project's policy on version compatibility? |
Yes, it is clear now. Thank you. |
This PR extracts docstrings from static fields defined via
def_property_readonly_staticanddef_property_static.This is based on the observation that the docstring for these fields is reachable from Python:
For each
Fielddetected by pybind11-stubgen,FixMissingFieldDocStringchecks whether__dict__[...]is different fromobj, which should only be the case for static properties. It then tries to use the dcostring in__doc__.Version compatibility
This approach requires cpython 3.12+ as it relies on python/cpython#23205 and pybind11 2.10.1+ due to pybind/pybind11#4168