How do I make sphinx understand the documentation information in static methods? #707
-
Hi any and all, We decided to switch from pybind11 to nanobind. This is a growth pain question. I have a problem getting my sphinx documentation for static data and static methods working in nanobind. In pybind11, static methods produces nice documentation. You can see this comparing our stable new and stable documentation. This page gives the method Note that if I go into I am wondering if there are workarounds here so that sphinx can see the documentation? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
How do you list the classes and their members? Have you tried registering them explicitly, like so?
It's possible that Sphinx thinks that the static methods are data, not functions. |
Beta Was this translation helpful? Give feedback.
-
I have the same issue. I noticed that when I added a listener in sphinx in my def autodoc_process_handler(app, what, name, obj, options, lines, huh):
print(what, name, obj, lines, obj.__doc__)
def setup(app):
app.connect('autodoc-process-signature', autodoc_process_handler) sphinx gives me output like this:
but it is inferring incorrectly that the methods are attributes and thus gives no documentation. |
Beta Was this translation helpful? Give feedback.
-
My solution is also brittle, I wrote a script to parse, since my object hierarchy is flat enough: import inspect
class AutodocCollector:
entries = []
def __init__(self, module, dunder=False):
for key in dir(module):
thing = getattr(module, key)
if key.startswith('__') and not dunder:
continue
if 'nanobind.nb_func' in str(type(thing)):
self.parse_func(thing)
elif 'enum.EnumType' in str(type(thing)):
self.parse_enum(thing)
elif 'nanobind.nb_type_0' in str(type(thing)):
self.parse_class(thing)
else:
print(key, type(thing), thing.__name__, inspect.getmembers(thing))
def add_entry(self, e):
self.entries.append(e)
def to_file(self, path, title):
with open(path, 'w') as fp:
fp.write(title+'\n')
fp.write('='*len(title)+'\n\n')
fp.write('\n\n'.join(self.entries))
def parse_func(self, thing):
mother = inspect.getmodule(thing)
self.add_entry(f'.. autofunction:: {mother.__name__}.{thing.__name__}\n\n :teqpflsh:`{thing.__name__}`')
def parse_enum(self, thing):
mother = inspect.getmodule(thing)
self.add_entry(f'.. autoclass:: {mother.__name__}.{thing.__name__}\n :undoc-members:\n :members:\n :show-inheritance:\n\n :teqpflsh:`{thing.__name__}`')
def parse_class(self, thing):
mother = inspect.getmodule(thing)
members = inspect.getmembers(thing)
e = f'.. autoclass:: {mother.__name__}.{thing.__name__}\n :show-inheritance:'
e += f'\n\n C++ docs: :teqpflsh:`{thing.__name__}`'
for name, member in members:
if name.startswith('__'): continue
if isinstance(member, property):
e += '\n\n' + ' '*4 + f'.. autoproperty:: {name}\n'
else:
e += '\n\n' + ' '*4 + f'.. automethod:: {name}\n'
self.add_entry(e)
if __name__ == '__main__':
import teqpflsh
ac = AutodocCollector(teqpflsh._teqpflsh_impl)
ac.to_file('api/teqpflsh.rst', title='teqpflsh Package') |
Beta Was this translation helpful? Give feedback.
How do you list the classes and their members? Have you tried registering them explicitly, like so?
It's possible that Sphinx thinks that the static methods are data, not functions.