Sphinx doesn't generate docs for __init__(self) by default. I have tried the following:
.. automodule:: mymodule
:members:
and
..autoclass:: MyClass
:members:
In conf.py, setting the following only appends the __init__(self) docstring to the class docstring (the Sphinx autodoc documentation seems to agree that this is the expected behavior, but mentions nothing regarding the problem I'm trying to solve):
autoclass_content = 'both'
You were close. You can use the
autoclass_content
option in yourconf.py
file:Over the past years I've written several variants of
autodoc-skip-member
callbacks for various unrelated Python projects because I wanted methods like__init__()
,__enter__()
and__exit__()
to show up in my API documentation (after all, these "special methods" are part of the API and what better place to document them than inside the special method's docstring).Recently I took the best implementation and made it part of one of my Python projects (here's the documentation). The implementation basically comes down to this:
Yes, there's more documentation than logic :-). The advantage of defining an
autodoc-skip-member
callback like this over the use of thespecial-members
option (for me) is that thespecial-members
option also enables documentation of properties like__weakref__
(available on all new-style classes, AFAIK) which I consider noise and not useful at all. The callback approach avoids this (because it only works on functions/methods and ignores other attributes).Here are three alternatives:
To ensure that
__init__()
is always documented, you can useautodoc-skip-member
in conf.py. Like this:This explicitly defines
__init__
not to be skipped (which it is by default). This configuration is specified once, and it does not require any additional markup for every class in the .rst source.The
special-members
option was added in Sphinx 1.1. It makes "special" members (those with names like__special__
) be documented by autodoc.Since Sphinx 1.2, this option takes arguments which makes it more useful than it was previously.
Use
automethod
:This has to be added for every class (cannot be used with
automodule
, as pointed out in a comment to the first revision of this answer).