6

Is there any way to document a nested class with Sphinx's autodoc plugin?

In:

class A:
    class B:
    """
    class B's documentation.
    """

    # ...

I want to use autoclass or something similar in my .rst file to document A.B specifically.

I tried:

.. currentmodule:: package.module

.. autoclass:: A.B

and

.. autoclass:: package.module.A.B

without success:

/path/to/file.rst:280: WARNING: autodoc: failed to import class 'B' from module 'package.module.A'; the following exception was raised:

...

Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/sphinx/ext/autodoc.py", line 335, in import_object
    __import__(self.modname)
ImportError: No module named 'package.module.A'; 'package.module' is not a package

Of course A is not a module; it seems like autoclass is considering anything before the last . as packages and modules.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
eepp
  • 7,255
  • 1
  • 38
  • 56
  • This isn't an answer to how to get Sphinx to do what you want, but nested classes are very uncommon in Python, so you might continue to find poor support for them. You could make your classes unnested and get Sphinx to work very easily. – Ned Batchelder Dec 06 '14 at 23:05
  • Yes, I know. Unfortunately, those nested classes are part of an already existing API. The next major version should unnest them, but in the meantime, I still have to document them. – eepp Dec 07 '14 at 00:21
  • I created a Sphinx [bug report](https://github.com/sphinx-doc/sphinx/issues/2820). – Brecht Machiels Jul 29 '16 at 13:18
  • We use nested classes all the time, both to keep the global namespace as clean as possible and to convey context. – Bob Kline Jul 22 '20 at 11:24

1 Answers1

8

Try:

.. autoclass:: package.module::A.B

Source: https://groups.google.com/forum/#!topic/sphinx-users/IL5V7HR1ZYE

Ethan Koenig
  • 116
  • 2
  • 3