Mypy fails to detect missing keyword arguments when unpacking a TypedDict with NotRequired fields
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
from typing import TypedDict
from typing_extensions import NotRequired
# Creates a TypedDict with one optional property
class Params(TypedDict):
a: str
b: NotRequired[int]
# Defines a function with two mandatory keyword arguments
def func(*, a: str, b: int):
pass
# Defines a dict with only the mandatory argument
params: Params = {"a": "a"}
# Tries to call the function with one missing argument
func(**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):
[tool.mypy]
python_version = "3.10"
check_untyped_defs = true
ignore_errors = false
ignore_missing_imports = true
strict_optional = true
warn_unused_ignores = true
warn_redundant_casts = true
warn_unused_configs = true
disallow_untyped_defs = false
enable_incomplete_feature = ["Unpack"]
plugins = [
"pydantic.mypy",
]
- Python version used: 3.10.12
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, das TypedDict- und NotRequired-Beispiel aus dem Issue mithilfe des verlinkten mypy playgrounds sowie der angegebenen Python- und mypy-Versionen zu reproduzieren. Verfolge, wie mypy durch TypedDict-Unpacking übergebene Schlüsselwortargumente prüft, einschließlich des Falls total=False. Als erledigt gilt die Aufgabe, wenn der ungültige Aufruf mit einem fehlenden erforderlichen Schlüsselwortargument einen Typprüfungsfehler erzeugt, ohne gültiges Unpacking zu beeinträchtigen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100