# Buggy code
def leave_partial_types(self) -> None:
    """Pop partial type scope.

    Also report errors for variables which still have partial
    types, i.e. we couldn't infer a complete type.
    """
    partial_types = self.partial_types.pop()
    if not self.current_node_deferred:
        for var, context in partial_types.items():
            if experiments.STRICT_OPTIONAL and cast(PartialType, var.type).type is None:
                # None partial type: assume variable is intended to have type None
                var.type = NoneTyp()
            else:
                self.msg.fail(messages.NEED_ANNOTATION_FOR_VAR, context)
                var.type = AnyType()


# 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 leave_partial_types(self) -> None:
    """Pop partial type scope.

    Also report errors for variables which still have partial
    types, i.e. we couldn't infer a complete type.
    """
    partial_types = self.partial_types.pop()
    if not self.current_node_deferred:
        for var, context in partial_types.items():
            if (
                experiments.STRICT_OPTIONAL
                and isinstance(var.type, PartialType)
                and var.type.type is None
            ):
                # None partial type: assume variable is intended to have type None
                var.type = NoneTyp()
            else:
                self.msg.fail(messages.NEED_ANNOTATION_FOR_VAR, context)
                var.type = AnyType()