Skip to content

Commit 07fa12d

Browse files
author
Roy Williams
committed
Create an Optional for calls to get with only one parameter
1 parent 11bdb88 commit 07fa12d

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

mypy/checkexpr.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ def check_call(self, callee: Type, args: List[Expression],
344344
if isinstance(callee, TypedDictGetFunction):
345345
if 1 <= len(args) <= 2 and isinstance(args[0], (StrExpr, UnicodeExpr)):
346346
return_type = self.get_typeddict_index_type(callee.typed_dict, args[0])
347+
if len(args) == 1:
348+
return_type = UnionType.make_union([
349+
return_type, NoneTyp()])
347350
return return_type, callee
348351
if callee.is_concrete_type_obj() and callee.type_object().is_abstract:
349352
type = callee.type_object()

test-data/unit/check-typeddict.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,9 @@ def set_coordinate(p: TaggedPoint, key: str, value: int) -> None:
435435
from mypy_extensions import TypedDict
436436
TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
437437
p = TaggedPoint(type='2d', x=42, y=1337)
438-
reveal_type(p.get('type')) # E: Revealed type is 'builtins.str'
439-
reveal_type(p.get('x')) # E: Revealed type is 'builtins.int'
440-
reveal_type(p.get('y')) # E: Revealed type is 'builtins.int'
438+
reveal_type(p.get('type')) # E: Revealed type is 'Union[builtins.str, builtins.None]'
439+
reveal_type(p.get('x')) # E: Revealed type is 'Union[builtins.int, builtins.None]'
440+
reveal_type(p.get('y', 0)) # E: Revealed type is 'builtins.int'
441441
[builtins fixtures/dict.pyi]
442442

443443
[case testCannotGetMethodWithInvalidStringLiteralKey]
@@ -460,7 +460,7 @@ from mypy_extensions import TypedDict
460460
TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
461461
PointSet = TypedDict('PointSet', {'first_point': TaggedPoint})
462462
p = PointSet(first_point=TaggedPoint(type='2d', x=42, y=1337))
463-
reveal_type(p.get('first_point', {}).get('x')) # E: Revealed type is 'builtins.int'
463+
reveal_type(p.get('first_point', {}).get('x', 0)) # E: Revealed type is 'builtins.int'
464464
[builtins fixtures/dict.pyi]
465465

466466
[case testDictGetMethodStillCallable]

0 commit comments

Comments
 (0)