狮身人面像包括私人属性,即使没有指示

发布于 2025-02-06 02:39:43 字数 1284 浏览 0 评论 0原文

我没有在任何地方包括:private-Members:选项,但是Sphinx仍然在课堂文档中包含私人属性。我在做什么错?

MWE:

mypackage/foo.py

class MyClass(object):
    """A class.

    Attributes
    ----------
    attr1 : float
        Attribute 1.
    attr2 : float
        Attribute 2.
    _private1 : float
        Private attribute 1.
    _private2 : float
        Private attribute 2.
    """
    pass

index.rst

.. toctree::
    :maxdepth: 2
    :caption: Table of Contents

    foo

foo.rst

``foo`` Module
==============

.. automodule:: mypackage.foo
    :members:

conf.py

# -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.

import os
import sys
sys.path.insert(0, os.path.abspath('../'))

extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.napoleon',
]

source_suffix = '.rst'

master_doc = 'index'

language = 'en'

渲染html :

I did not include the :private-members: option anywhere, but Sphinx still includes private attributes in the docs for the class. What am I doing wrong?

MWE:

mypackage/foo.py:

class MyClass(object):
    """A class.

    Attributes
    ----------
    attr1 : float
        Attribute 1.
    attr2 : float
        Attribute 2.
    _private1 : float
        Private attribute 1.
    _private2 : float
        Private attribute 2.
    """
    pass

index.rst:

.. toctree::
    :maxdepth: 2
    :caption: Table of Contents

    foo

foo.rst:

``foo`` Module
==============

.. automodule:: mypackage.foo
    :members:

conf.py:

# -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.

import os
import sys
sys.path.insert(0, os.path.abspath('../'))

extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.napoleon',
]

source_suffix = '.rst'

master_doc = 'index'

language = 'en'

Rendered html:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

烂柯人 2025-02-13 02:39:44

缺少:私人会员:仅对狮身人面像通过解析Python代码找到的成员生效。例如:

class MyClass(object):
    """A class"""
    attr1 = 42  #: Attribute 1.
    attr2 = 43  #: Attribute 2.
    _private1 = 44  #: Private attribute 1.
    _private2 = 45  #: Private attribute 2.

在这种情况下,它不会记录_private1_private2(没有:private>:private-Members:)。

但是,在您的情况下,_private1_private2只是文档字符串中的文本。我怀疑在这种情况下,狮身人面像知道它们是班级的属性。

The absence of :private-members: only takes effect for members that sphinx finds by parsing the Python code. For example:

class MyClass(object):
    """A class"""
    attr1 = 42  #: Attribute 1.
    attr2 = 43  #: Attribute 2.
    _private1 = 44  #: Private attribute 1.
    _private2 = 45  #: Private attribute 2.

In this case, it won't document _private1 and _private2 (without :private-members:).

In you case, however, _private1 and _private2 are just text inside a doc string. I doubt even that sphinx in that case knows that they are attributes of the class.

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