How to make Sphinx Respect Importing Classes Into Package with __init__.py

后端 未结 1 1106
旧巷少年郎
旧巷少年郎 2020-11-28 15:55

I have a package:

  • foo
    • foo.py
    • bar.py
    • __init__.py

foo.py has a class Foo. In __init__.py I import class Foo

相关标签:
1条回答
  • 2020-11-28 16:35

    The value of the __module__ attribute is the name of the module in which a class/function/method was defined (see https://docs.python.org/2.7/reference/datamodel.html). The attribute is writable so it can be redefined in __init__.py:

    Foo.__module__ = "foo"
    

    Now if you use .. automodule:: foo, the qualified name of the Foo class will be shown as foo.Foo in the generated module documentation.


    As an alternative to __module__-fiddling, you can use autoclass instead of automodule.

    .. autoclass:: foo.Foo will produce the wanted output.

    0 讨论(0)
提交回复
热议问题