【问题标题】:automodule breaking with Python return type annotations使用 Python 返回类型注释的自动模块中断
【发布时间】:2014-05-27 22:31:15
【问题描述】:

我正在尝试使用 Sphinx 记录我的 Python 3 代码。这是我的示例文件:

class MyClass:
    """
    Class description
    """

    def get_name(self) -> str:
        """
        Returns the name of this object as a string.
        """
        return "Jim"

我在一个 .rst 文件中的指令是:

.. autoclass:: my_class.MyClass
   :members:

但是当我运行sphinx-build -b html -d _build/doctrees . _build/html(版本1.1.3)时,类型注释->会中断:

    def command_name(self) -> str:
                           ^
SyntaxError: invalid syntax

如何让 Sphinx 识别类型注释?

编辑:我的 python3 版本是 3.2.3。

【问题讨论】:

标签: python python-sphinx


【解决方案1】:

虽然自动模块可能无法识别 Python 3 注释,但构建不应中断使用它们。我的问题是 Sphinx 正在使用我的 python2.7 解释器:

SyntaxError: invalid syntax
Traceback (most recent call last):on_osdp_messages
  File "/usr/lib/python2.7/dist-packages/sphinx/ext/autodoc.py", line 321, in import_object
    __import__(self.modname)

这可能意味着您安装了错误版本的 Sphinx。请参阅this questionin my case 我需要使用不同的 Debian 软件包)。

【讨论】:

    猜你喜欢
    • 2021-10-31
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 2011-08-03
    • 2019-10-19
    • 2021-08-03
    • 2017-02-10
    相关资源
    最近更新 更多