Using python TypedDict
and Unpack
, we can add types to any function that accepts variable-length keyword arguments
from typing import TypedDict
from typing import Unpack # python3.11
# if you're on python3.10 or lower
# from typing_extensions import Unpack
class AKwargs(TypedDict):
name: str
age: int
def a(**kwargs: Unpack[AKwargs]):
pass
Your editor's language server will alert you if there are any errors in input to the function. Here's how pylance reports it.
Here's how mypy reports the error
❯ mypy t.py --enable-incomplete-feature=Unpack
t.py:15: error: Missing named argument "name" for "a" [call-arg]
t.py:15: error: Missing named argument "age" for "a" [call-arg]
Found 2 errors in 1 file (checked 1 source file)
And when you add an unknown parameter,