Skip to content

Commit 5c59f61

Browse files
committed
make sure comment overrides inferred assignment
1 parent 3289b92 commit 5c59f61

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

mypy/checker.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -909,16 +909,18 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> Type:
909909
910910
Handle all kinds of assignment statements (simple, indexed, multiple).
911911
"""
912-
self.check_assignments(self.expand_lvalues(s.lvalues[-1]), s.rvalue)
912+
self.check_assignments(self.expand_lvalues(s.lvalues[-1]), s.rvalue,
913+
s.type)
913914
if len(s.lvalues) > 1:
914915
# Chained assignment (e.g. x = y = ...).
915916
# Make sure that rvalue type will not be reinferred.
916917
rvalue = self.temp_node(self.type_map[s.rvalue], s)
917918
for lv in s.lvalues[:-1]:
918-
self.check_assignments(self.expand_lvalues(lv), rvalue)
919+
self.check_assignments(self.expand_lvalues(lv), rvalue,
920+
s.type)
919921

920922
def check_assignments(self, lvalues: List[Node],
921-
rvalue: Node) -> None:
923+
rvalue: Node, force_rvalue_type: Type=None) -> None:
922924
# Collect lvalue types. Index lvalues require special consideration,
923925
# since we cannot typecheck them until we know the rvalue type.
924926
# For each lvalue, one of lvalue_types[i] or index_lvalues[i] is not
@@ -964,12 +966,12 @@ def check_assignments(self, lvalues: List[Node],
964966
# Single lvalue.
965967
rvalue_type = self.check_single_assignment(lvalue_types[0],
966968
index_lvalues[0], rvalue, rvalue)
967-
if rvalue_type:
969+
if rvalue_type and not force_rvalue_type:
968970
self.binder.assign_type(lvalues[0], rvalue_type)
969971
else:
970972
rvalue_types = self.check_multi_assignment(lvalue_types, index_lvalues,
971973
rvalue, rvalue)
972-
if rvalue_types:
974+
if rvalue_types and not force_rvalue_type:
973975
for lv, rt in zip(lvalues, rvalue_types):
974976
self.binder.assign_type(lv, rt)
975977
if is_inferred:

mypy/test/data/check-isinstance.test

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
[case testForcedAssignment]
2+
x = 1 # type: object
3+
y = 1
4+
y = x # E: Incompatible types in assignment (expression has type "object", variable has type "int")
5+
x = 2
6+
y = x
17
[case testJoinAny]
28
from typing import List, Any
39

@@ -255,7 +261,8 @@ from typing import Union, Undefined
255261

256262
def foo() -> Union[int, str]: pass
257263

258-
x = 1 # type: Union[int, str]
264+
x = foo()
265+
x = 1
259266
x = x + 1
260267
x = foo()
261268
x = x + 1 # E: Unsupported operand types for + (likely involving Union)
@@ -452,7 +459,8 @@ x + [1] # E: Unsupported operand types for + (likely involving Uni
452459
from typing import Union, List
453460

454461
while 1:
455-
x = 1 # type: Union[int, str, List[int]]
462+
x = None # type: Union[int, str, List[int]]
463+
x = 1
456464
if isinstance(x, int):
457465
x + 1
458466
break

0 commit comments

Comments
 (0)