首页 > 解决方案 > mypy 可以检查文档字符串吗?

问题描述

我有numpydoc 风格的文档字符串

def foobar(filename, copy, dtype, iterable, shape, files):
    """
    foobar is 42.

    Parameters
    ----------
    filename : str
    copy : bool
    dtype : data-type
    iterable : iterable object
    shape : int or tuple of int
    files : list of str

    Returns
    -------
    foobarfoo : int
    """
    pass

是否可以检查文档字符串类型是否正确?

(附带问题:numpy 可以返回/打印它发现的函数签名吗?)

例如,我希望以下失败:

返回类型

def foobar():
    """
    Returns
    -------
    blub : int
    """
    return "foo"

或者

def foobar(a, b):
    """
    Parameters
    ----------
    a : number
    b : number

    Returns
    -------
    blub : int
    """
    if a > b:
        return "foo"
    return 42

参数类型

def foobar(a, b):
    """
    Parameters
    ----------
    a : str
    b : int

    Returns
    -------
    blub : int
    """
    return a * b

标签: pythonmypynumpydoc

解决方案


不,mypy 只理解官方 Python 的打字符号。请参阅mypy 文档。这很好,我们不需要很多替代方法来输入注释,因为

应该有一种——最好只有一种——明显的方法来做到这一点。


推荐阅读