# Buggy code
def lookup(self, name: str, ctx: Context) -> SymbolTableNode:
    """Look up an unqualified name in all active namespaces."""
    # 1a. Name declared using 'global x' takes precedence
    if name in self.global_decls[-1]:
        if name in self.globals:
            return self.globals[name]
        else:
            self.name_not_defined(name, ctx)
            return None
    # 1b. Name declared using 'nonlocal x' takes precedence
    if name in self.nonlocal_decls[-1]:
        for table in reversed(self.locals[:-1]):
            if table is not None and name in table:
                return table[name]
        else:
            self.name_not_defined(name, ctx)
            return None
    # 2. Class attributes (if within class definition)
    if self.is_class_scope() and name in self.type.names:
        return self.type[name]
    # 3. Local (function) scopes
    for table in reversed(self.locals):
        if table is not None and name in table:
            return table[name]
    # 4. Current file global scope
    if name in self.globals:
        return self.globals[name]
    # 5. Builtins
    b = self.globals.get("__builtins__", None)
    if b:
        table = cast(MypyFile, b.node).names
        if name in table:
            if name[0] == "_" and name[1] != "_":
                self.name_not_defined(name, ctx)
                return None
            node = table[name]
            return node
    # Give up.
    self.name_not_defined(name, ctx)
    self.check_for_obsolete_short_name(name, ctx)
    return None


# Traceback (KeyError)
Traceback (most recent call last):
File "/home/machinalis/.virtualenvs/mypy-django/bin/mypy", line 6, in <module>
exec(compile(open(__file__).read(), __file__, 'exec'))
File "/home/machinalis/oss/mypy/scripts/mypy", line 6, in <module>
main(__file__)
File "/home/machinalis/oss/mypy/mypy/main.py", line 38, in main
res = type_check_only(sources, bin_dir, options)
File "/home/machinalis/oss/mypy/mypy/main.py", line 79, in type_check_only
options=options)
File "/home/machinalis/oss/mypy/mypy/build.py", line 180, in build
dispatch(sources, manager)
File "/home/machinalis/oss/mypy/mypy/build.py", line 1335, in dispatch
process_graph(graph, manager)
File "/home/machinalis/oss/mypy/mypy/build.py", line 1478, in process_graph
process_stale_scc(graph, scc)
File "/home/machinalis/oss/mypy/mypy/build.py", line 1551, in process_stale_scc
graph[id].semantic_analysis()
File "/home/machinalis/oss/mypy/mypy/build.py", line 1300, in semantic_analysis
self.manager.semantic_analyzer.visit_file(self.tree, self.xpath)
File "/home/machinalis/oss/mypy/mypy/semanal.py", line 244, in visit_file
self.accept(d)
File "/home/machinalis/oss/mypy/mypy/semanal.py", line 2462, in accept
node.accept(self)
File "/home/machinalis/oss/mypy/mypy/nodes.py", line 664, in accept
return visitor.visit_class_def(self)
File "/home/machinalis/oss/mypy/mypy/semanal.py", line 572, in visit_class_def
defn.defs.accept(self)
File "/home/machinalis/oss/mypy/mypy/nodes.py", line 727, in accept
return visitor.visit_block(self)
File "/home/machinalis/oss/mypy/mypy/semanal.py", line 1028, in visit_block
self.accept(s)
File "/home/machinalis/oss/mypy/mypy/semanal.py", line 2462, in accept
node.accept(self)
File "/home/machinalis/oss/mypy/mypy/nodes.py", line 765, in accept
return visitor.visit_assignment_stmt(self)
File "/home/machinalis/oss/mypy/mypy/semanal.py", line 1060, in visit_assignment_stmt
s.rvalue.accept(self)
File "/home/machinalis/oss/mypy/mypy/nodes.py", line 1116, in accept
return visitor.visit_name_expr(self)
File "/home/machinalis/oss/mypy/mypy/semanal.py", line 1906, in visit_name_expr
n = self.lookup(expr.name, expr)
File "/home/machinalis/oss/mypy/mypy/semanal.py", line 2250, in lookup
return self.type[name]
File "/home/machinalis/oss/mypy/mypy/nodes.py", line 1864, in __getitem__
raise KeyError(name)
KeyError: 'readlines'

*** INTERNAL ERROR ***

myiter.py:7: 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 lookup(self, name: str, ctx: Context) -> SymbolTableNode:
    """Look up an unqualified name in all active namespaces."""
    # 1a. Name declared using 'global x' takes precedence
    if name in self.global_decls[-1]:
        if name in self.globals:
            return self.globals[name]
        else:
            self.name_not_defined(name, ctx)
            return None
    # 1b. Name declared using 'nonlocal x' takes precedence
    if name in self.nonlocal_decls[-1]:
        for table in reversed(self.locals[:-1]):
            if table is not None and name in table:
                return table[name]
        else:
            self.name_not_defined(name, ctx)
            return None
    # 2. Class attributes (if within class definition)
    if self.is_class_scope() and name in self.type.names:
        return self.type.names[name]
    # 3. Local (function) scopes
    for table in reversed(self.locals):
        if table is not None and name in table:
            return table[name]
    # 4. Current file global scope
    if name in self.globals:
        return self.globals[name]
    # 5. Builtins
    b = self.globals.get("__builtins__", None)
    if b:
        table = cast(MypyFile, b.node).names
        if name in table:
            if name[0] == "_" and name[1] != "_":
                self.name_not_defined(name, ctx)
                return None
            node = table[name]
            return node
    # Give up.
    self.name_not_defined(name, ctx)
    self.check_for_obsolete_short_name(name, ctx)
    return None