Trying to use static types in Python code, so mypy
can help me with some hidden errors. It's quite simple to use with single variables
real_hour: int = lower_hour + hour_iterator
Harder to use it with lists and dictionaries, need to import additional typing
library:
from typing import Dict, List
hour_dict: Dict[str, str] = {"test_key": "test_value"}
But the main problem - how to use it with Dicts with different value types, like:
hour_dict = {"test_key": "test_value", "test_keywords": ["test_1","test_2"]}
If I don't use static typing for such dictionaries - mypy shows me errors, like:
len(hour_dict['test_keywords'])
- Argument 1 to "len" has incompatible type
So, my question: how to add static types to such dictionaries? :)
Union
. e.g.Dict[str, Union[list, str]]
. However, this doesn't ensure that particular keys are always of a specified type, it just allows values to be either (e.g.) strings or lists. – MuldoonDict[str, str]
, notDict[str:str]
.) – Jinajingle