你是否在函数的文档字符串中复制函数的类型提示? PEP 指南?

问题描述 投票:0回答:0

背景

建议有功能:

def do_product(a: int, b: int) -> int:
    return a * b

然后我开始记录我的功能:

def do_product(a: int, b: int) -> int:
    """This function gets two integers and returns the product

    :param a: first integer 
    :param b: second integer

    :return: return the product of a and b"""
    return a * b

当我使用 Sphinx 时,我使用 sphinx 的 sphinx-autodoc-typehints 扩展将类型提示导入到 API 文档中。这样做的好处是我在编程时只声明一次类型并且不能消除文档和函数声明之间的歧义。我目前正在处理一个 PR,要求我在文档字符串中声明类型。这看起来像这样:

def do_product(a: int, b: int) -> int:
    """This function gets two integers and returns the product

    :param int a: first integer 
    :param int b: second integer

    :return int: return the product of a and b"""
    return a * b

这没有意义,因为这似乎是函数内声明的复制..

问题:

  1. 写文档字符串的时候,需要定义类型吗?
  2. 在 python 中记录类型提示的最佳实践是什么?
  3. 是否有针对此的 PEP 标准?我能找到合适的..
python sphinx docstring pep
© www.soinside.com 2019 - 2024. All rights reserved.