【发布时间】:2018-08-19 13:48:40
【问题描述】:
我在我的 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 忽略这一行的错误?我希望我可以做这样的事情让 mypy 跳过那一行:
from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper # nomypy
except ImportError:
from yaml import Loader, Dumper
【问题讨论】: