简体   繁体   English

Python:docstrings和type annotations

[英]Python: docstrings and type annotations

Having a function like: 有这样的功能:

def foo(x: int) -> float:
    return float(x)

I would like to use a NumPy-like docstring like the following: 我想使用类似NumPy的文档字符串,如下所示:

def foo(x: int) -> float:
    """
    Parameters
    ----------
    x
        Input parameter

    Returns
    -------
    The output value.
    """
    return float(x)

Note that: 注意:

  • I do not want to specify the parameter type again. 我不想再次指定参数类型。
  • I do not want to specify the return type again. 我不想再次指定返回类型。
  • I would like that extension to be able to read the annotated types (and write them in the generated HTML documentation). 我希望该扩展能够读取带注释的类型(并在生成的HTML文档中编写它们)。

Is there a Sphinx extension that supports that? 是否有支持它的Sphinx扩展? Would you recommend another syntax? 你会推荐另一种语法吗?

Standard extension is autodoc . 标准扩展名是autodoc Napoleon extension supports Google- and NumPy-style docstrings. Napoleon扩展支持Google和NumPy风格的文档字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM