mypy 未检测到基本类型错误 [英] mypy not detecting a basic type error

查看:61
本文介绍了mypy 未检测到基本类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 python 3.5.1.以及当前使用 git 安装的 mypy,mypy 标记错误 1 ​​&2,但它不报告 3

With python 3.5.1. and a current installation of mypy using git, mypy flags error 1 & 2, but it does not report 3

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

What am I doing wrong, or is this a bug, or is this a known problem?

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

推荐答案

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

My understanding from the docs: http://mypy.readthedocs.org/en/latest/basics.html is that mypy will only check a thing (module, function, whatever) if it's indicated to it that it should check it (by importing typing at module level or by annotating a function).

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

So 1 is checked because it's in a function which is typed, 2 is checked as the import typing indicates that your module is typed and it's at module scope but 3 is in the scope of an untyped function so it's ignored.

这篇关于mypy 未检测到基本类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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