【发布时间】:2021-08-01 00:45:27
【问题描述】:
给定一个名为 mymodule 的模块中的以下函数,我想使用带有 autodoc 的 Sphinx 进行记录:
from typing import Union
from collections.abc import Iterable
from numpy.typing import ArrayLike
def foo(a: Union[str, int], b: Iterable, c: ArrayLike) -> None:
"""Do something useful."""
pass
在源代码中,函数的签名非常易读。但是,在autodoc 生成的文档中,签名显示为
khadl._util.foo(a: Union[str, int], b: collections.abc.Iterable, c: Union[int, float, complex, str, bytes, numpy.generic, Sequence[Union[int, float, complex, str, bytes, numpy.generic]], Sequence[Sequence[Any]], numpy.typing._array_like._SupportsArray]) → None
这是不可读的。源自typing 模块的类以简短形式显示(Union、Sequence、Any),但对于抽象基类Iterable,会生成一个唯一标识符(collections.abc.Iterable)和@ 987654332@ 甚至被“解包”(Union[int, float, complex, str, bytes, numpy.generic, Sequence[Union[int, float, complex, str, bytes, numpy.generic]], Sequence[Sequence[Any]], numpy.typing._array_like._SupportsArray])。
如何配置 Sphinx 以便函数的签名在文档中以可读的方式显示,例如和源代码一样?
【问题讨论】:
-
您的
c输入非常笼统。必须有那么多选择吗? -
您可以通过将签名添加为文档字符串的第一行来完成此操作。见stackoverflow.com/q/12082570/407651。
-
@astrochun 好吧,我不只是为了好玩才选择那种类型。它是“任何可以转换为
numpy.ndarray的东西”。 -
嗯。它不包括可转换为
numpy.ndarray的list -
@astrochun
lists 是Sequences。
标签: python python-sphinx type-hinting autodoc