# Buggy code
def analyze_class_attribute_access(
    itype: Instance,
    name: str,
    context: Context,
    is_lvalue: bool,
    builtin_type: Callable[[str], Instance],
    not_ready_callback: Callable[[str, Context], None],
    msg: MessageBuilder,
    original_type: Type,
) -> Type:
    """original_type is the type of E in the expression E.var"""
    node = itype.type.get(name)
    if not node:
        if itype.type.fallback_to_any:
            return AnyType()
        return None

    is_decorated = isinstance(node.node, Decorator)
    is_method = is_decorated or isinstance(node.node, FuncDef)
    if is_lvalue:
        if is_method:
            msg.cant_assign_to_method(context)
        if isinstance(node.node, TypeInfo):
            msg.fail(messages.CANNOT_ASSIGN_TO_TYPE, context)

    if itype.type.is_enum and not (is_lvalue or is_decorated or is_method):
        return itype

    t = node.type
    if t:
        if isinstance(t, PartialType):
            return handle_partial_attribute_type(t, is_lvalue, msg, node.node)
        is_classmethod = is_decorated and cast(Decorator, node.node).func.is_class
        return add_class_tvars(t, itype, is_classmethod, builtin_type, original_type)
    elif isinstance(node.node, Var):
        not_ready_callback(name, context)
        return AnyType()

    if isinstance(node.node, TypeInfo):
        return type_object_type(node.node, builtin_type)

    if isinstance(node.node, MypyFile):
        # Reference to a module object.
        return builtin_type("builtins.module")

    if is_decorated:
        # TODO: Return type of decorated function. This is quick hack to work around #998.
        return AnyType()
    else:
        return function_type(
            cast(FuncBase, node.node), builtin_type("builtins.function")
        )


# Traceback (AttributeError)
test2.py: note: In function "example":
test2.py:9: error: Invalid type "A"
test2.py:8: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues
Traceback (most recent call last):
File "/usr/local/bin/mypy", line 6, in <module>
main(__file__)
File "/usr/local/lib/python3.5/dist-packages/mypy/main.py", line 41, in main
res = type_check_only(sources, bin_dir, options)
File "/usr/local/lib/python3.5/dist-packages/mypy/main.py", line 86, in type_check_only
options=options)
File "/usr/local/lib/python3.5/dist-packages/mypy/build.py", line 183, in build
dispatch(sources, manager)
File "/usr/local/lib/python3.5/dist-packages/mypy/build.py", line 1512, in dispatch
process_graph(graph, manager)
File "/usr/local/lib/python3.5/dist-packages/mypy/build.py", line 1692, in process_graph
process_stale_scc(graph, scc)
File "/usr/local/lib/python3.5/dist-packages/mypy/build.py", line 1771, in process_stale_scc
graph[id].type_check_first_pass()
File "/usr/local/lib/python3.5/dist-packages/mypy/build.py", line 1440, in type_check_first_pass
self.type_checker.check_first_pass()
File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 174, in check_first_pass
self.accept(d)
File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 243, in accept
typ = node.accept(self)
File "/usr/local/lib/python3.5/dist-packages/mypy/nodes.py", line 710, in accept
return visitor.visit_class_def(self)
File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 992, in visit_class_def
self.accept(defn.defs)
File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 243, in accept
typ = node.accept(self)
File "/usr/local/lib/python3.5/dist-packages/mypy/nodes.py", line 771, in accept
return visitor.visit_block(self)
File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 1087, in visit_block
self.accept(s)
File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 243, in accept
typ = node.accept(self)
File "/usr/local/lib/python3.5/dist-packages/mypy/nodes.py", line 597, in accept
return visitor.visit_decorator(self)
File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 1882, in visit_decorator
e.func.accept(self)
File "/usr/local/lib/python3.5/dist-packages/mypy/nodes.py", line 532, in accept
return visitor.visit_func_def(self)
File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 455, in visit_func_def
self.check_func_item(defn, name=defn.name())
File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 518, in check_func_item
self.check_func_def(defn, typ, name)
File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 648, in check_func_def
self.accept(item.body)
File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 243, in accept
typ = node.accept(self)
File "/usr/local/lib/python3.5/dist-packages/mypy/nodes.py", line 771, in accept
return visitor.visit_block(self)
File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 1087, in visit_block
self.accept(s)
File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 243, in accept
typ = node.accept(self)
File "/usr/local/lib/python3.5/dist-packages/mypy/nodes.py", line 812, in accept
return visitor.visit_assignment_stmt(self)
File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 1095, in visit_assignment_stmt
self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 1161, in check_assignment
self.infer_variable_type(inferred, lvalue, self.accept(rvalue),
File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 243, in accept
typ = node.accept(self)
File "/usr/local/lib/python3.5/dist-packages/mypy/nodes.py", line 1202, in accept
return visitor.visit_member_expr(self)
File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 2054, in visit_member_expr
return self.expr_checker.visit_member_expr(e)
File "/usr/local/lib/python3.5/dist-packages/mypy/checkexpr.py", line 889, in visit_member_expr
result = self.analyze_ordinary_member_access(e, False)
File "/usr/local/lib/python3.5/dist-packages/mypy/checkexpr.py", line 904, in analyze_ordinary_member_access
original_type=original_type, chk=self.chk)
File "/usr/local/lib/python3.5/dist-packages/mypy/checkmember.py", line 142, in analyze_member_access
original_type=original_type)
File "/usr/local/lib/python3.5/dist-packages/mypy/checkmember.py", line 387, in analyze_class_attribute_access
return function_type(cast(FuncBase, node.node), builtin_type('builtins.function'))
File "/usr/local/lib/python3.5/dist-packages/mypy/types.py", line 1508, in function_type
if func.type:
AttributeError: 'TypeVarExpr' object has no attribute 'type'

# Fixed code
def analyze_class_attribute_access(
    itype: Instance,
    name: str,
    context: Context,
    is_lvalue: bool,
    builtin_type: Callable[[str], Instance],
    not_ready_callback: Callable[[str, Context], None],
    msg: MessageBuilder,
    original_type: Type,
) -> Type:
    """original_type is the type of E in the expression E.var"""
    node = itype.type.get(name)
    if not node:
        if itype.type.fallback_to_any:
            return AnyType()
        return None

    is_decorated = isinstance(node.node, Decorator)
    is_method = is_decorated or isinstance(node.node, FuncDef)
    if is_lvalue:
        if is_method:
            msg.cant_assign_to_method(context)
        if isinstance(node.node, TypeInfo):
            msg.fail(messages.CANNOT_ASSIGN_TO_TYPE, context)

    if itype.type.is_enum and not (is_lvalue or is_decorated or is_method):
        return itype

    t = node.type
    if t:
        if isinstance(t, PartialType):
            return handle_partial_attribute_type(t, is_lvalue, msg, node.node)
        is_classmethod = is_decorated and cast(Decorator, node.node).func.is_class
        return add_class_tvars(t, itype, is_classmethod, builtin_type, original_type)
    elif isinstance(node.node, Var):
        not_ready_callback(name, context)
        return AnyType()

    if isinstance(node.node, TypeVarExpr):
        return TypeVarType(node.tvar_def, node.tvar_def.line, node.tvar_def.column)

    if isinstance(node.node, TypeInfo):
        return type_object_type(node.node, builtin_type)

    if isinstance(node.node, MypyFile):
        # Reference to a module object.
        return builtin_type("builtins.module")

    if is_decorated:
        # TODO: Return type of decorated function. This is quick hack to work around #998.
        return AnyType()
    else:
        return function_type(
            cast(FuncBase, node.node), builtin_type("builtins.function")
        )