python / python/mypy

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

Open
#15,834 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-typed-dict
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the TypedDict and NotRequired example from the issue, using the linked mypy playground and the reported Python and mypy versions. Trace how mypy checks keyword arguments supplied through TypedDict unpacking, including the total=False case. Done means the invalid call with a missing required keyword argument produces a type-checking error without breaking valid unpacking.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.