0

I'd like to document my class inheriting from a base, both of which are inside a submodule of a larger package. This package imports the classes to the very top level for a better user experience. For that reason I've also made my documentation on that level. Here's the gist of my situation.

# Directory structure
package
  __init__.py
  sub.py

# sub.py
class Foo:
    pass

class Bar(Foo):
    pass

# __init__.py
from .sub import Foo, Bar

# Some documentation file
.. currentmodule:: package
.. autoclass:: Foo
.. autoclass:: Bar
   :show-inheritance:

Other references to those classes work fine, functioning well with the package.Foo references instead of package.sub.Foo. But the show-inheritance option of autoclass displays the original path, along with a broken link. Moving the classes to __init__.py restores the link.

How could I produce a working link? As a bonus I'd like to have the shorter name as is in the class name already.

Doc


This is a related question, whose solution I tried out. Modifying the __module__ attribute of those classes works.

# __init__.py
Foo.__module__ = 'package'
Bar.__module__ = 'package'

However, I doubt the optimal solution is to do this manually for every class I've defined, most of which have some kind of inheritance going on.

Felix
  • 2,548
  • 19
  • 48
  • 1
    Related issue: https://github.com/sphinx-doc/sphinx/issues/5589 – mzjn May 05 '20 at 10:27
  • I've slowly begun to accept that modifying the module attribute is what is needed. It has one upside though, for a package which is used from the top level namespace but defined in submodules, it hides the directory structure quite well. But I'd still like an answer to the question whether it can be done some other way. – Felix May 07 '20 at 14:06

0 Answers0