我在用着mypy http://mypy-lang.org/在我的 python 项目中进行类型检查。我还使用 PyYAML 来读取和写入项目配置文件。不幸的是,当使用PyYAML 文档中推荐的导入机制 http://pyyaml.org/wiki/PyYAMLDocumentation这会在尝试导入本机库的 try/ except 子句中生成虚假错误:
from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
在我的系统上CLoader
and CDumper
不存在,这会导致错误error: Module 'yaml' has no attribute 'CLoader'
and error: Module 'yaml' has no attribute 'CDumper'
.
有没有办法让 mypy 忽略这一行的错误?我希望我可以做这样的事情让 mypy 跳过该行:
from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper # nomypy
except ImportError:
from yaml import Loader, Dumper
您可以忽略类型错误# type: ignore
as of 版本0.2 https://github.com/python/mypy/commit/c0334e419a8b50eb4844e5dadc48399036f12ed3(参见问题#500、忽略特定行 https://github.com/python/mypy/issues/500):
PEP 484 https://www.python.org/dev/peps/pep-0484/ uses # type: ignore
for 忽略特定行上的类型错误 ...
另外,使用# type: ignore
靠近文件顶部[跳过]完全检查该文件.
Source: mypy#500 https://github.com/python/mypy/issues/500#issuecomment-86340780. See also the mypy documentation https://mypy.readthedocs.io/en/stable/common_issues.html#spurious-errors-and-locally-silencing-the-checker.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)