简体   繁体   English

使 Sphinx autodoc 包含“隐藏”类

[英]Getting Sphinx autodoc to include 'hidden' classes

We have a module, in which each class file is in a file starting with an underscore.我们有一个模块,其中每个类文件都在一个以下划线开头的文件中。 In the init .py, we import these files, and we expose constructor functions for each class (this is necessary as each class constructor needs some additional state that we don't want users of the module to need to bother with).init .py 中,我们导入这些文件,并为每个类公开构造函数(这是必要的,因为每个类构造函数都需要一些我们不希望模块用户需要打扰的附加状态)。

Eg we may have _foo.py with:例如,我们可能有 _foo.py:

class Foo(object):
  def __init(context, params):
    ...

and in init .py we have:init .py 中我们有:

from ._foo import Foo as _Foo

def foo(params):
  return _Foo(_getContext(), params)

I'm trying to generate documentation with Sphinx autodoc for this.我正在尝试为此使用 Sphinx autodoc 生成文档。 I obviously want documentation to be generated for Foo and its methods (although preferably not its constructor), as well as for the foo() constructor wrapper.我显然希望为 Foo 及其方法(尽管最好不是它的构造函数)以及 foo() 构造函数包装器生成文档。

I can get Sphinx to generate docs for foo() but can't seem to get it to generate anything for Foo.我可以让 Sphinx 为 foo() 生成文档,但似乎无法让它为 Foo 生成任何内容。 I've tried:我试过了:

.. automodule:: foo_module
    :members:

as well as:也:

.. autoclass:: foo_module.Foo
    :members:

but to no avail.但无济于事。 The former just includes foo();前者只包括 foo(); the latter gives me an error:后者给了我一个错误:

index.rst:17: WARNING: autodoc: failed to import class u'Foo' from module u'foo_module'; the following exception was raised:
Traceback (most recent call last):
  File "/python2.7/site-packages/sphinx/ext/autodoc.py", line 342, in import_object
    obj = self.get_attr(obj, part)
  File "/python2.7/site-packages/sphinx/ext/autodoc.py", line 241, in get_attr
    return safe_getattr(obj, name, *defargs)
  File "/python2.7/site-packages/sphinx/util/inspect.py", line 114, in safe_getattr
    raise AttributeError(name)
AttributeError: Foo

Is there a way around this?有没有解决的办法?

Long ago, but...很久以前,但是...

Just use the directive option :private-members: as well.只需使用指令选项:private-members:

From https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html来自https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM