@@ -369,11 +369,20 @@ reveal_type(p['x']) # E: Revealed type is 'builtins.int'
369
369
reveal_type(p['y']) # E: Revealed type is 'builtins.int'
370
370
[builtins fixtures/dict.pyi]
371
371
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
+
372
381
[case testCannotGetItemOfTypedDictWithInvalidStringLiteralKey]
373
382
from mypy_extensions import TypedDict
374
383
TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
375
384
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']
377
386
[builtins fixtures/dict.pyi]
378
387
379
388
[case testCannotGetItemOfTypedDictWithNonLiteralKey]
@@ -382,24 +391,32 @@ from typing import Union
382
391
TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
383
392
p = TaggedPoint(type='2d', x=42, y=1337)
384
393
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']
386
395
[builtins fixtures/dict.pyi]
387
396
388
397
389
398
-- Special Method: __setitem__
390
399
391
- [case testCanSetItemOfTypedDictWithValidStringLiteralKey ]
400
+ [case testCanSetItemOfTypedDictWithValidStringLiteralKeyAndCompatibleValueType ]
392
401
from mypy_extensions import TypedDict
393
402
TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
394
403
p = TaggedPoint(type='2d', x=42, y=1337)
404
+ p['type'] = 'two_d'
395
405
p['x'] = 1
396
406
[builtins fixtures/dict.pyi]
397
407
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
+
398
415
[case testCannotSetItemOfTypedDictWithInvalidStringLiteralKey]
399
416
from mypy_extensions import TypedDict
400
417
TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
401
418
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']
403
420
[builtins fixtures/dict.pyi]
404
421
405
422
[case testCannotSetItemOfTypedDictWithNonLiteralKey]
@@ -408,7 +425,7 @@ from typing import Union
408
425
TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
409
426
p = TaggedPoint(type='2d', x=42, y=1337)
410
427
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']
412
429
[builtins fixtures/dict.pyi]
413
430
414
431
0 commit comments