6

Suppose I have a fully type hinted method with only keyword arguments :

class A:
    def func(a: int, b: str, c: SomeIntricateTypeHint) -> OutputClass:
        ...

Now suppose I have a function that takes in variable keyword arguments, and passes them entirely onto that method :

def outer_func(n_repeat: int, **kwargs: ???) -> OtherOutputClass:
    a = A()
    for _ in range(n_repeat):
        a.func(**kwargs)

In doing so, I have lost the benefits of the type hints for func. How do I type hint kwargs in outer_func such that I recover those benefits ?

For extra detail, in my case, I don't personally define func. It's actually a method from a boto3 client object. As a result I'm looking for a solution that dynamically creates the type hint, rather than having to manually create the TypedDict.

ticster
  • 744
  • 2
  • 5
  • 19
  • dict[some types]? – funnydman Sep 13 '22 at 14:51
  • I don't see how that would dynamically bring forth the variable names and types defined in `func`. – ticster Sep 13 '22 at 14:53
  • Does this answer your question? [Type annotations for \*args and \*\*kwargs](https://stackoverflow.com/questions/37031928/type-annotations-for-args-and-kwargs) – ThePyGuy Sep 13 '22 at 14:53
  • 1
    No, as I understand the general rules for typing *args and **kwargs. My question is if there's a way to used an already type hinted function to type hint a **kwargs, – ticster Sep 13 '22 at 14:55
  • 2
    If I understand what you're asking, [this](https://stackoverflow.com/questions/71253495/how-to-annotate-the-type-of-arguments-forwarded-to-another-function) might be relevant. – iced Sep 13 '22 at 17:19
  • It's very close, but in my case the signatures are not completeley identical, but I can definitely adapt the solution to my case. I must say, that is a very clever use of decorators ! – ticster Sep 13 '22 at 18:05

0 Answers0