我正在尝试使用mypy检查我的类型注解,但不断出现以下错误:
Script.py:201: error: Item "Dict[str, Union[float, int]]" of "Union[Dict[str, Union[float, int]], str, float, int, bool]" has no
attribute "endswith"
我的代码如下所示:
from typing import Counter, Dict, Iterable, List, NoReturn, Optional, Set, Tuple, Union
TYPE_NUMBER = Union[float, int]
TYPE_CONFIGURATION = Dict[str, Union[Dict[str, TYPE_NUMBER], str, float, int, bool]]
def check_configuration(config: TYPE_CONFIGURATION) -> Union[bool, NoReturn]:
database = 'database'
assert isinstance(config[database], str)
assert config[database].endswith('.prdb')
它正常调用python运行得很好。所以我知道结果 config[database1]
实际上是一根弦。问题在于我的类型别名: TYPE_CONFIGURATION = Dict[str, Union[Dict[str, TYPE_NUMBER], str, float, int, bool]]
还是一只虫子?
配置是从json文件加载的dict,其中唯一的可选参数是“start”。json文件如下所示:
{
"database" : "bla/bla/bla/file.csv",
"distance" : 800,
"t" : false,
"output" : "bla/bla/bla/file-out.csv",
"start" : {"1": 1343.786, "2": 1356.523}
}
暂无答案!
目前还没有任何答案,快来回答吧!