Skip to content

Make typeddict-unknown-key sub-code of typeddict-item #14620

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

Merged
merged 1 commit into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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