Can mypy check docstrings?

╄→гoц情女王★ 提交于 2021-02-18 12:12:38

问题


I have 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

Is it possible to check if the docstring-types can possibly be correct?

(side question: Can numpy return/print the function signatures it discovered?)

For example, I would expect the following to fail:

Return Types

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

or

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

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

Parameter types

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

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

回答1:


No, mypy understands the official Python's typing notation only. See the mypy docs. And this fine, we don't need many alternative ways to type annotate, as

There should be one-- and preferably only one --obvious way to do it.



来源:https://stackoverflow.com/questions/53221369/can-mypy-check-docstrings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!