32

I am using Sphinx for documenting my python project. I have the autodoc extension enabled and have the following in my docs.

.. autoclass:: ClassName
   :members:

The problem is, it only documents the non-private methods in the class. How do I include the private methods too?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
cnu
  • 36,135
  • 23
  • 65
  • 63

9 Answers9

36

if you are using sphinx 1.1 or above, from the sphinx documentation site at http://www.sphinx-doc.org/en/master/ext/autodoc.html,

:special-members:
:private-members:
machnic
  • 2,304
  • 2
  • 17
  • 21
mariotomo
  • 9,438
  • 8
  • 47
  • 66
  • 7
    Note that you can make this a default for all classes by using the [`autodoc_default_flags`](http://sphinx.pocoo.org/ext/autodoc.html#confval-autodoc_default_flags) setting. – André Caron Aug 30 '12 at 17:50
17

You can add this to conf.py file:

autodoc_default_options = {
    "members": True,
    "undoc-members": True,
    "private-members": True
}

For sphinx <= 1.8

autodoc_default_flags = [
    'members',
    'undoc-members',
    'private-members',
    'special-members',
    'inherited-members',
    'show-inheritance'
]

1.8 update thanks to @partofthething.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
John
  • 170
  • 1
  • 5
  • 4
    Heads up, that was deprecated in Sphinx 1.8 and merged into `autodoc_default_options` so a new version would look more like: `autodoc_default_options = { "members": True, "undoc-members": True, "private-members": True }` https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#confval-autodoc_default_options – partofthething Jun 27 '20 at 00:00
10

One way to get around this is to explicitly force Sphinx to document private members. You can do this by appending automethod to the end of the class level docs:

class SmokeMonster(object):
   """
   A large smoke monster that protects the island.
   """
   def __init__(self,speed):
      """
      :param speed: Velocity in MPH of the smoke monster
      :type  speed: int

      .. document private functions
      .. automethod:: _evaporate
      """
      self.speed = speed

   def _evaporate(self):
      """
      Removes the smoke monster from reality. Not to be called by client.
      """
      pass
cmcginty
  • 113,384
  • 42
  • 163
  • 163
  • Just keep in mind that `.. document private functions` and `.. automethod:: _FUNC_NAME` should be placed wherever you want the output to be located. They do not have to be in the `__init__()` function of the related class. – dtlussier Nov 23 '11 at 23:34
  • thanks! also worked at the module level for private module function: IE `.. autofunction:: _my_private_module_function` in the module docstring and in my `.rst` file. Unfortunately `:private-members:` didn't work for module level private functions. I think it only works on classes. – Mark Mikofski Oct 07 '13 at 23:43
4

Have you tried using a custom method for determining whether a member should be included in the documentation, using autodoc-skip-member?

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191
4

Looking at apidoc code, we can change what sphinx-apidoc generate setting an environment variable:

export SPHINX_APIDOC_OPTIONS='members,special-members,private-members,undoc-members,show-inheritance'

You can also add this setup into your Makefile (if your package uses one):

docs:
    rm -rf docs/api
    SPHINX_APIDOC_OPTIONS='members,special-members,private-members,undoc-members,show-inheritance' sphinx-apidoc -o docs/api/ intellprice
    $(MAKE) -C docs clean
    $(MAKE) -C docs html
rodfersou
  • 914
  • 6
  • 10
3

Other than above answers, if you are using the command sphinx-apidoc

just add the -P flag to add the private members, for example

sphinx-apidoc -e -P -o docs/ local_Directory/

for more options and flags info check the official documentation:

https://www.sphinx-doc.org/en/master/man/sphinx-apidoc.html

Elie Eid
  • 703
  • 7
  • 11
2

No, private means private to the class and that it shouldn't be used from the public API. It's not meant to mean secret and for those of us wishing to use sphinx for full documentation of classes, excluding private methods is rather annoying.

The previous answer is correct. You will have to use a custom method as Sphinx does not currently support autodoc in conjunction with private methods.

2

If you only want to document specific private methods, not all of them, you can use the automethod directive for each one, rather than :private-members:.

I often use leading underscore methods with the same name as a normal public method, which has the actual implementation of a function. The public method then has various sanity checks about the input arguments. The underscore method skips them, so they can be called for improved efficiency, but with less type safety.

E.g. (with apologies to @cmcginty for stealing their example)

class SmokeMonster(object):
   """
   A large smoke monster that protects the island.
   """
   def __init__(self, speed, initial_position):
      """
      :param speed: Velocity in MPH of the smoke monster
      :param inital_position: Position of the smoke monster
      """
      self.speed = speed
      self.position = initial_position

   def _evaporate(self):
      """
      Removes the smoke monster from reality. Not to be called by client.
      """
      pass

   def fly_to(self, position):
      """
      Have the monster fly to the specified position.

      :param position: Desired location for the monster to fly to.
      """
      if not position.is_valid():
          raise ValueError("Invalid position: " + str(position))
      if not self.can_fly():
          raise RuntimeError("Smoke monster is not currently able to fly.")

      self._fly_to(position)

   def _fly_to(self, position):
      """Equivalent to :meth:`SmokeMonster.fly_to`, but without the safety checks.

      Not normally recommended for end users, but available if you need to
      improve efficiency of the `fly_to` call and you already know it is safe
      to call.
      """
      self.position = position

Then to document _fly_to, but not _evaporate, you can do:

.. autoclass:: SmokeMonster
    :members:

    .. automethod:: SmokeMonster._fly_to
Mike Jarvis
  • 889
  • 6
  • 17
-10

Here's a hint: imagine that private means "secret".

That's why Sphinx won't document them.

If you don't mean "secret", consider changing their names. Avoid using the single-leading-underscore name in general; it doesn't help unless you have a reason to keep the implementation secret.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • 1
    This seems backwards to what PEP-8 says about private. "If in doubt, choose non-public" http://www.python.org/dev/peps/pep-0008/ – svrist Aug 19 '09 at 19:31
  • @svrist: Not backwards -- that's the exact point. Sphinx won't document non-public. If you choose non-public, you don't get automatic documentation. If you want documentation, on the other hand, don't choose non-public. Here, "doubt" means you have a good reason for both and can't decide. If you don't have a *good* reason for non-public, you don't have "doubt", either. Make it public unless you have a *good* reason for non-public. – S.Lott Aug 19 '09 at 19:55
  • 1
    But what if you're using Sphinx for *internal* documentation? – Max Cantor Nov 30 '10 at 21:43
  • @Max: the rules don't change. "Avoid using the single-leading-underscore name in general". They're not "internal". They're "so secret they cannot be revealed." As in "for more information, read the source." – S.Lott Nov 30 '10 at 22:35
  • 6
    methods like `__add__`, `__getitem__` fall in the hidden category and renaming them is quite out of question. – mariotomo Oct 12 '11 at 12:34