Skip to content

Accessing a NotRequired field in a TypedDict does not error #17329

@mark-thm

Description

@mark-thm

Bug Report

Accessing a NotRequired field in a TypedDict does not result in an error.

To Reproduce

from typing import NotRequired, TypedDict

TypeDef = TypedDict(
    "TypeDef",
    {
        "Field": NotRequired[str],
    },
)

sample = TypeDef()
val: str = sample["Field"] # expect error here

Expected Behavior

The final line should produce an error, either that "Field" is not necessarily defined or that the resulting access might produce a None, and that val is therefore the wrong type.

Actual Behavior

No error.

Your Environment

MacOS Sonoma, arm64.

  • Mypy version used: 1.10.0
  • Mypy command-line flags: default
  • Mypy configuration options from mypy.ini (and other config files): default
  • Python version used: 3.12.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions