setter参数的类型未在狮身人面表文档中显示

发布于 2025-01-18 09:15:46 字数 1409 浏览 2 评论 0原文

我不知道如何使类型提示出现在seters参数的狮身人面像生成的文档中。

我有一个python类,带有一个名为批处理的属性,并且在属性中有Docstrings,并在setter参数(int)中键入暗示。以下是最小示例,但是

class Settings:
    """Settings used"""

    def __init__(self):
        self._batches = None 

    @property
    def batches(self):
        """Number of batches to simulate"""
        return self._batches

    @batches.setter
    def batches(self, batches: int):
        self._batches = batches

使用以下命令。

sphinx-build -b html ./source/ ./build/html/

我正在使用sphinx构建文档,并在 conf.py 我在拿破仑文档所建议的拿破仑包之前有“ sphinx_autodoc_typehints”软件包。我还尝试将其放在检查之后:-)

文档我正在使用:autosummary:

.. autosummary::
   :toctree: generated
   :nosignatures:
   :template: myclass.rst

docs 正在构建,但类型提示没有出现:

I can't figure out how to get the type hints to appear in the Sphinx generated documentation from setters arguments.

I have a Python class with an attribute called batches and I've got docstrings in the property and type hinting in the setter argument (int). Below is the minimal example, but here is the full version

class Settings:
    """Settings used"""

    def __init__(self):
        self._batches = None 

    @property
    def batches(self):
        """Number of batches to simulate"""
        return self._batches

    @batches.setter
    def batches(self, batches: int):
        self._batches = batches

I'm building documentation with sphinx and using the following command

sphinx-build -b html ./source/ ./build/html/

In the conf.py I have the "sphinx_autodoc_typehints" package before the napoleon package as suggested by the sphinx docs. I have also tried putting it after just to check :-)

In the docs I am using :autosummary:

.. autosummary::
   :toctree: generated
   :nosignatures:
   :template: myclass.rst

The docs are building but the type hints are not appearing:

doc output

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

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

发布评论

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

评论(1

白色秋天 2025-01-25 09:15:46

使用装饰器制造的属性与属性相同。这不是一个函数。狮身人面像对此表示敬意。实际上,sphinx仅读取@property方法的文档,而不是@property.setter.setter完全。

在您的情况下,指定基本属性的类型提示就足够了。

class Settings:

    @property
    def batches(self) -> int:
        """Number of batches to simulate"""
        return self._batches

如果您的二传手接受其他类型(例如浮点,字符串等)并设法将此输入作为内部投入,则可以将其作为基本属性中的散文记录,例如。

class Settings:

    @property
    def batches(self) -> int:
        """Number of batches to simulate

        When set with a non integer value, the value is coerced
        as an integer, or will raise a :exc:`TypeError` if this 
        fails.
        """
        return self._batches

    @batches.setter
    def batches(self, batches: Union[int,str]):
        self._batches = int(batches)

A property made using the decorators is for all purposes the same as an attribute. It is not a function. Sphinx honors this. Actually, Sphinx only read the documentation for the @property method, not the @property.setter one at all.

In your case, specifying a type hint for the base property should be enough.

class Settings:

    @property
    def batches(self) -> int:
        """Number of batches to simulate"""
        return self._batches

In case your setter accepts other types (like floats, strings, etc) and manage to cast this input as an int internally, you can document that as prose in the base property, like so.

class Settings:

    @property
    def batches(self) -> int:
        """Number of batches to simulate

        When set with a non integer value, the value is coerced
        as an integer, or will raise a :exc:`TypeError` if this 
        fails.
        """
        return self._batches

    @batches.setter
    def batches(self, batches: Union[int,str]):
        self._batches = int(batches)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文