简体   繁体   English

如何将 Sphinx 的 Autodoc 扩展用于私有方法?

[英]How can I use Sphinx' Autodoc-extension for private methods?

I am using Sphinx for documenting my python project.我正在使用 Sphinx 来记录我的 python 项目。 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?我如何也包含私有方法?

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 ,如果您使用的是 sphinx 1.1 或更高版本,请访问 http://www.sphinx-doc.org/en/master/ext/autodoc.html的 sphinx 文档站点,

:special-members:
:private-members:

您可以将其添加到conf.py文件中:

autodoc_default_flags = ['members', 'undoc-members', 'private-members', 'special-members', 'inherited-members', 'show-inheritance']

One way to get around this is to explicitly force Sphinx to document private members.解决此问题的一种方法是显式强制 Sphinx 记录私有成员。 You can do this by appending automethod to the end of the class level docs:您可以通过将automethod附加到类级别文档的末尾来做到这一点:

class SmokeMonster(object):
   """
   A large smoke monster that protects the island.
   """
   def __init__(self,speed):
      """
      :param speed: Velocity in MPH of the smoke monster
      :type  speed: int

      .. document private functions
      .. automethod:: _evaporate
      """
      self.speed = speed

   def _evaporate(self):
      """
      Removes the smoke monster from reality. Not to be called by client.
      """
      pass

您是否尝试过使用自定义方法来确定是否应将成员包含在文档中,使用autodoc-skip-member

Looking at apidoc code , we can change what sphinx-apidoc generate setting an environment variable:查看apidoc 代码,我们可以更改 sphinx-apidoc 生成的设置环境变量:

export SPHINX_APIDOC_OPTIONS='members,special-members,private-members,undoc-members,show-inheritance'

You can also add this setup into your Makefile (if your package uses one):您还可以将此设置添加到您的 Makefile 中(如果您的包使用一个):

docs:
    rm -rf docs/api
    SPHINX_APIDOC_OPTIONS='members,special-members,private-members,undoc-members,show-inheritance' sphinx-apidoc -o docs/api/ intellprice
    $(MAKE) -C docs clean
    $(MAKE) -C docs html

No, private means private to the class and that it shouldn't be used from the public API.不,私有意味着类私有,并且不应从公共 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.这并不意味着秘密,对于我们这些希望使用 sphinx 来完整记录类的人来说,不包括私有方法是相当烦人的。

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.您将不得不使用自定义方法,因为 Sphinx 目前不支持将 autodoc 与私有方法结合使用。

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: .如果您只想记录特定的私有方法,而不是全部,您可以为每个方法使用automethod指令,而不是: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.下划线方法跳过它们,因此可以调用它们以提高效率,但类型安全性较低。

Eg (with apologies to @cmcginty for stealing their example)例如(对@cmcginty 窃取他们的示例表示歉意)

class SmokeMonster(object):
   """
   A large smoke monster that protects the island.
   """
   def __init__(self, speed, initial_position):
      """
      :param speed: Velocity in MPH of the smoke monster
      :param inital_position: Position of the smoke monster
      """
      self.speed = speed
      self.position = initial_position

   def _evaporate(self):
      """
      Removes the smoke monster from reality. Not to be called by client.
      """
      pass

   def fly_to(self, position):
      """
      Have the monster fly to the specified position.

      :param position: Desired location for the monster to fly to.
      """
      if not position.is_valid():
          raise ValueError("Invalid position: " + str(position))
      if not self.can_fly():
          raise RuntimeError("Smoke monster is not currently able to fly.")

      self._fly_to(position)

   def _fly_to(self, position):
      """Equivalent to :meth:`SmokeMonster.fly_to`, but without the safety checks.

      Not normally recommended for end users, but available if you need to
      improve efficiency of the `fly_to` call and you already know it is safe
      to call.
      """
      self.position = position

Then to document _fly_to , but not _evaporate , you can do:然后记录_fly_to ,而不是_evaporate ,你可以这样做:

.. autoclass:: SmokeMonster
    :members:

    .. automethod:: SmokeMonster._fly_to

Other than above answers, if you are using the command sphinx-apidoc除了上述答案,如果您使用的是命令sphinx-apidoc

just add the -P flag to add the private members, for example只需添加 -P 标志即可添加私有成员,例如

sphinx-apidoc -e -P -o docs/ local_Directory/

for more options and flags info check the official documentation:有关更多选项和标志信息,请查看官方文档:

https://www.sphinx-doc.org/en/master/man/sphinx-apidoc.html https://www.sphinx-doc.org/en/master/man/sphinx-apidoc.html

Here's a hint: imagine that private means "secret".这里有一个提示:想象一下 private 的意思是“秘密”。

That's why Sphinx won't document them.这就是 Sphinx 不会记录它们的原因。

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.除非您有理由对实施保密,否则它无济于事。

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

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