Mypy可以检查文档字符串吗?

时间:2018-11-09 07:21:55

标签: python mypy numpydoc

我有numpydoc-style docstrings

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

1 个答案:

答案 0 :(得分:1)

不,mypy仅了解Python的官方输入符号。参见the mypy docs。很好,我们不需要很多其他方式来输入注释,例如

  

应该有一种-最好只有一种-显而易见的方法。