我在python项目中使用mypy进行类型检查。我还使用PyYAML来读写项目配置文件。不幸的是,当使用recommended import mechanism from the PyYAML documentation时,这会在尝试导入本机库的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
和CDumper
不存在,这会导致错误error: Module 'yaml' has no attribute 'CLoader'
和error: Module 'yaml' has no attribute 'CDumper'
。
有没有办法让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
从0.57开始执行此操作(请参见#500, Ignore specific lines):另外,如果使用shebang,则要忽略works文件顶部的
# mypy: ignore-errors
代码行应如下所示:Gvanrossum comment
相关问题 更多 >
编程相关推荐