Skip to content

Optional fields in TypedDicts are not reported #18879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sk- opened this issue Apr 4, 2025 · 1 comment
Closed

Optional fields in TypedDicts are not reported #18879

sk- opened this issue Apr 4, 2025 · 1 comment
Labels
bug mypy got something wrong

Comments

@sk-
Copy link

sk- commented Apr 4, 2025

Bug Report
Optional fields in TypeDicts are not reported, allowing to unsafely access such fields.

To Reproduce

from typing import NotRequired, TypedDict


class Data(TypedDict):
    foo: NotRequired[int]


def extract(data: Data) -> int:
    return data["foo"]


print(extract({}))

https://mypy-play.net/?mypy=latest&python=3.12&flags=strict&gist=aa7bfac9b1c70873510bb3e28128f1d4

Expected Behavior
The code should raise a typing error similar to what pyright reports

mypy_typeddict.py:9:12 - error: Could not access item in TypedDict
    "foo" is not a required key in "Data", so access may result in runtime exception (reportTypedDictNotRequiredAccess)
1 error, 0 warnings, 0 informations

This is important because the code above does produce a runtime error.
Actual Behavior

No error is reported

Your Environment

  • Mypy version used: 1.15.0
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.12
@sk- sk- added the bug mypy got something wrong label Apr 4, 2025
@brianschubert
Copy link
Collaborator

Thanks - closing as duplicate of #12094

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants