我如何使用 Sphinx' 私有方法的 Autodoc 扩展?

发布于 2024-07-27 14:03:23 字数 175 浏览 12 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

请恋爱 2024-08-03 14:03:23

如果您使用的是 sphinx 1.1 或更高版本,请从 sphinx 文档站点 http: //www.sphinx-doc.org/en/master/ext/autodoc.html,

:special-members:
:private-members:

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,

:special-members:
:private-members:
机场等船 2024-08-03 14:03:23

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

autodoc_default_options = {
    "members": True,
    "undoc-members": True,
    "private-members": True
}

对于 sphinx <= 1.8

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

1.8 更新,感谢@partofthething。

You can add this to conf.py file:

autodoc_default_options = {
    "members": True,
    "undoc-members": True,
    "private-members": True
}

For sphinx <= 1.8

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

1.8 update thanks to @partofthething.

半仙 2024-08-03 14:03:23

解决这个问题的一种方法是明确强制 Sphinx 记录私有成员。 您可以通过将 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

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:

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
薄荷梦 2024-08-03 14:03:23

您是否尝试过使用自定义方法来确定成员是否应该包含在文档中,使用 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?

新人笑 2024-08-03 14:03:23

查看 apidoc 代码,我们可以更改什么sphinx-apidoc 生成设置环境变量:

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

您还可以将此设置添加到您的 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

Looking at apidoc code, we can change what sphinx-apidoc generate setting an environment variable:

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):

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
隔纱相望 2024-08-03 14:03:23

除了上面的答案之外,如果您使用命令 sphinx-apidoc

只需添加 -P 标志即可添加私有成员,
例如,

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

有关更多选项和标志信息,请查看官方文档:

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

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

缱倦旧时光 2024-08-03 14:03:23

不,私有意味着该类是私有的,并且不应该从公共 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.

扛起拖把扫天下 2024-08-03 14:03:23

如果您只想记录特定的私有方法,而不是全部,则可以对每个方法使用 automethod 指令,而不是 :private-members:

我经常使用与普通公共方法同名的前导下划线方法,该方法具有函数的实际实现。 然后,公共方法对输入参数进行各种健全性检查。 下划线方法会跳过它们,因此可以调用它们以提高效率,但类型安全性较低。

例如(对@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

然后记录_fly_to,而不是_evaporate,你可以这样做:

.. autoclass:: SmokeMonster
    :members:

    .. automethod:: SmokeMonster._fly_to

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)

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:

.. autoclass:: SmokeMonster
    :members:

    .. automethod:: SmokeMonster._fly_to
相思碎 2024-08-03 14:03:23

这里有一个提示:想象一下 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文