How to get a TypedDict corresponding to a function signature?

Viewed 402

Say I've got a function signature like this:

def any_foo(
    bar: Bar,
    with_baz: Optional[Baz] = None,
    with_datetime: Optional[datetime] = None,
    effective: Optional[bool] = False,
) -> Foo

I could of course just copy its declaration and fiddle with it enough to create the following TypedDict:

AnyFooParameters = TypedDict(
    "AnyFooParameters",
    {
        bar: Bar,
        with_baz: Optional[Baz],
        with_datetime: Optional[datetime],
        effective: Optional[bool]
    }
)

But this seems like such a straight-forward transformation that I wonder whether there's some easy way to create this TypedDict (or at least the name: type pairs) straight from the function.

0 Answers
Related