@@ -360,64 +360,73 @@ reveal_type(f(g)) # E: Revealed type is '<uninhabited>'
360
360
361
361
-- Special Method: __getitem__
362
362
363
- -- TODO: Implement support for this case.
364
- --[case testCanGetItemOfTypedDictWithValidStringLiteralKey]
365
- --from mypy_extensions import TypedDict
366
- --TaggedPoint = TypedDict('TaggedPoint ', {'type': str, 'x': int, 'y': int} )
367
- --p = TaggedPoint( type='2d', x=42, y=1337)
368
- --def get_x(p: TaggedPoint) -> int:
369
- -- return p['x']
370
- -- [builtins fixtures/dict.pyi]
363
+ [ case testCanGetItemOfTypedDictWithValidStringLiteralKey]
364
+ from mypy_extensions import TypedDict
365
+ TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
366
+ p = TaggedPoint(type='2d ', x=42, y=1337 )
367
+ reveal_type(p['type']) # E: Revealed type is 'builtins.str'
368
+ reveal_type(p['x']) # E: Revealed type is 'builtins. int'
369
+ reveal_type(p['y']) # E: Revealed type is 'builtins.int'
370
+ [builtins fixtures/dict.pyi]
371
371
372
- -- TODO: Implement support for this case.
373
- --[case testCannotGetItemOfTypedDictWithInvalidStringLiteralKey]
374
- -- from mypy_extensions import TypedDict
375
- --TaggedPoint = TypedDict('TaggedPoint ', {'type': str, 'x': int, 'y ': int})
376
- --p = TaggedPoint(type='2d', x=42, y=1337 )
377
- --def get_z(p: TaggedPoint) -> int:
378
- -- return p['z'] # E: ... 'z' is not a valid key for Point. Expected one of {'x', 'y'}.
379
- --[builtins fixtures/dict.pyi]
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
380
381
- -- TODO: Implement support for this case.
382
- --[case testCannotGetItemOfTypedDictWithNonLiteralKey]
383
- --from mypy_extensions import TypedDict
384
- --from typing import Union
385
- --TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
386
- --p = TaggedPoint(type='2d', x=42, y=1337)
387
- --def get_coordinate(p: TaggedPoint, key: str) -> Union[str, int]:
388
- -- return p[key] # E: ... Cannot prove 'key' is a valid key for Point. Expected one of {'x', 'y'}
389
- --[builtins fixtures/dict.pyi]
381
+ [case testCannotGetItemOfTypedDictWithInvalidStringLiteralKey]
382
+ from mypy_extensions import TypedDict
383
+ TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
384
+ p = TaggedPoint(type='2d', x=42, y=1337)
385
+ p['z'] # E: 'z' is not a valid item name; expected one of ['type', 'x', 'y']
386
+ [builtins fixtures/dict.pyi]
387
+
388
+ [case testCannotGetItemOfTypedDictWithNonLiteralKey]
389
+ from mypy_extensions import TypedDict
390
+ from typing import Union
391
+ TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
392
+ p = TaggedPoint(type='2d', x=42, y=1337)
393
+ def get_coordinate(p: TaggedPoint, key: str) -> Union[str, int]:
394
+ return p[key] # E: Cannot prove expression is a valid item name; expected one of ['type', 'x', 'y']
395
+ [builtins fixtures/dict.pyi]
390
396
391
397
392
398
-- Special Method: __setitem__
393
399
394
- -- TODO: Implement support for this case.
395
- --[case testCanSetItemOfTypedDictWithValidStringLiteralKey]
396
- --from mypy_extensions import TypedDict
397
- --TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
398
- --p = TaggedPoint(type='2d', x=42, y=1337)
399
- --def set_x(p: TaggedPoint, x: int) -> None:
400
- -- p['x'] = x
401
- --[builtins fixtures/dict.pyi]
400
+ [case testCanSetItemOfTypedDictWithValidStringLiteralKeyAndCompatibleValueType]
401
+ from mypy_extensions import TypedDict
402
+ TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
403
+ p = TaggedPoint(type='2d', x=42, y=1337)
404
+ p['type'] = 'two_d'
405
+ p['x'] = 1
406
+ [builtins fixtures/dict.pyi]
402
407
403
- -- TODO: Implement support for this case.
404
- --[case testCannotSetItemOfTypedDictWithInvalidStringLiteralKey]
405
- --from mypy_extensions import TypedDict
406
- --TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
407
- --p = TaggedPoint(type='2d', x=42, y=1337)
408
- --def set_z(p: TaggedPoint, z: int) -> None:
409
- -- p['z'] = z # E: ... 'z' is not a valid key for Point. Expected one of {'x', 'y'}.
410
- --[builtins fixtures/dict.pyi]
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]
411
414
412
- -- TODO: Implement support for this case.
413
- --[case testCannotSetItemOfTypedDictWithNonLiteralKey]
414
- --from mypy_extensions import TypedDict
415
- --from typing import Union
416
- --TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
417
- --p = TaggedPoint(type='2d', x=42, y=1337)
418
- --def set_coordinate(p: TaggedPoint, key: str, value: Union[str, int]) -> None:
419
- -- p[key] = value # E: ... Cannot prove 'key' is a valid key for Point. Expected one of {'x', 'y'}
420
- --[builtins fixtures/dict.pyi]
415
+ [case testCannotSetItemOfTypedDictWithInvalidStringLiteralKey]
416
+ from mypy_extensions import TypedDict
417
+ TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
418
+ p = TaggedPoint(type='2d', x=42, y=1337)
419
+ p['z'] = 1 # E: 'z' is not a valid item name; expected one of ['type', 'x', 'y']
420
+ [builtins fixtures/dict.pyi]
421
+
422
+ [case testCannotSetItemOfTypedDictWithNonLiteralKey]
423
+ from mypy_extensions import TypedDict
424
+ from typing import Union
425
+ TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
426
+ p = TaggedPoint(type='2d', x=42, y=1337)
427
+ def set_coordinate(p: TaggedPoint, key: str, value: int) -> None:
428
+ p[key] = value # E: Cannot prove expression is a valid item name; expected one of ['type', 'x', 'y']
429
+ [builtins fixtures/dict.pyi]
421
430
422
431
423
432
-- Special Method: get
0 commit comments