You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using a TypedDict that contains fields marked with NotRequired from typing_extensions, and then unpacking this dictionary to pass it as keyword arguments to a function, Mypy does not raise an error even when mandatory keyword arguments are missing.
To Reproduce
The following playground contains an invalid Python code that Mypy is unable to detect any issues
fromtypingimportTypedDictfromtyping_extensionsimportNotRequired# Creates a TypedDict with one optional propertyclassParams(TypedDict):
a: strb: NotRequired[int]
# Defines a function with two mandatory keyword argumentsdeffunc(*, a: str, b: int):
pass# Defines a dict with only the mandatory argumentparams: Params= {"a": "a"}
# Tries to call the function with one missing argumentfunc(**params)
Note: The same issue happens when using total=False
Expected Behavior
Mypy should raise an error if a function does not receive all its required arguments
Actual Behavior
Mypy is unable to detect any problems
Your Environment
Mypy version used: 1.4.1
Mypy command-line flags:
Mypy configuration options from mypy.ini (and other config files):
I think mypy is doing the right thing in this case.
When matching keyword parameters, a type checker needs to either assume that NotRequired keys are present or that they are not. It cannot generally know (statically) whether a NotRequired key is present. I think it's reasonable for mypy to assume that all NotRequred keys are present. (That's the same assumption pyright makes in this case.)
This approach reduces false positive errors, potentially at the expense of false negatives in some cases. This is consistent with the handling of an unpacked dict, which can also be missing some keys.
deffunc(*, a: str, b: str):
passparams: dict[str, str] = {"a": "a"}
func(**params) # No error reported by mypy
Note btw there is a flag --extra-checks (part of --strict), that has couple ~similar checks for e.g. TypedDict literals with {"foo": "bar", **other}, and TypedDict.update(). We may include such check there.
Bug Report
When using a TypedDict that contains fields marked with NotRequired from typing_extensions, and then unpacking this dictionary to pass it as keyword arguments to a function, Mypy does not raise an error even when mandatory keyword arguments are missing.
To Reproduce
The following playground contains an invalid Python code that Mypy is unable to detect any issues
https://mypy-play.net/?mypy=latest&python=3.11&gist=27aab8774484cff83b69d1659290ab09
Note: The same issue happens when using
total=False
Expected Behavior
Mypy should raise an error if a function does not receive all its required arguments
Actual Behavior
Mypy is unable to detect any problems
Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: