【发布时间】:2017-07-28 15:55:15
【问题描述】:
如何向 Sphinx 告知“类型提示中前向引用的类定义在哪里”?
如何避免丢失类型由类型提示的前向引用定义的参数(在 Sphinx 文档中)?
我使用 sphinx-apidoc 来编写我的 Python 程序的文档。
我正在使用PEP 484 中定义的类型提示的前向引用,如下所示:
# In file A/A.py
class A:
def a(self, param: 'MyClass') -> None:
"""docstring here"""
pass
未导入 MyClass 以避免循环导入。当然,程序运行良好,因为在运行时会忽略类型提示。
当我运行 sphinx-apidoc 并制作 html 时,出现以下警告(名称 'MyClass' 未定义)。
$ sphinx-apidoc -f -o doc .
Creating file doc/A.rst
$ make html
...
reading sources... [100%] main
path/to/A/A.rst:XX: WARNING: error while formatting arguments for A.A.a: name 'MyClass' is not defined
looking for now-outdated files... none found
...
build succeeded, 1 warnings.
Build finished. The HTML pages are in _build/html.
在生成的 html 中,方法 'a' 没有这样的参数:
A module
class A
a() -> None
docstring here
在 import MyClass 的上下文中(没有循环导入),生成的 html 具有如下参数:
A module
class A
a(param: MyClass) -> None
docstring here
我的环境:
$ python3 --version
Python 3.5.2
$ pip3 --version
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)
$ pip3 freeze | grep Sphinx
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Sphinx==1.5.2
【问题讨论】:
标签: python python-3.x python-sphinx