Mypy vs passing along kwargs (special appearance by argparse.ArgumentParser)

The untyped version worked because by default mypy won’t check untyped functions. As soon as you write the first type annotation in your function, mypy will start looking at it.

There are two problems here. The first is that you want to forward arguments from your function’s parameters to another callable. As you discovered by yourself, the only way of doing this in a fully-typed manner is by re-writing the full signature of the target callable as a TypedDict. IMO, this is one of the places in which you should take advantage of the fact that typing in Python is gradual: just declare opts as Any. Or, if you prefer, hide the mypy error with a #type: ignore comment.

If you want to go ahead with the TypedDict solution, you will get to the second problem: one of the types of your target callable is a private module member. Argh. Again, this is not easy to solve, and again IMO you should just type that as Any.

You might want to take a look at Taking the argument signature from a different function

1 Like