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.
Asked
Active
Viewed 384 times
1
2 Answers
1
It is possible to use sphinx extensions to fix the problem. For me the following was enough:
in
conf.py
:- add appropriate path to
sys.path
inconf.py
- replace
sphinx.ext.autodoc
with new module name inextensions
list
- add appropriate path to
in new module:
- import
sphinx.ext.autodoc
- create a subclass of
.autodoc.FunctionDocumenter
- create
setup(app)
function which calls.autodoc.setup()
and then callsapp.add_autodocumenter(SubclassName)
- import
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.

ZyX
- 52,536
- 7
- 114
- 135
-
1Be careful, however, when also using `sphinx.ext.autosummary`: Since Sphinx 1.2.1, it [overwrites custom-set documenters](https://github.com/sphinx-doc/sphinx/commit/574a796) with the default ones. A solution is to set the documenter in the "env-before-read-docs" event, which is called after the autosummary overwrite. An example can be seen [here](https://github.com/F30/salt/blob/autodoc-fix/doc/_ext/saltautodoc.py). – F30 Sep 07 '16 at 11:42
0
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()

shx2
- 61,779
- 13
- 130
- 153
-
It produces things like `powerline.segments.vim.repository_status =
`. Documentation attached is right, but it should produce `(*args, **kwargs)`, not `= – ZyX Mar 16 '13 at 21:04`.