Skip to content

Commit 1316c4e

Browse files
dmoissetJukkaL
authored andcommitted
Add specific message for type errors on dict displays (#3100)
This fixes #304, #1183, and #2538
1 parent 61cdbf3 commit 1316c4e

File tree

5 files changed

+13
-7
lines changed

5 files changed

+13
-7
lines changed

mypy/checkexpr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1745,7 +1745,7 @@ def visit_dict_expr(self, e: DictExpr) -> Type:
17451745
[None],
17461746
self.chk.named_generic_type('builtins.dict', [kt, vt]),
17471747
self.named_type('builtins.function'),
1748-
name='<list>',
1748+
name='<dict>',
17491749
variables=[ktdef, vtdef])
17501750
rv = self.check_call(constructor, args, [nodes.ARG_POS] * len(args), e)[0]
17511751
else:

mypy/messages.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,13 @@ def incompatible_argument(self, n: int, m: int, callee: CallableType, arg_type:
522522
name = callee.name[1:-1]
523523
n -= 1
524524
msg = '{} item {} has incompatible type {}'.format(
525-
name[0].upper() + name[1:], n, self.format_simple(arg_type))
525+
name.title(), n, self.format_simple(arg_type))
526+
elif callee.name == '<dict>':
527+
name = callee.name[1:-1]
528+
n -= 1
529+
key_type, value_type = cast(TupleType, arg_type).items
530+
msg = '{} entry {} has incompatible type {}: {}'.format(
531+
name.title(), n, self.format_simple(key_type), self.format_simple(value_type))
526532
elif callee.name == '<list-comprehension>':
527533
msg = 'List comprehension has incompatible type List[{}]'.format(
528534
strip_quotes(self.format(arg_type)))

test-data/unit/check-expressions.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,8 +1522,8 @@ def g() -> Iterator[int]:
15221522
[case testDictWithKeywordArgsOnly]
15231523
from typing import Dict, Any
15241524
d1 = dict(a=1, b=2) # type: Dict[str, int]
1525-
d2 = dict(a=1, b='') # type: Dict[str, int] # E: List item 1 has incompatible type "Tuple[str, str]"
1526-
d3 = dict(a=1) # type: Dict[int, int] # E: List item 0 has incompatible type "Tuple[str, int]"
1525+
d2 = dict(a=1, b='') # type: Dict[str, int] # E: Dict entry 1 has incompatible type "str": "str"
1526+
d3 = dict(a=1) # type: Dict[int, int] # E: Dict entry 0 has incompatible type "str": "int"
15271527
d4 = dict(a=1, b=1)
15281528
d4.xyz # E: Dict[str, int] has no attribute "xyz"
15291529
d5 = dict(a=1, b='') # type: Dict[str, Any]

test-data/unit/check-modules.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,7 @@ def do_something(dic: Row) -> None:
13591359
def do_another() -> Row:
13601360
return {}
13611361

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

13651365
[file ex2a.py]

test-data/unit/pythoneval.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ d3.xyz # E
10651065
d4 = dict(a=1, b='') # type: Dict[str, Any]
10661066
result = dict(x=[], y=[]) # type: Dict[str, List[str]]
10671067
[out]
1068-
_program.py:3: error: List item 1 has incompatible type "Tuple[str, str]"
1068+
_program.py:3: error: Dict entry 1 has incompatible type "str": "str"
10691069
_program.py:5: error: Dict[str, int] has no attribute "xyz"
10701070

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

11031103
[case testNoSubcriptionOfStdlibCollections]

0 commit comments

Comments
 (0)