【问题标题】:Can type hint in python 3 be used to generate docstring?python 3中的类型提示可以用来生成文档字符串吗?
【发布时间】:2017-02-16 19:50:12
【问题描述】:

我们可以在python中使用docstring来表示函数参数的类型:

def f1(a):
    """
    :param a: an input.
    :type a: int
    :return: the input integer.
    :rtype: int
    """
    return a

对于f1,autodoc 生成以下文档:

fun1(a)
    Parameters : a (int) – an input.
    Returns    : the input integer.
    Return type: int

在python 3中,类型也可以通过类型提示来指示:

def f2(a: int):
    """
    :param a: an input.
    :return: the input integer.
    :rtype: int
    """
    return a

当我们运行 autodoc 时,它通过参数声明来放置类型,而不是在描述中:

f2(a: int)
    Parameters : a – an input.
    Returns    : the input integer.
    Return type: int

是否可以使用注释而不是文档字符串将文档生成为f1?我正在使用python 3.6。谢谢!

【问题讨论】:

  • 显然你可以写出你想要的代码。
  • 查看:github.com/sphinx-doc/sphinx/issues/1968 可能通过此更改,您可以获得开箱即用的类型注释支持
  • 是的,希望 Sphinx 很快会支持它。

标签: python python-3.x autodoc


【解决方案1】:

还没有,据我所知,Sphinx 还不支持此功能。评论中引用的错误是关于类型提示的表示而不是它们的定位。

我知道目前有一个 Sphinx 扩展程序可以为您解决这个问题,名为 sphinx-autodoc-typehints。您可能暂时可以使用它。

【讨论】:

  • 太好了,暂时用这个插件。希望这个功能能够原生集成到 Sphinx 中。谢谢。
  • 如果有人也想使用Napoleonthis answer 有更多关于如何让两者一起玩的详细信息。
猜你喜欢
  • 2021-11-16
  • 2020-01-26
  • 1970-01-01
  • 2012-06-20
  • 1970-01-01
  • 1970-01-01
  • 2017-03-09
  • 1970-01-01
  • 2021-01-14
相关资源
最近更新 更多