From 5b7f8d9680c18eac5e8e40ca29ba86571ccdb1cd Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Thu, 28 Apr 2016 17:40:52 -0700 Subject: [PATCH] Avoid crash in edge case of multiple assignment. Fix #974, # 1420, #1438. --- mypy/checker.py | 2 ++ mypy/test/data/check-tuples.test | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/mypy/checker.py b/mypy/checker.py index ce33850d96a52..2fcb9d4088489 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -1143,6 +1143,8 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> Type: if len(s.lvalues) > 1: # Chained assignment (e.g. x = y = ...). # Make sure that rvalue type will not be reinferred. + if s.rvalue not in self.type_map: + self.accept(s.rvalue) rvalue = self.temp_node(self.type_map[s.rvalue], s) for lv in s.lvalues[:-1]: self.check_assignment(lv, rvalue, s.type is None) diff --git a/mypy/test/data/check-tuples.test b/mypy/test/data/check-tuples.test index 2e943faa2bcf8..e57d6532186d2 100644 --- a/mypy/test/data/check-tuples.test +++ b/mypy/test/data/check-tuples.test @@ -364,6 +364,12 @@ b = '' a = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") b = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str") +[case testMultipleAssignmentWithMixedVariables] +a = b, c = 1, 1 +x, y = p, q = 1, 1 +u, v, w = r, s = 1, 1 # E: Need more than 2 values to unpack (3 expected) +d, e = f, g, h = 1, 1 # E: Need more than 2 values to unpack (3 expected) + -- Assignment to starred expressions -- ---------------------------------