# Buggy code
def enclosing_class(self) -> Optional[TypeInfo]:
    top = self.top_function()
    assert top, "This method must be called from inside a function"
    index = self.stack.index(top)
    assert index, "CheckerScope stack must always start with a module"
    enclosing = self.stack[index - 1]
    if isinstance(enclosing, TypeInfo):
        return enclosing
    return None


# Traceback (AssertionError)
Traceback (most recent call last):
File "/home/derrick_chambers/anaconda3/bin/mypy", line 11, in <module>
sys.exit(console_entry())
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/__main__.py", line 7, in console_entry
main(None)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/main.py", line 91, in main
res = type_check_only(sources, bin_dir, options, flush_errors, fscache)  # noqa
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/main.py", line 148, in type_check_only
fscache=fscache)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/build.py", line 183, in build
flush_errors, fscache)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/build.py", line 356, in _build
graph = dispatch(sources, manager)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/build.py", line 2543, in dispatch
process_graph(graph, manager)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/build.py", line 2840, in process_graph
process_stale_scc(graph, scc, manager)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/build.py", line 2963, in process_stale_scc
graph[id].type_check_first_pass()
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/build.py", line 2103, in type_check_first_pass
self.type_checker().check_first_pass()
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/checker.py", line 256, in check_first_pass
self.accept(d)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/checker.py", line 355, in accept
stmt.accept(self)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/nodes.py", line 820, in accept
return visitor.visit_class_def(self)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/checker.py", line 1461, in visit_class_def
self.accept(defn.defs)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/checker.py", line 355, in accept
stmt.accept(self)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/nodes.py", line 885, in accept
return visitor.visit_block(self)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/checker.py", line 1607, in visit_block
self.accept(s)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/checker.py", line 355, in accept
stmt.accept(self)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/nodes.py", line 603, in accept
return visitor.visit_func_def(self)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/checker.py", line 666, in visit_func_def
self._visit_func_def(defn)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/checker.py", line 670, in _visit_func_def
self.check_func_item(defn, name=defn.name())
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/checker.py", line 732, in check_func_item
self.check_func_def(defn, typ, name)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/checker.py", line 898, in check_func_def
self.accept(item.body)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/checker.py", line 355, in accept
stmt.accept(self)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/nodes.py", line 885, in accept
return visitor.visit_block(self)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/checker.py", line 1607, in visit_block
self.accept(s)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/checker.py", line 355, in accept
stmt.accept(self)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/nodes.py", line 933, in accept
return visitor.visit_assignment_stmt(self)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/checker.py", line 1614, in visit_assignment_stmt
self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/checker.py", line 1663, in check_assignment
if self.check_compatibility_all_supers(lvalue, lvalue_type, rvalue):
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/checker.py", line 1755, in check_compatibility_all_supers
base_type, base_node = self.lvalue_type_from_base(lvalue_node, base)
File "/home/derrick_chambers/anaconda3/lib/python3.7/site-packages/mypy/checker.py", line 1842, in lvalue_type_from_base
assert self_type is not None, "Internal error: base lookup outside class"
AssertionError: Internal error: base lookup outside class
breakmypy.py:14: : note: use --pdb to drop into pdb

# Fixed code
def enclosing_class(self) -> Optional[TypeInfo]:
    """Is there a class *directly* enclosing this function?"""
    top = self.top_function()
    assert top, "This method must be called from inside a function"
    index = self.stack.index(top)
    assert index, "CheckerScope stack must always start with a module"
    enclosing = self.stack[index - 1]
    if isinstance(enclosing, TypeInfo):
        return enclosing
    return None