# 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,
) -> Type:
    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.type, is_classmethod, builtin_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 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)
Traceback (most recent call last):
File "C:\Users\fred\Anaconda3\Scripts\mypy", line 6, in <module>
main(__file__)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\main.py", line 54, in main
res = type_check_only(sources, bin_dir, options)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\main.py", line 102, in type_check_only
python_path=options.python_path)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\build.py", line 211, in build
dispatch(sources, manager)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\build.py", line 1336, in dispatch
process_graph(graph, manager)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\build.py", line 1467, in process_graph
process_stale_scc(graph, scc)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\build.py", line 1497, in process_stale_scc
graph[id].type_check()
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\build.py", line 1316, in type_check
manager.type_checker.visit_file(self.tree, self.xpath)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\checker.py", line 426, in visit_file
self.accept(d)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\checker.py", line 476, in accept
typ = node.accept(self)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\nodes.py", line 531, in accept
return visitor.visit_decorator(self)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\checker.py", line 1956, in visit_decorator
dec = self.accept(d)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\checker.py", line 476, in accept
typ = node.accept(self)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\nodes.py", line 1185, in accept
return visitor.visit_call_expr(self)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\checker.py", line 2009, in visit_call_expr
return self.expr_checker.visit_call_expr(e)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\checkexpr.py", line 135, in visit_call_expr
callee_type = self.accept(e.callee)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\checkexpr.py", line 1531, in accept
return self.chk.accept(node, context)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\checker.py", line 476, in accept
typ = node.accept(self)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\nodes.py", line 1141, in accept
return visitor.visit_member_expr(self)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\checker.py", line 2053, in visit_member_expr
return self.expr_checker.visit_member_expr(e)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\checkexpr.py", line 836, in visit_member_expr
result = self.analyze_ordinary_member_access(e, False)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\checkexpr.py", line 847, in analyze_ordinary_member_access
return analyze_member_access(e.name, self.accept(e.expr), e,
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\checkexpr.py", line 1531, in accept
return self.chk.accept(node, context)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\checker.py", line 476, in accept
typ = node.accept(self)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\nodes.py", line 1141, in accept
return visitor.visit_member_expr(self)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\checker.py", line 2053, in visit_member_expr
return self.expr_checker.visit_member_expr(e)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\checkexpr.py", line 836, in visit_member_expr
result = self.analyze_ordinary_member_access(e, False)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\checkexpr.py", line 849, in analyze_ordinary_member_access
self.named_type, self.not_ready_callback, self.msg)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\checkmember.py", line 96, in analyze_member_access
builtin_type, not_ready_callback, msg)
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\checkmember.py", line 319, in analyze_class_attribute_access
return function_type(cast(FuncBase, node.node), builtin_type('builtins.function'))
File "C:\Users\fred\Anaconda3\lib\site-packages\mypy\nodes.py", line 2129, in function_type
if func.type:
AttributeError: 'MypyFile' 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,
) -> Type:
    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.type, is_classmethod, builtin_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")
        )