我如何使用 Sphinx' 私有方法的 Autodoc 扩展?
我正在使用 Sphinx 来记录我的 python 项目。 我启用了 autodoc 扩展,并在我的文档中包含以下内容。
.. autoclass:: ClassName
:members:
问题是,它只记录类中的非私有方法。 我如何也包含私有方法?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您使用的是 sphinx 1.1 或更高版本,请从 sphinx 文档站点 http: //www.sphinx-doc.org/en/master/ext/autodoc.html,
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,
您可以将其添加到
conf.py
文件中:对于 sphinx <= 1.8
1.8 更新,感谢@partofthething。
You can add this to
conf.py
file:For sphinx <= 1.8
1.8 update thanks to @partofthething.
解决这个问题的一种方法是明确强制 Sphinx 记录私有成员。 您可以通过将
automethod
附加到类级别文档的末尾来完成此操作: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:您是否尝试过使用自定义方法来确定成员是否应该包含在文档中,使用
autodoc-skip-member
?Have you tried using a custom method for determining whether a member should be included in the documentation, using
autodoc-skip-member
?查看 apidoc 代码,我们可以更改什么sphinx-apidoc 生成设置环境变量:
您还可以将此设置添加到您的 Makefile 中(如果您的包使用它):
Looking at apidoc code, we can change what sphinx-apidoc generate setting an environment variable:
You can also add this setup into your Makefile (if your package uses one):
除了上面的答案之外,如果您使用命令
sphinx-apidoc
只需添加 -P 标志即可添加私有成员,
例如,
有关更多选项和标志信息,请查看官方文档:
https ://www.sphinx-doc.org/en/master/man/sphinx-apidoc.html
Other than above answers, if you are using the command
sphinx-apidoc
just add the -P flag to add the private members,
for example
for more options and flags info check the official documentation:
https://www.sphinx-doc.org/en/master/man/sphinx-apidoc.html
不,私有意味着该类是私有的,并且不应该从公共 API 中使用它。 这并不意味着秘密,对于我们这些希望使用 sphinx 来完整记录类的人来说,排除私有方法是相当烦人的。
前面的答案是正确的。 您必须使用自定义方法,因为 Sphinx 目前不支持 autodoc 与私有方法结合使用。
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.
如果您只想记录特定的私有方法,而不是全部,则可以对每个方法使用
automethod
指令,而不是:private-members:
。我经常使用与普通公共方法同名的前导下划线方法,该方法具有函数的实际实现。 然后,公共方法对输入参数进行各种健全性检查。 下划线方法会跳过它们,因此可以调用它们以提高效率,但类型安全性较低。
例如(对@cmcginty盗用他们的示例表示歉意)
然后记录
_fly_to
,而不是_evaporate
,你可以这样做: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)
Then to document
_fly_to
, but not_evaporate
, you can do:这里有一个提示:想象一下 private 意味着“秘密”。
这就是为什么 Sphinx 不会记录它们。
如果您的意思不是“秘密”,请考虑更改他们的名字。 一般情况下避免使用单前导下划线名称; 除非你有理由对实施保密,否则它没有帮助。
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.