Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can mypy check docstrings?

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
like image 356
Martin Thoma Avatar asked Oct 16 '22 10:10

Martin Thoma


1 Answers

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.

like image 178
pawelswiecki Avatar answered Nov 15 '22 05:11

pawelswiecki