I'm trying to add type hints in a dict
type which has multiple fields that bind functions to them.
e.g.
from typing import Dict, Callable, Any, Union
def fn():
print("Hello World")
def fn2(name):
print("goodbye world", name)
d = {
"hello" : {
"world": fn
},
"goodbye": {
"world": fn2
}
} # type: Dict[str, Dict[str, Union[Callable[[], None], Callable[[str], None]]]]
d["hello"]["world"]()
d["goodbye"]["world"]("john")
The problem I'm running to is whenever I try run mypy
(v0.782) it throws error:
test2.py:17: error: Too few arguments
test2.py:18: error: Too many arguments
Clearly, I've passed the right argument as can be seen from the function definition and type hints. I'm clearly missing something for it to be throwing error.
However, the following works, so I suspect it has something to do with Union
type in type hints.
from typing import Dict, Callable, Any, Union
def fn():
print("Hello World")
d = {"hello": {"world": fn}} # type: Dict[str, Dict[str, Callable[[], None]]]
d["hello"]["world"]()