# Buggy code
def perform_transform(self, node: Node, transform: Callable[[Type], Type]) -> None:
    """Apply transform to all types associated with node."""
    if isinstance(node, ForStmt):
        if node.index_type:
            node.index_type = transform(node.index_type)
        self.transform_types_in_lvalue(node.index, transform)
    if isinstance(node, WithStmt):
        if node.target_type:
            node.target_type = transform(node.target_type)
        for n in node.target:
            if isinstance(n, NameExpr) and isinstance(n.node, Var) and n.node.type:
                n.node.type = transform(n.node.type)
    if isinstance(
        node, (FuncDef, OverloadedFuncDef, CastExpr, AssignmentStmt, TypeAliasExpr, Var)
    ):
        assert node.type, "Scheduled patch for non-existent type"
        node.type = transform(node.type)
    if isinstance(node, TypeAlias):
        node.target = transform(node.target)
    if isinstance(node, NewTypeExpr):
        assert node.old_type, "Scheduled patch for non-existent type"
        node.old_type = transform(node.old_type)
        if node.info:
            new_bases = []  # type: List[Instance]
            for b in node.info.bases:
                new_b = transform(b)
                # TODO: this code can be combined with code in second pass.
                if isinstance(new_b, Instance):
                    new_bases.append(new_b)
                elif isinstance(new_b, TupleType):
                    new_bases.append(new_b.fallback)
                else:
                    self.fail(
                        "Argument 2 to NewType(...) must be subclassable"
                        " (got {})".format(new_b),
                        node,
                    )
                    new_bases.append(self.builtin_type("object"))
            node.info.bases = new_bases
    if isinstance(node, TypeVarExpr):
        if node.upper_bound:
            node.upper_bound = transform(node.upper_bound)
        if node.values:
            node.values = [transform(v) for v in node.values]
    if isinstance(node, TypedDictExpr):
        assert node.info.typeddict_type, "Scheduled patch for non-existent type"
        node.info.typeddict_type = cast(
            TypedDictType, transform(node.info.typeddict_type)
        )
    if isinstance(node, NamedTupleExpr):
        assert node.info.tuple_type, "Scheduled patch for non-existent type"
        node.info.tuple_type = cast(TupleType, transform(node.info.tuple_type))
    if isinstance(node, TypeApplication):
        node.types = [transform(t) for t in node.types]
    if isinstance(node, TypeInfo):
        for tvar in node.defn.type_vars:
            if tvar.upper_bound:
                tvar.upper_bound = transform(tvar.upper_bound)
            if tvar.values:
                tvar.values = [transform(v) for v in tvar.values]
        new_bases = []
        for base in node.bases:
            new_base = transform(base)
            if isinstance(new_base, Instance):
                new_bases.append(new_base)
            else:
                # Don't fix the NamedTuple bases, they are Instance's intentionally.
                # Patch the 'args' just in case, although generic tuple types are
                # not supported yet.
                alt_base = Instance(base.type, [transform(a) for a in base.args])
                new_bases.append(alt_base)
        node.bases = new_bases


# Traceback (TypeError)
typetest.py:5: error: Recursive types not fully supported yet, nested types replaced with "Any"
typetest.py: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.610
Traceback (most recent call last):
File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/jo/.local/lib/python3.6/site-packages/mypy/__main__.py", line 11, in <module>
main(None)
File "/home/jo/.local/lib/python3.6/site-packages/mypy/main.py", line 91, in main
res = type_check_only(sources, bin_dir, options, flush_errors, fscache)  # noqa
File "/home/jo/.local/lib/python3.6/site-packages/mypy/main.py", line 148, in type_check_only
fscache=fscache)
File "/home/jo/.local/lib/python3.6/site-packages/mypy/build.py", line 185, in build
flush_errors, fscache)
File "/home/jo/.local/lib/python3.6/site-packages/mypy/build.py", line 285, in _build
graph = dispatch(sources, manager)
File "/home/jo/.local/lib/python3.6/site-packages/mypy/build.py", line 2398, in dispatch
process_graph(graph, manager)
File "/home/jo/.local/lib/python3.6/site-packages/mypy/build.py", line 2691, in process_graph
process_stale_scc(graph, scc, manager)
File "/home/jo/.local/lib/python3.6/site-packages/mypy/build.py", line 2820, in process_stale_scc
graph[id].finish_passes()
File "/home/jo/.local/lib/python3.6/site-packages/mypy/build.py", line 2067, in finish_passes
manager.report_file(self.tree, self.type_map(), self.options)
File "/usr/lib/python3.6/contextlib.py", line 99, in __exit__
self.gen.throw(type, value, traceback)
File "/home/jo/.local/lib/python3.6/site-packages/mypy/build.py", line 1818, in wrap_context
yield
File "/home/jo/.local/lib/python3.6/site-packages/mypy/build.py", line 2062, in finish_passes
self._patch_indirect_dependencies(self.type_checker().module_refs, self.type_map())
File "/home/jo/.local/lib/python3.6/site-packages/mypy/build.py", line 2076, in _patch_indirect_dependencies
encountered = self.manager.indirection_detector.find_modules(types) | module_refs
File "/home/jo/.local/lib/python3.6/site-packages/mypy/indirection.py", line 29, in find_modules
return self._visit(*typs)
File "/home/jo/.local/lib/python3.6/site-packages/mypy/indirection.py", line 37, in _visit
modules = typ.accept(self)
File "/home/jo/.local/lib/python3.6/site-packages/mypy/types.py", line 1042, in accept
return visitor.visit_tuple_type(self)
File "/home/jo/.local/lib/python3.6/site-packages/mypy/indirection.py", line 91, in visit_tuple_type
return self._visit(*t.items) | self._visit(t.fallback)
File "/home/jo/.local/lib/python3.6/site-packages/mypy/indirection.py", line 37, in _visit
modules = typ.accept(self)
File "/home/jo/.local/lib/python3.6/site-packages/mypy/types.py", line 512, in accept
return visitor.visit_instance(self)
File "/home/jo/.local/lib/python3.6/site-packages/mypy/indirection.py", line 70, in visit_instance
out = self._visit(*t.args)
File "/home/jo/.local/lib/python3.6/site-packages/mypy/indirection.py", line 37, in _visit
modules = typ.accept(self)
File "/home/jo/.local/lib/python3.6/site-packages/mypy/types.py", line 512, in accept
return visitor.visit_instance(self)
File "/home/jo/.local/lib/python3.6/site-packages/mypy/indirection.py", line 75, in visit_instance
for s in t.type.mro:
TypeError: 'NoneType' object is not iterable
typetest.py: : note: use --pdb to drop into pdb

