Skip to content

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

Open
@canassa

Description

@canassa

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions