Skip to content

Add specific message for type errors on dict displays #3100

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
Apr 4, 2017
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
2 changes: 1 addition & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1726,7 +1726,7 @@ def visit_dict_expr(self, e: DictExpr) -> Type:
[None],
self.chk.named_generic_type('builtins.dict', [kt, vt]),
self.named_type('builtins.function'),
name='<list>',
name='<dict>',
variables=[ktdef, vtdef])
rv = self.check_call(constructor, args, [nodes.ARG_POS] * len(args), e)[0]
else:
Expand Down
8 changes: 7 additions & 1 deletion mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,13 @@ def incompatible_argument(self, n: int, m: int, callee: CallableType, arg_type:
name = callee.name[1:-1]
n -= 1
msg = '{} item {} has incompatible type {}'.format(
name[0].upper() + name[1:], n, self.format_simple(arg_type))
name.title(), n, self.format_simple(arg_type))
elif callee.name == '<dict>':
name = callee.name[1:-1]
n -= 1
key_type, value_type = cast(TupleType, arg_type).items
msg = '{} entry {} has incompatible type {}: {}'.format(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use item instead of entry, similar to the naming of the items() method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen "entry" used for dictionaries (example: https://docs.python.org/3/reference/expressions.html#dictionary-displays) quite often; item too but it's more ambiguous (it's also used for lists). I don't mind changing it to avoid bikeshedding if you still prefer it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, entry seems official enough so let's keep it.

name.title(), n, self.format_simple(key_type), self.format_simple(value_type))
elif callee.name == '<list-comprehension>':
msg = 'List comprehension has incompatible type List[{}]'.format(
strip_quotes(self.format(arg_type)))
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1522,8 +1522,8 @@ def g() -> Iterator[int]:
[case testDictWithKeywordArgsOnly]
from typing import Dict, Any
d1 = dict(a=1, b=2) # type: Dict[str, int]
d2 = dict(a=1, b='') # type: Dict[str, int] # E: List item 1 has incompatible type "Tuple[str, str]"
d3 = dict(a=1) # type: Dict[int, int] # E: List item 0 has incompatible type "Tuple[str, int]"
d2 = dict(a=1, b='') # type: Dict[str, int] # E: Dict entry 1 has incompatible type "str": "str"
d3 = dict(a=1) # type: Dict[int, int] # E: Dict entry 0 has incompatible type "str": "int"
d4 = dict(a=1, b=1)
d4.xyz # E: Dict[str, int] has no attribute "xyz"
d5 = dict(a=1, b='') # type: Dict[str, Any]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ def do_something(dic: Row) -> None:
def do_another() -> Row:
return {}

do_something({'good': 'bad'}) # E: List item 0 has incompatible type "Tuple[str, str]"
do_something({'good': 'bad'}) # E: Dict entry 0 has incompatible type "str": "str"
reveal_type(do_another()) # E: Revealed type is 'builtins.dict[builtins.str, builtins.int]'

[file ex2a.py]
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,7 @@ d3.xyz # E
d4 = dict(a=1, b='') # type: Dict[str, Any]
result = dict(x=[], y=[]) # type: Dict[str, List[str]]
[out]
_program.py:3: error: List item 1 has incompatible type "Tuple[str, str]"
_program.py:3: error: Dict entry 1 has incompatible type "str": "str"
_program.py:5: error: Dict[str, int] has no attribute "xyz"

[case testDefaultDict]
Expand Down Expand Up @@ -1097,7 +1097,7 @@ MyDDict(dict)[0]
_program.py:6: error: Argument 1 to "defaultdict" has incompatible type List[_T]; expected Callable[[], str]
_program.py:9: error: Invalid index type "str" for "dict"; expected type "int"
_program.py:9: error: Incompatible types in assignment (expression has type "int", target has type "str")
_program.py:19: error: List item 0 has incompatible type "Tuple[str, List[<uninhabited>]]"
_program.py:19: error: Dict entry 0 has incompatible type "str": List[<uninhabited>]
_program.py:23: error: Invalid index type "str" for "dict"; expected type "int"

[case testNoSubcriptionOfStdlibCollections]
Expand Down