Skip to content

Commit 7b12582

Browse files
committed
Integrate feedback.
1 parent 654eaf6 commit 7b12582

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

mypy/messages.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -822,15 +822,15 @@ def typeddict_instantiated_with_unexpected_items(self,
822822
def typeddict_item_name_must_be_string_literal(self,
823823
typ: TypedDictType,
824824
context: Context):
825-
self.fail('Cannot prove expression is a valid item name. Expected one of {}.'.format(
826-
list(typ.items.keys())), context)
825+
self.fail('Cannot prove expression is a valid item name; expected one of {}'.format(
826+
format_item_name_list(typ.items.keys())), context)
827827

828828
def typeddict_item_name_not_found(self,
829829
typ: TypedDictType,
830830
item_name: str,
831831
context: Context):
832-
self.fail('\'{}\' is not a valid item name. Expected one of {}.'.format(
833-
item_name, list(typ.items.keys())), context)
832+
self.fail('\'{}\' is not a valid item name; expected one of {}'.format(
833+
item_name, format_item_name_list(typ.items.keys())), context)
834834

835835

836836
def capitalize(s: str) -> str:
@@ -875,6 +875,14 @@ def format_string_list(s: Iterable[str]) -> str:
875875
return '%s, ... and %s (%i methods suppressed)' % (', '.join(l[:2]), l[-1], len(l) - 3)
876876

877877

878+
def format_item_name_list(s: Iterable[str]) -> str:
879+
l = list(s)
880+
if len(l) <= 5:
881+
return '[' + ', '.join(["'%s'" % name for name in l]) + ']'
882+
else:
883+
return '[' + ', '.join(["'%s'" % name for name in l[:5]]) + ', ...]'
884+
885+
878886
def callable_name(type: CallableType) -> str:
879887
if type.name:
880888
return type.name

test-data/unit/check-typeddict.test

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,20 @@ reveal_type(p['x']) # E: Revealed type is 'builtins.int'
369369
reveal_type(p['y']) # E: Revealed type is 'builtins.int'
370370
[builtins fixtures/dict.pyi]
371371

372+
[case testCanGetItemOfTypedDictWithValidBytesOrUnicodeLiteralKey]
373+
# flags: --python-version 2.7
374+
from mypy_extensions import TypedDict
375+
Cell = TypedDict('Cell', {'value': int})
376+
c = Cell(value=42)
377+
reveal_type(c['value']) # E: Revealed type is 'builtins.int'
378+
reveal_type(c[u'value']) # E: Revealed type is 'builtins.int'
379+
[builtins_py2 fixtures/dict.pyi]
380+
372381
[case testCannotGetItemOfTypedDictWithInvalidStringLiteralKey]
373382
from mypy_extensions import TypedDict
374383
TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
375384
p = TaggedPoint(type='2d', x=42, y=1337)
376-
p['z'] # E: 'z' is not a valid item name. Expected one of ['type', 'x', 'y'].
385+
p['z'] # E: 'z' is not a valid item name; expected one of ['type', 'x', 'y']
377386
[builtins fixtures/dict.pyi]
378387

379388
[case testCannotGetItemOfTypedDictWithNonLiteralKey]
@@ -382,24 +391,32 @@ from typing import Union
382391
TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
383392
p = TaggedPoint(type='2d', x=42, y=1337)
384393
def get_coordinate(p: TaggedPoint, key: str) -> Union[str, int]:
385-
return p[key] # E: Cannot prove expression is a valid item name. Expected one of ['type', 'x', 'y'].
394+
return p[key] # E: Cannot prove expression is a valid item name; expected one of ['type', 'x', 'y']
386395
[builtins fixtures/dict.pyi]
387396

388397

389398
-- Special Method: __setitem__
390399

391-
[case testCanSetItemOfTypedDictWithValidStringLiteralKey]
400+
[case testCanSetItemOfTypedDictWithValidStringLiteralKeyAndCompatibleValueType]
392401
from mypy_extensions import TypedDict
393402
TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
394403
p = TaggedPoint(type='2d', x=42, y=1337)
404+
p['type'] = 'two_d'
395405
p['x'] = 1
396406
[builtins fixtures/dict.pyi]
397407

408+
[case testCannotSetItemOfTypedDictWithIncompatibleValueType]
409+
from mypy_extensions import TypedDict
410+
TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
411+
p = TaggedPoint(type='2d', x=42, y=1337)
412+
p['x'] = 'y' # E: Argument 2 has incompatible type "str"; expected "int"
413+
[builtins fixtures/dict.pyi]
414+
398415
[case testCannotSetItemOfTypedDictWithInvalidStringLiteralKey]
399416
from mypy_extensions import TypedDict
400417
TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
401418
p = TaggedPoint(type='2d', x=42, y=1337)
402-
p['z'] = 1 # E: 'z' is not a valid item name. Expected one of ['type', 'x', 'y'].
419+
p['z'] = 1 # E: 'z' is not a valid item name; expected one of ['type', 'x', 'y']
403420
[builtins fixtures/dict.pyi]
404421

405422
[case testCannotSetItemOfTypedDictWithNonLiteralKey]
@@ -408,7 +425,7 @@ from typing import Union
408425
TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
409426
p = TaggedPoint(type='2d', x=42, y=1337)
410427
def set_coordinate(p: TaggedPoint, key: str, value: int) -> None:
411-
p[key] = value # E: Cannot prove expression is a valid item name. Expected one of ['type', 'x', 'y'].
428+
p[key] = value # E: Cannot prove expression is a valid item name; expected one of ['type', 'x', 'y']
412429
[builtins fixtures/dict.pyi]
413430

414431

0 commit comments

Comments
 (0)