# Buggy code
def check_multi_assignment_from_tuple(
    self,
    lvalues: List[Lvalue],
    rvalue: Expression,
    rvalue_type: TupleType,
    context: Context,
    undefined_rvalue: bool,
    infer_lvalue_type: bool = True,
) -> None:
    if self.check_rvalue_count_in_assignment(lvalues, len(rvalue_type.items), context):
        star_index = next(
            (i for i, lv in enumerate(lvalues) if isinstance(lv, StarExpr)),
            len(lvalues),
        )

        left_lvs = lvalues[:star_index]
        star_lv = (
            cast(StarExpr, lvalues[star_index]) if star_index != len(lvalues) else None
        )
        right_lvs = lvalues[star_index + 1 :]

        if not undefined_rvalue:
            # Infer rvalue again, now in the correct type context.
            lvalue_type = self.lvalue_type_for_inference(lvalues, rvalue_type)
            reinferred_rvalue_type = self.expr_checker.accept(rvalue, lvalue_type)

            if isinstance(reinferred_rvalue_type, UnionType):
                # If this is an Optional type in non-strict Optional code, unwrap it.
                relevant_items = reinferred_rvalue_type.relevant_items()
                if len(relevant_items) == 1:
                    reinferred_rvalue_type = relevant_items[0]

            assert isinstance(reinferred_rvalue_type, TupleType)
            rvalue_type = reinferred_rvalue_type

        left_rv_types, star_rv_types, right_rv_types = self.split_around_star(
            rvalue_type.items, star_index, len(lvalues)
        )

        for lv, rv_type in zip(left_lvs, left_rv_types):
            self.check_assignment(
                lv, self.temp_node(rv_type, context), infer_lvalue_type
            )
        if star_lv:
            list_expr = ListExpr(
                [self.temp_node(rv_type, context) for rv_type in star_rv_types]
            )
            list_expr.set_line(context.get_line())
            self.check_assignment(star_lv.expr, list_expr, infer_lvalue_type)
        for lv, rv_type in zip(right_lvs, right_rv_types):
            self.check_assignment(
                lv, self.temp_node(rv_type, context), infer_lvalue_type
            )


# Traceback (AssertionError)
mypy --py2 --show-traceback test.py

test.py:6: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.521-858f7512cf3b4e39c0f4e8de5a13eee0e1e138fb-dirty
Traceback (most recent call last):
File "/usr/local/Cellar/mypy/0.521/libexec/bin/mypy", line 11, in <module>
load_entry_point('mypy===0.521-858f7512cf3b4e39c0f4e8de5a13eee0e1e138fb-dirty', 'console_scripts', 'mypy')()
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/__main__.py", line 7, in console_entry
main(None)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/main.py", line 50, in main
res = type_check_only(sources, bin_dir, options)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/main.py", line 97, in type_check_only
options=options)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/build.py", line 196, in build
graph = dispatch(sources, manager)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/build.py", line 1801, in dispatch
process_graph(graph, manager)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/build.py", line 2044, in process_graph
process_stale_scc(graph, scc, manager)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/build.py", line 2147, in process_stale_scc
graph[id].type_check_first_pass()
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/build.py", line 1716, in type_check_first_pass
self.type_checker.check_first_pass()
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 185, in check_first_pass
self.accept(d)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 273, in accept
stmt.accept(self)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/nodes.py", line 565, in accept
return visitor.visit_func_def(self)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 519, in visit_func_def
self.check_func_item(defn, name=defn.name())
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 578, in check_func_item
self.check_func_def(defn, typ, name)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 738, in check_func_def
self.accept(item.body)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 273, in accept
stmt.accept(self)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/nodes.py", line 815, in accept
return visitor.visit_block(self)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 1202, in visit_block
self.accept(s)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 273, in accept
stmt.accept(self)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/nodes.py", line 859, in accept
return visitor.visit_assignment_stmt(self)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 1209, in visit_assignment_stmt
self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 1236, in check_assignment
infer_lvalue_type)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 1483, in check_assignment_to_multiple_lvalues
self.check_multi_assignment(lvalues, rvalue, context, infer_lvalue_type)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 1522, in check_multi_assignment
context, undefined_rvalue, infer_lvalue_type)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 1550, in check_multi_assignment_from_tuple
assert isinstance(reinferred_rvalue_type, TupleType)
AssertionError:
test.py:6: note: use --pdb to drop into pdb

# Fixed code
def check_multi_assignment_from_tuple(
    self,
    lvalues: List[Lvalue],
    rvalue: Expression,
    rvalue_type: TupleType,
    context: Context,
    undefined_rvalue: bool,
    infer_lvalue_type: bool = True,
) -> None:
    if self.check_rvalue_count_in_assignment(lvalues, len(rvalue_type.items), context):
        star_index = next(
            (i for i, lv in enumerate(lvalues) if isinstance(lv, StarExpr)),
            len(lvalues),
        )

        left_lvs = lvalues[:star_index]
        star_lv = (
            cast(StarExpr, lvalues[star_index]) if star_index != len(lvalues) else None
        )
        right_lvs = lvalues[star_index + 1 :]

        if not undefined_rvalue:
            # Infer rvalue again, now in the correct type context.
            lvalue_type = self.lvalue_type_for_inference(lvalues, rvalue_type)
            reinferred_rvalue_type = self.expr_checker.accept(rvalue, lvalue_type)

            if isinstance(reinferred_rvalue_type, UnionType):
                # If this is an Optional type in non-strict Optional code, unwrap it.
                relevant_items = reinferred_rvalue_type.relevant_items()
                if len(relevant_items) == 1:
                    reinferred_rvalue_type = relevant_items[0]
            if isinstance(reinferred_rvalue_type, UnionType):
                self.check_multi_assignment_from_union(
                    lvalues, rvalue, reinferred_rvalue_type, context, infer_lvalue_type
                )
                return
            assert isinstance(reinferred_rvalue_type, TupleType)
            rvalue_type = reinferred_rvalue_type

        left_rv_types, star_rv_types, right_rv_types = self.split_around_star(
            rvalue_type.items, star_index, len(lvalues)
        )

        for lv, rv_type in zip(left_lvs, left_rv_types):
            self.check_assignment(
                lv, self.temp_node(rv_type, context), infer_lvalue_type
            )
        if star_lv:
            list_expr = ListExpr(
                [self.temp_node(rv_type, context) for rv_type in star_rv_types]
            )
            list_expr.set_line(context.get_line())
            self.check_assignment(star_lv.expr, list_expr, infer_lvalue_type)
        for lv, rv_type in zip(right_lvs, right_rv_types):
            self.check_assignment(
                lv, self.temp_node(rv_type, context), infer_lvalue_type
            )