python / python/mypy

Mypy fails to detect missing keyword arguments when unpacking a TypedDict with NotRequired fields

Aperta
#15,834 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-typed-dict
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo l’esempio di TypedDict e NotRequired dell’issue, usando il mypy playground collegato e le versioni di Python e mypy riportate. Traccia il modo in cui mypy controlla gli argomenti con nome forniti tramite l’unpacking di TypedDict, incluso il caso total=False. Il lavoro è completato quando la chiamata non valida con un argomento con nome obbligatorio mancante produce un errore di type checking senza compromettere l’unpacking valido.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.