Skip to content

Commit 8d93b67

Browse files
authored
Make typeddict-unknown-key sub-code of typeddict-item (#14620)
The PR that added the error code didn't make into 1.0, so I make it a sub-code, to improve backwards compatibility. Also fixing a type while I am touching this code.
1 parent 11c63aa commit 8d93b67

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

docs/source/error_code_list.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,9 @@ Whereas reading an unknown value will generate the more generic/serious
526526
# Error: TypedDict "Point" has no key "z" [typeddict-item]
527527
_ = a["z"]
528528
529+
.. note::
530+
531+
This error code is a sub-error code of a wider ``[typeddict-item]`` code.
529532

530533
Check that type of target is known [has-type]
531534
---------------------------------------------

mypy/errorcodes.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,11 @@ def __str__(self) -> str:
8484
TYPEDDICT_ITEM: Final = ErrorCode(
8585
"typeddict-item", "Check items when constructing TypedDict", "General"
8686
)
87-
TYPPEDICT_UNKNOWN_KEY: Final = ErrorCode(
88-
"typeddict-unknown-key", "Check unknown keys when constructing TypedDict", "General"
87+
TYPEDDICT_UNKNOWN_KEY: Final = ErrorCode(
88+
"typeddict-unknown-key",
89+
"Check unknown keys when constructing TypedDict",
90+
"General",
91+
sub_code_of=TYPEDDICT_ITEM,
8992
)
9093
HAS_TYPE: Final = ErrorCode(
9194
"has-type", "Check that type of reference can be determined", "General"

mypy/messages.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,7 +1652,7 @@ def unexpected_typeddict_keys(
16521652
format_key_list(extra, short=True), format_type(typ)
16531653
),
16541654
context,
1655-
code=codes.TYPPEDICT_UNKNOWN_KEY,
1655+
code=codes.TYPEDDICT_UNKNOWN_KEY,
16561656
)
16571657
if missing or extra:
16581658
# No need to check for further errors
@@ -1693,7 +1693,7 @@ def typeddict_key_not_found(
16931693
context,
16941694
)
16951695
else:
1696-
err_code = codes.TYPPEDICT_UNKNOWN_KEY if setitem else codes.TYPEDDICT_ITEM
1696+
err_code = codes.TYPEDDICT_UNKNOWN_KEY if setitem else codes.TYPEDDICT_ITEM
16971697
self.fail(
16981698
f'TypedDict {format_type(typ)} has no key "{item_name}"', context, code=err_code
16991699
)

test-data/unit/check-errorcodes.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,14 @@ not_exist = a['not_exist'] # type: ignore[typeddict-item]
481481
[builtins fixtures/dict.pyi]
482482
[typing fixtures/typing-typeddict.pyi]
483483

484+
[case testErrorCodeTypedDictSubCodeIgnore]
485+
from typing_extensions import TypedDict
486+
class D(TypedDict):
487+
x: int
488+
d: D = {'x': 1, 'y': 2} # type: ignore[typeddict-item]
489+
[builtins fixtures/dict.pyi]
490+
[typing fixtures/typing-typeddict.pyi]
491+
484492
[case testErrorCodeCannotDetermineType]
485493
y = x # E: Cannot determine type of "x" [has-type] # E: Name "x" is used before definition [used-before-def]
486494
reveal_type(y) # N: Revealed type is "Any"

0 commit comments

Comments
 (0)