# Fixed code
def perform_transform(self, node: Node, transform: Callable[[Type], Type]) -> None:
    """Apply transform to all types associated with node."""
    if isinstance(node, ForStmt):
        if node.index_type:
            node.index_type = transform(node.index_type)
        self.transform_types_in_lvalue(node.index, transform)
    if isinstance(node, WithStmt):
        if node.target_type:
            node.target_type = transform(node.target_type)
        for n in node.target:
            if isinstance(n, NameExpr) and isinstance(n.node, Var) and n.node.type:
                n.node.type = transform(n.node.type)
    if isinstance(
        node, (FuncDef, OverloadedFuncDef, CastExpr, AssignmentStmt, TypeAliasExpr, Var)
    ):
        assert node.type, "Scheduled patch for non-existent type"
        node.type = transform(node.type)
    if isinstance(node, TypeAlias):
        node.target = transform(node.target)
    if isinstance(node, NewTypeExpr):
        assert node.old_type, "Scheduled patch for non-existent type"
        node.old_type = transform(node.old_type)
        if node.info:
            new_bases = []  # type: List[Instance]
            for b in node.info.bases:
                new_b = transform(b)
                # TODO: this code can be combined with code in second pass.
                if isinstance(new_b, Instance):
                    new_bases.append(new_b)
                elif isinstance(new_b, TupleType):
                    new_bases.append(new_b.fallback)
                else:
                    self.fail(
                        "Argument 2 to NewType(...) must be subclassable"
                        " (got {})".format(new_b),
                        node,
                    )
                    new_bases.append(self.builtin_type("object"))
            node.info.bases = new_bases
    if isinstance(node, TypeVarExpr):
        if node.upper_bound:
            node.upper_bound = transform(node.upper_bound)
        if node.values:
            node.values = [transform(v) for v in node.values]
    if isinstance(node, TypedDictExpr):
        assert node.info.typeddict_type, "Scheduled patch for non-existent type"
        node.info.typeddict_type = cast(
            TypedDictType, transform(node.info.typeddict_type)
        )
    if isinstance(node, NamedTupleExpr):
        assert node.info.tuple_type, "Scheduled patch for non-existent type"
        node.info.tuple_type = cast(TupleType, transform(node.info.tuple_type))
    if isinstance(node, TypeApplication):
        node.types = [transform(t) for t in node.types]
    if isinstance(node, TypeInfo):
        for tvar in node.defn.type_vars:
            if tvar.upper_bound:
                tvar.upper_bound = transform(tvar.upper_bound)
            if tvar.values:
                tvar.values = [transform(v) for v in tvar.values]
        new_bases = []
        for base in node.bases:
            new_base = transform(base)
            if isinstance(new_base, Instance):
                new_bases.append(new_base)
            else:
                # Don't fix the NamedTuple bases, they are Instance's intentionally.
                # Patch the 'args' just in case, although generic tuple types are
                # not supported yet.
                alt_base = Instance(base.type, [transform(a) for a in base.args])
                new_bases.append(alt_base)
        node.bases = new_bases
        if node.tuple_type:
            new_tuple_type = transform(node.tuple_type)
            assert isinstance(new_tuple_type, TupleType)
            node.tuple_type = new_tuple_type