Question

When using sphinx autodoc extension to create documentation how can I document a class instance (not the class itself) as if it was a function? Class has a __call__ method defined.

Était-ce utile?

La solution

It is possible to use sphinx extensions to fix the problem. For me the following was enough:

  1. in conf.py:

    1. add appropriate path to sys.path in conf.py
    2. replace sphinx.ext.autodoc with new module name in extensions list
  2. in new module:

    1. import sphinx.ext.autodoc
    2. create a subclass of .autodoc.FunctionDocumenter
    3. create setup(app) function which calls .autodoc.setup() and then calls app.add_autodocumenter(SubclassName)

Note that this will replace documenter for functions, thus you will need to make it useful for them. It is possible to avoid the issue by defining objtype class constant with unique string as a value, but this will require some more job then just that. Caring about regular functions is easier.

Code is here if anybody is interested.

Autres conseils

I'm not familiar with autodoc specifically, but you can try this:

class MyClass(object):
    ...
    def __call__(self):
        ...

c = MyClass()
"""
this is a function-like object.
"""

c2 = MyClass()
"""
this is another function-like object.
"""

Or this:

#: this is a function-like object.
c = MyClass()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top