Can mypy check docstrings?

2.1k Views Asked by At

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

There are 1 best solutions below

5
pawelswiecki On

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.