# Buggy code
def try_infer_partial_type(self, e: CallExpr) -> None:
    if isinstance(e.callee, MemberExpr) and isinstance(e.callee.expr, RefExpr):
        var = cast(Var, e.callee.expr.node)
        partial_types = self.chk.find_partial_types(var)
        if partial_types is not None and not self.chk.current_node_deferred:
            partial_type = cast(PartialType, var.type)
            if partial_type is None or partial_type.type is None:
                # A partial None type -> can't infer anything.
                return
            typename = partial_type.type.fullname()
            methodname = e.callee.name
            # Sometimes we can infer a full type for a partial List, Dict or Set type.
            # TODO: Don't infer argument expression twice.
            if (
                typename in self.item_args
                and methodname in self.item_args[typename]
                and e.arg_kinds == [ARG_POS]
            ):
                item_type = self.accept(e.args[0])
                full_item_type = UnionType.make_simplified_union(
                    [item_type, partial_type.inner_types[0]]
                )
                if mypy.checker.is_valid_inferred_type(full_item_type):
                    var.type = self.chk.named_generic_type(typename, [full_item_type])
                    del partial_types[var]
            elif (
                typename in self.container_args
                and methodname in self.container_args[typename]
                and e.arg_kinds == [ARG_POS]
            ):
                arg_type = self.accept(e.args[0])
                if isinstance(arg_type, Instance):
                    arg_typename = arg_type.type.fullname()
                    if arg_typename in self.container_args[typename][methodname]:
                        full_item_types = [
                            UnionType.make_simplified_union([item_type, prev_type])
                            for item_type, prev_type in zip(
                                arg_type.args, partial_type.inner_types
                            )
                        ]
                        if all(
                            mypy.checker.is_valid_inferred_type(item_type)
                            for item_type in full_item_types
                        ):
                            var.type = self.chk.named_generic_type(
                                typename, list(full_item_types)
                            )
                            del partial_types[var]


# Traceback (AttributeError)
Traceback (most recent call last):
File "/usr/lib/python-exec/python3.4/mypy", line 6, in <module>
main(__file__)
File "/usr/lib64/python3.4/site-packages/mypy/main.py", line 40, in main
res = type_check_only(sources, bin_dir, options)
File "/usr/lib64/python3.4/site-packages/mypy/main.py", line 81, in type_check_only
options=options)
File "/usr/lib64/python3.4/site-packages/mypy/build.py", line 178, in build
dispatch(sources, manager)
File "/usr/lib64/python3.4/site-packages/mypy/build.py", line 1325, in dispatch
process_graph(graph, manager)
File "/usr/lib64/python3.4/site-packages/mypy/build.py", line 1468, in process_graph
process_stale_scc(graph, scc)
File "/usr/lib64/python3.4/site-packages/mypy/build.py", line 1545, in process_stale_scc
graph[id].type_check()
File "/usr/lib64/python3.4/site-packages/mypy/build.py", line 1303, in type_check
manager.type_checker.visit_file(self.tree, self.xpath)
File "/usr/lib64/python3.4/site-packages/mypy/checker.py", line 152, in visit_file
self.accept(d)
File "/usr/lib64/python3.4/site-packages/mypy/checker.py", line 201, in accept
typ = node.accept(self)
File "/usr/lib64/python3.4/site-packages/mypy/nodes.py", line 476, in accept
return visitor.visit_func_def(self)
File "/usr/lib64/python3.4/site-packages/mypy/checker.py", line 399, in visit_func_def
self.check_func_item(defn, name=defn.name())
File "/usr/lib64/python3.4/site-packages/mypy/checker.py", line 466, in check_func_item
self.leave_partial_types()
File "/usr/lib64/python3.4/site-packages/mypy/checker.py", line 2191, in leave_partial_types
if experiments.STRICT_OPTIONAL and cast(PartialType, var.type).type is None:
AttributeError: 'AnyType' object has no attribute 'type'

*** INTERNAL ERROR ***

/tmp/asd8.py:3: error: Internal error -- please report a bug at https://github.com/python/mypy/issues

NOTE: you can use "mypy --pdb ..." to drop into the debugger when this happens.

# Fixed code
def try_infer_partial_type(self, e: CallExpr) -> None:
    if isinstance(e.callee, MemberExpr) and isinstance(e.callee.expr, RefExpr):
        var = cast(Var, e.callee.expr.node)
        partial_types = self.chk.find_partial_types(var)
        if partial_types is not None and not self.chk.current_node_deferred:
            partial_type = var.type
            if (
                partial_type is None
                or not isinstance(partial_type, PartialType)
                or partial_type.type is None
            ):
                # A partial None type -> can't infer anything.
                return
            typename = partial_type.type.fullname()
            methodname = e.callee.name
            # Sometimes we can infer a full type for a partial List, Dict or Set type.
            # TODO: Don't infer argument expression twice.
            if (
                typename in self.item_args
                and methodname in self.item_args[typename]
                and e.arg_kinds == [ARG_POS]
            ):
                item_type = self.accept(e.args[0])
                full_item_type = UnionType.make_simplified_union(
                    [item_type, partial_type.inner_types[0]]
                )
                if mypy.checker.is_valid_inferred_type(full_item_type):
                    var.type = self.chk.named_generic_type(typename, [full_item_type])
                    del partial_types[var]
            elif (
                typename in self.container_args
                and methodname in self.container_args[typename]
                and e.arg_kinds == [ARG_POS]
            ):
                arg_type = self.accept(e.args[0])
                if isinstance(arg_type, Instance):
                    arg_typename = arg_type.type.fullname()
                    if arg_typename in self.container_args[typename][methodname]:
                        full_item_types = [
                            UnionType.make_simplified_union([item_type, prev_type])
                            for item_type, prev_type in zip(
                                arg_type.args, partial_type.inner_types
                            )
                        ]
                        if all(
                            mypy.checker.is_valid_inferred_type(item_type)
                            for item_type in full_item_types
                        ):
                            var.type = self.chk.named_generic_type(
                                typename, list(full_item_types)
                            )
                            del partial_types[var]