Type checking for unpacked TypedDicts as function arguments #374
Labels
enhancement
New feature or request
fixed in next version (main)
A fix has been implemented and will appear in an upcoming version
Issue Type: Feature request.
Summary: when using a TypedDict and keyword unpacking it (**dict), check that the types in the TypedDict match with the arguments in the function.
In functions where arguments are meant to be passed as arguments to other functions the number of arguments can get too large. I find that using dicts to store keyword arguments for the inner functions is really useful, and with the introduction of TypedDicts we know the values that are present and could use them to check if it is compatible with the arguments of the function.
For example we could do something like:
Currently Pylance does not type check the unpacking and treats
mydict
as a normal dict. I think we could take advantage that we know the types of each value in the TypedDict to type check if all arguments are given and if given the correct type.In this example
my_other_function
has an argumentvar1: str
instead ofint
, so the type checker would show a type error.More cases
In this case for a function to allow unpacking of
MyDict
it should have argumentsvar1
,var2
andvar3
of the types indicated in the TypedDicts, butvar3
must have a default value.MyDict
with 3 values andtotal=True
, then when unpacking the function should have 3 arguments (if less then error raised) or define a**kwargs
to match remaining.MyDict
do not complete the function arguments then they must be completed manually:The text was updated successfully, but these errors were encountered: