I'm trying to check my type annotations using mypy, but this error keeps ocurring:
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"
My code looks like this:
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')
It runs just fine calling python normally. So I know the result of config[database1]
is in fact a string. Is the problem is my type alias:
TYPE_CONFIGURATION = Dict[str, Union[Dict[str, TYPE_NUMBER], str, float, int, bool]]
or is it a bug ?
The config is a dict loaded from a JSON file where the only optional parameter is "start". The JSON file looks like this:
{
"database" : "bla/bla/bla/file.csv",
"distance" : 800,
"t" : false,
"output" : "bla/bla/bla/file-out.csv",
"start" : {"1": 1343.786, "2": 1356.523}
}