# Buggy code
def load_graph(sources: List[BuildSource], manager: BuildManager) -> Graph:
    """Given some source files, load the full dependency graph."""
    graph = {}  # type: Graph
    # The deque is used to implement breadth-first traversal.
    # TODO: Consider whether to go depth-first instead.  This may
    # affect the order in which we process files within import cycles.
    new = collections.deque()  # type: collections.deque[State]
    # Seed the graph with the initial root sources.
    for bs in sources:
        try:
            st = State(id=bs.module, path=bs.path, source=bs.text, manager=manager)
        except ModuleNotFound:
            continue
        if st.id in graph:
            manager.errors.set_file(st.xpath)
            manager.errors.report(-1, "Duplicate module named '%s'" % st.id)
            manager.errors.raise_error()
        graph[st.id] = st
        new.append(st)
    # Collect dependencies.  We go breadth-first.
    while new:
        st = new.popleft()
        for dep in st.ancestors + st.dependencies:
            if dep not in graph:
                try:
                    if dep in st.ancestors:
                        # TODO: Why not 'if dep not in st.dependencies' ?
                        # Ancestors don't have import context.
                        newst = State(
                            id=dep,
                            path=None,
                            source=None,
                            manager=manager,
                            ancestor_for=st,
                        )
                    else:
                        newst = State(
                            id=dep,
                            path=None,
                            source=None,
                            manager=manager,
                            caller_state=st,
                            caller_line=st.dep_line_map.get(dep, 1),
                        )
                except ModuleNotFound:
                    if dep in st.dependencies:
                        st.dependencies.remove(dep)
                        st.suppressed.append(dep)
                else:
                    assert newst.id not in graph, newst.id
                    graph[newst.id] = newst
                    new.append(newst)
            if dep in st.ancestors and dep in graph:
                graph[dep].child_modules.add(st.id)
    for id, g in graph.items():
        if g.has_new_submodules():
            g.parse_file()
    return graph


# Traceback (TypeError)
testdir/main.py:4: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.5.2/Frameworks/Python.framework/Versions/3.5/lib/python3.5/runpy.py", line 184, in _run_module_as_main
"__main__", mod_spec)
File "/usr/local/Cellar/python3/3.5.2/Frameworks/Python.framework/Versions/3.5/lib/python3.5/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/__main__.py", line 5, in <module>
main(None)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/main.py", line 40, in main
res = type_check_only(sources, bin_dir, options)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/main.py", line 81, in type_check_only
options=options)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/build.py", line 180, in build
dispatch(sources, manager)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/build.py", line 1335, in dispatch
process_graph(graph, manager)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/build.py", line 1478, in process_graph
process_stale_scc(graph, scc)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/build.py", line 1555, in process_stale_scc
graph[id].type_check()
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/build.py", line 1313, in type_check
manager.type_checker.visit_file(self.tree, self.xpath)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/checker.py", line 152, in visit_file
self.accept(d)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/checker.py", line 201, in accept
typ = node.accept(self)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/nodes.py", line 477, in accept
return visitor.visit_func_def(self)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/checker.py", line 408, in visit_func_def
self.check_func_item(defn, name=defn.name())
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/checker.py", line 471, in check_func_item
self.check_func_def(defn, typ, name)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/checker.py", line 591, in check_func_def
self.accept(item.body)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/checker.py", line 201, in accept
typ = node.accept(self)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/nodes.py", line 727, in accept
return visitor.visit_block(self)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/checker.py", line 999, in visit_block
self.accept(s)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/checker.py", line 201, in accept
typ = node.accept(self)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/nodes.py", line 741, in accept
return visitor.visit_expression_stmt(self)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/checker.py", line 1437, in visit_expression_stmt
self.accept(s.expr)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/checker.py", line 201, in accept
typ = node.accept(self)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/nodes.py", line 1157, in accept
return visitor.visit_member_expr(self)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/checker.py", line 1978, in visit_member_expr
return self.expr_checker.visit_member_expr(e)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/checkexpr.py", line 861, in visit_member_expr
result = self.analyze_ordinary_member_access(e, False)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/checkexpr.py", line 874, in analyze_ordinary_member_access
self.named_type, self.not_ready_callback, self.msg)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/checkmember.py", line 77, in analyze_member_access
report_type=report_type)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/checkmember.py", line 174, in analyze_member_var_access
v = lookup_member_var_or_accessor(info, name, is_lvalue)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/checkmember.py", line 267, in lookup_member_var_or_accessor
node = info.get(name)
File "/Users/me/ve/35/lib/python3.5/site-packages/mypy/nodes.py", line 1853, in get
for cls in self.mro:
TypeError: 'NoneType' object is not iterable
testdir/main.py:4: note: use --pdb to drop into pdb

# Fixed code
def load_graph(sources: List[BuildSource], manager: BuildManager) -> Graph:
    """Given some source files, load the full dependency graph."""
    graph = {}  # type: Graph
    # The deque is used to implement breadth-first traversal.
    # TODO: Consider whether to go depth-first instead.  This may
    # affect the order in which we process files within import cycles.
    new = collections.deque()  # type: collections.deque[State]
    # Seed the graph with the initial root sources.
    for bs in sources:
        try:
            st = State(id=bs.module, path=bs.path, source=bs.text, manager=manager)
        except ModuleNotFound:
            continue
        if st.id in graph:
            manager.errors.set_file(st.xpath)
            manager.errors.report(-1, "Duplicate module named '%s'" % st.id)
            manager.errors.raise_error()
        graph[st.id] = st
        new.append(st)
    # Collect dependencies.  We go breadth-first.
    while new:
        st = new.popleft()
        for dep in st.ancestors + st.dependencies + st.suppressed:
            if dep not in graph:
                try:
                    if dep in st.ancestors:
                        # TODO: Why not 'if dep not in st.dependencies' ?
                        # Ancestors don't have import context.
                        newst = State(
                            id=dep,
                            path=None,
                            source=None,
                            manager=manager,
                            ancestor_for=st,
                        )
                    else:
                        newst = State(
                            id=dep,
                            path=None,
                            source=None,
                            manager=manager,
                            caller_state=st,
                            caller_line=st.dep_line_map.get(dep, 1),
                        )
                except ModuleNotFound:
                    if dep in st.dependencies:
                        st.dependencies.remove(dep)
                        st.suppressed.append(dep)
                else:
                    assert newst.id not in graph, newst.id
                    graph[newst.id] = newst
                    new.append(newst)
            if dep in st.ancestors and dep in graph:
                graph[dep].child_modules.add(st.id)
            if dep in graph and dep in st.suppressed:
                # Previously suppressed file is now visible
                if dep in st.suppressed:
                    st.suppressed.remove(dep)
                    st.dependencies.append(dep)
    for id, g in graph.items():
        if g.has_new_submodules():
            g.parse_file()
    return graph