mypy 可以检查文档字符串吗? [英] Can mypy check docstrings?

查看:38
本文介绍了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 能否返回/打印它发现的函数签名?)

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

例如,我希望以下内容失败:

For example, I would expect the following to fail:

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

推荐答案

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

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.

这篇关于mypy 可以检查文档字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