mypy:同一丢失包的不同错误消息

mypy: different error message for the same missing package

基本上,安装 flask_api 和 flask_sqlalchemy(通过 pip3),如果我 运行 mypy on this

import flask_sqalchemy
import flask_api

错误信息是这样的

testMypy.py:1: error: No library stub file for module 'flask_sqlalchemy'
testMypy.py:1: note: (Stub files are from https://github.com/python/typeshed)
testMypy.py:2: error: Cannot find module named 'flask_api'
testMypy.py:2: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports

现在,我知道从 Jan/19/2019、flask_sqlalchemy 和 flask_api 开始,类型化中还没有存根文件,但我认为如果两个模块都是安装在系统中,报错信息应该都是"No library stub file for module ..",这两个有什么区别?

两条错误信息的意思是一样的。唯一的区别是,如果您导入的模块被认为是 "popular" 第三方库,您会收到第一条错误消息——具体来说,如果它是 in this list 模块之一。在这种情况下,flask_sqlalchemy 是该列表的成员,但 flask_api 不是。

mypy 对这些模块进行特殊处理的原因主要是为了可用性:当您看似通过 pip 安装它时,收到类似 "Cannot find module named 'blah'" 的错误消息有点令人困惑。因此,它对可能常用的第 3 方库进行了特殊处理,因此它至少可以改善那里的用户体验。

您可能会遇到的后续问题是 "why doesn't mypy just look at what's pip-installed and use the first error message whenever you try importing anything that's pip-installed?"。毕竟,mypy 无论如何都需要这样做来尝试找到 PEP 561 compatible packages——声明它们与类型提示捆绑在一起的包。

好吧,mypy 可能 可以 这样做——但是代码库的这一部分是在 PEP 561 出现之前添加的(在 mypy 真正需要扫描 pip-installed 之前)包),我怀疑很长一段时间没有人真正考虑过代码库的这个角落。