mypy 未检测到基本类型错误

Tim*_*son 6 python static-typing mypy

使用 python 3.5.1。以及当前使用 git 安装的 mypy,mypy 标记错误 1 ​​和 2,但未报告 3

我做错了什么,或者这是一个错误,还是一个已知的问题?

import typing

def test_ordered_dict(od: typing.Dict[str,int]) -> typing.Dict[str,int]:
    return 1   #type error 1

a = test_ordered_dict(1)   #type error 2

def test_me():
    a = test_ordered_dict(1)  # type error 3 is not reported
Run Code Online (Sandbox Code Playgroud)

Mac*_*cha 6

我对文档的理解:http ://mypy.readthedocs.org/en/latest/basics.html 是如果它指示它应该检查它(通过在模块级别导入输入或通过注释函数)。

因此,检查 1 是因为它位于已输入的函数中,检查 2 是因为导入类型表明您的模块已输入并且在模块范围内,但 3 在无类型函数的范围内,因此被忽略。

  • 你可以使用 `--check-untyped-defs` 来捕捉这个错误。 (5认同)