Apologies for the mess of a title:
Suppose I have a function defined that takes a number of parameters.
Now later in my code, I use a dictionary to pass into test()
all the keyword args.
import argparse
from typing import Dict, Callable
def test(a: str, b: Dict[str, str], c: str, d: argparse.Namespace, e: Callable) -> None:
if d.t:
print(f"{a} to the {b['foo']} to the {c}!")
else:
print(f"{a} to the {b['foo']} to the {c} also, {e()}")
def hello() -> str:
return "Hello"
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-t", action="store_true", default=False)
args = parser.parse_args()
b = {'foo': 'bar'}
info = {'b': b, "c": "foobar", "d": args, "e": hello}
test("foo", **info)
When I run MyPy on my code, I get the following errors:
test.py:21: error: Argument 2 to "test" has incompatible type "**Dict[str, object]"; expected "Dict[str, str]"
test.py:21: error: Argument 2 to "test" has incompatible type "**Dict[str, object]"; expected "str"
test.py:21: error: Argument 2 to "test" has incompatible type "**Dict[str, object]"; expected "Namespace"
test.py:21: error: Argument 2 to "test" has incompatible type "**Dict[str, object]"; expected "Callable[..., Any]"
Found 4 errors in 1 file (checked 1 source file)
Why am I getting this? And how can I properly type hint the function to allow this?