Skip to content

Commit

Permalink
Make typeddict-unknown-key sub-code of typeddict-item (#14620)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ilevkivskyi authored Feb 7, 2023
1 parent 11c63aa commit 8d93b67
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
3 changes: 3 additions & 0 deletions docs/source/error_code_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,9 @@ Whereas reading an unknown value will generate the more generic/serious
# Error: TypedDict "Point" has no key "z" [typeddict-item]
_ = a["z"]
.. note::

This error code is a sub-error code of a wider ``[typeddict-item]`` code.

Check that type of target is known [has-type]
---------------------------------------------
Expand Down
7 changes: 5 additions & 2 deletions mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ 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"
TYPEDDICT_UNKNOWN_KEY: Final = ErrorCode(
"typeddict-unknown-key",
"Check unknown keys when constructing TypedDict",
"General",
sub_code_of=TYPEDDICT_ITEM,
)
HAS_TYPE: Final = ErrorCode(
"has-type", "Check that type of reference can be determined", "General"
Expand Down
4 changes: 2 additions & 2 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1652,7 +1652,7 @@ def unexpected_typeddict_keys(
format_key_list(extra, short=True), format_type(typ)
),
context,
code=codes.TYPPEDICT_UNKNOWN_KEY,
code=codes.TYPEDDICT_UNKNOWN_KEY,
)
if missing or extra:
# No need to check for further errors
Expand Down Expand Up @@ -1693,7 +1693,7 @@ def typeddict_key_not_found(
context,
)
else:
err_code = codes.TYPPEDICT_UNKNOWN_KEY if setitem else codes.TYPEDDICT_ITEM
err_code = codes.TYPEDDICT_UNKNOWN_KEY if setitem else codes.TYPEDDICT_ITEM
self.fail(
f'TypedDict {format_type(typ)} has no key "{item_name}"', context, code=err_code
)
Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,14 @@ not_exist = a['not_exist'] # type: ignore[typeddict-item]
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testErrorCodeTypedDictSubCodeIgnore]
from typing_extensions import TypedDict
class D(TypedDict):
x: int
d: D = {'x': 1, 'y': 2} # type: ignore[typeddict-item]
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testErrorCodeCannotDetermineType]
y = x # E: Cannot determine type of "x" [has-type] # E: Name "x" is used before definition [used-before-def]
reveal_type(y) # N: Revealed type is "Any"
Expand Down

0 comments on commit 8d93b67

Please sign in to comment.