Allow using TypedDict for more precise typing of **kwds · Issue #4441 · python/mypy (original) (raw)

There are some situations where a user wants to have more precisely typed **kwds. Current syntax only allows homogeneous **kwds:

def fun(x: int, *, **options: str) -> None: ...

However, in situations with many heterogeneous options listing all options in the signature could be verbose and will require rewriting some existing code. There is a vague idea to allow TypedDict for such situations. For example:

class Options(TypedDict): timeout: int alternative: str on_error: Callable[[int], None] on_timeout: Callable[[], None] ...

def fun(x: int, *, **options: Options) -> None: ...

Maybe for such cases the TypedDict used should be automatically understood as defined with total=False. Also it is worth mentioning that this feature will allow reusing the TypedDicts in modules where several functions have same (or similar) option sets.