From c7824be884875ddd7c21db1b32c11164f64fd103 Mon Sep 17 00:00:00 2001 From: Joaquim Esteves Date: Wed, 30 Nov 2022 13:45:13 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Added=20new=20Error=20to=20TypedDic?= =?UTF-8?q?t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See: [#4617](https://github.com/python/mypy/issues/4617) This allows the following code to trigger the error `typeddict-unknown-key` ```python A = T.TypedDict("A", {"x": int}) def f(x: A) -> None: ... f({"x": 1, "y": "foo"}) ``` The user can then safely ignore this specific error at their disgression. --- mypy/errorcodes.py | 3 +++ mypy/messages.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/mypy/errorcodes.py b/mypy/errorcodes.py index 1c15407a955b..234b9bce40f7 100644 --- a/mypy/errorcodes.py +++ b/mypy/errorcodes.py @@ -67,6 +67,9 @@ def __str__(self) -> str: TYPEDDICT_ITEM: Final = ErrorCode( "typeddict-item", "Check items when constructing TypedDict", "General" ) +TYPPEDICT_UNKNOWN_KEY: Final = ErrorCode( + "typeddict-unknown-key", "Check unknown keys when constructing TypedDict", "General" +) HAS_TYPE: Final = ErrorCode( "has-type", "Check that type of reference can be determined", "General" ) diff --git a/mypy/messages.py b/mypy/messages.py index 85fa30512534..300ee85317ea 100644 --- a/mypy/messages.py +++ b/mypy/messages.py @@ -1633,7 +1633,7 @@ def unexpected_typeddict_keys( format_key_list(extra, short=True), format_type(typ) ), context, - code=codes.TYPEDDICT_ITEM, + code=codes.TYPPEDICT_UNKNOWN_KEY, ) return found = format_key_list(actual_keys, short=True)