# Buggy code
def visit_instance(self, t: Instance) -> None:
    info = t.type
    # Check type argument count.
    if len(t.args) != len(info.type_vars):
        if len(t.args) == 0:
            from_builtins = (
                t.type.fullname() in nongen_builtins and not t.from_generic_builtin
            )
            if (
                "generics" in self.options.disallow_any
                and not self.is_typeshed_stub
                and from_builtins
            ):
                alternative = nongen_builtins[t.type.fullname()]
                self.fail(messages.IMPLICIT_GENERIC_ANY_BUILTIN.format(alternative), t)
            # Insert implicit 'Any' type arguments.
            if from_builtins:
                # this 'Any' was already reported elsewhere
                any_type = AnyType(TypeOfAny.special_form, line=t.line, column=t.column)
            else:
                any_type = AnyType(
                    TypeOfAny.from_omitted_generics, line=t.line, column=t.column
                )
            t.args = [any_type] * len(info.type_vars)
            return
        # Invalid number of type parameters.
        n = len(info.type_vars)
        s = "{} type arguments".format(n)
        if n == 0:
            s = "no type arguments"
        elif n == 1:
            s = "1 type argument"
        act = str(len(t.args))
        if act == "0":
            act = "none"
        self.fail('"{}" expects {}, but {} given'.format(info.name(), s, act), t)
        # Construct the correct number of type arguments, as
        # otherwise the type checker may crash as it expects
        # things to be right.
        t.args = [AnyType(TypeOfAny.from_error) for _ in info.type_vars]
        t.invalid = True
    elif info.defn.type_vars:
        # Check type argument values.
        for (i, arg), tvar in zip(enumerate(t.args), info.defn.type_vars):
            if tvar.values:
                if isinstance(arg, TypeVarType):
                    arg_values = arg.values
                    if not arg_values:
                        self.fail(
                            'Type variable "{}" not valid as type '
                            'argument value for "{}"'.format(arg.name, info.name()),
                            t,
                        )
                        continue
                else:
                    arg_values = [arg]
                self.check_type_var_values(
                    info, arg_values, tvar.name, tvar.values, i + 1, t
                )
            if not is_subtype(arg, tvar.upper_bound):
                self.fail(
                    'Type argument "{}" of "{}" must be a subtype of "{}"'.format(
                        arg, info.name(), tvar.upper_bound
                    ),
                    t,
                )
    for arg in t.args:
        arg.accept(self)
    if info.is_newtype:
        for base in info.bases:
            base.accept(self)


# Traceback (TypeError)
mypy --show-traceback Deduction.py
Deduction.py:102: error: The return type of "__init__" must be None
Deduction.py:103: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.521-858f7512cf3b4e39c0f4e8de5a13eee0e1e138fb-dirty
Traceback (most recent call last):
File "/usr/local/Cellar/mypy/0.521/libexec/bin/mypy", line 11, in <module>
load_entry_point('mypy===0.521-858f7512cf3b4e39c0f4e8de5a13eee0e1e138fb-dirty', 'console_scripts', 'mypy')()
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/__main__.py", line 7, in console_entry
main(None)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/main.py", line 50, in main
res = type_check_only(sources, bin_dir, options)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/main.py", line 97, in type_check_only
options=options)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/build.py", line 196, in build
graph = dispatch(sources, manager)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/build.py", line 1801, in dispatch
process_graph(graph, manager)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/build.py", line 2044, in process_graph
process_stale_scc(graph, scc, manager)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/build.py", line 2147, in process_stale_scc
graph[id].type_check_first_pass()
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/build.py", line 1716, in type_check_first_pass
self.type_checker.check_first_pass()
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 185, in check_first_pass
self.accept(d)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 273, in accept
stmt.accept(self)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/nodes.py", line 754, in accept
return visitor.visit_class_def(self)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 1109, in visit_class_def
self.accept(defn.defs)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 273, in accept
stmt.accept(self)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/nodes.py", line 815, in accept
return visitor.visit_block(self)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 1202, in visit_block
self.accept(s)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 273, in accept
stmt.accept(self)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/nodes.py", line 565, in accept
return visitor.visit_func_def(self)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 519, in visit_func_def
self.check_func_item(defn, name=defn.name())
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 578, in check_func_item
self.check_func_def(defn, typ, name)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 738, in check_func_def
self.accept(item.body)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 273, in accept
stmt.accept(self)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/nodes.py", line 815, in accept
return visitor.visit_block(self)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 1202, in visit_block
self.accept(s)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 273, in accept
stmt.accept(self)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/nodes.py", line 974, in accept
return visitor.visit_if_stmt(self)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 1977, in visit_if_stmt
if_map, else_map = self.find_isinstance_check(e)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 2559, in find_isinstance_check
return find_isinstance_check(n, self.type_map)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 2810, in find_isinstance_check
return conditional_type_map(expr, vartype, type)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 2613, in conditional_type_map
and is_proper_subtype(current_type, proposed_type)):
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/subtypes.py", line 569, in is_proper_subtype
return left.accept(ProperSubtypeVisitor(right))
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/types.py", line 1114, in accept
return visitor.visit_union_type(self)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/subtypes.py", line 693, in visit_union_type
return all([is_proper_subtype(item, self.right) for item in left.items])
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/subtypes.py", line 693, in <listcomp>
return all([is_proper_subtype(item, self.right) for item in left.items])
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/subtypes.py", line 569, in is_proper_subtype
return left.accept(ProperSubtypeVisitor(right))
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/types.py", line 900, in accept
return visitor.visit_tuple_type(self)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/subtypes.py", line 671, in visit_tuple_type
if not is_proper_subtype(l, r):
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/subtypes.py", line 569, in is_proper_subtype
return left.accept(ProperSubtypeVisitor(right))
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/types.py", line 435, in accept
return visitor.visit_instance(self)
File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/subtypes.py", line 605, in visit_instance
for base in left.type.mro:
TypeError: 'NoneType' object is not iterable
Deduction.py:103: note: use --pdb to drop into pdb

# Fixed code
def visit_instance(self, t: Instance) -> None:
    info = t.type
    if info.replaced or info.tuple_type:
        self.indicator["synthetic"] = True
    # Check type argument count.
    if len(t.args) != len(info.type_vars):
        if len(t.args) == 0:
            from_builtins = (
                t.type.fullname() in nongen_builtins and not t.from_generic_builtin
            )
            if (
                "generics" in self.options.disallow_any
                and not self.is_typeshed_stub
                and from_builtins
            ):
                alternative = nongen_builtins[t.type.fullname()]
                self.fail(messages.IMPLICIT_GENERIC_ANY_BUILTIN.format(alternative), t)
            # Insert implicit 'Any' type arguments.
            if from_builtins:
                # this 'Any' was already reported elsewhere
                any_type = AnyType(TypeOfAny.special_form, line=t.line, column=t.column)
            else:
                any_type = AnyType(
                    TypeOfAny.from_omitted_generics, line=t.line, column=t.column
                )
            t.args = [any_type] * len(info.type_vars)
            return
        # Invalid number of type parameters.
        n = len(info.type_vars)
        s = "{} type arguments".format(n)
        if n == 0:
            s = "no type arguments"
        elif n == 1:
            s = "1 type argument"
        act = str(len(t.args))
        if act == "0":
            act = "none"
        self.fail('"{}" expects {}, but {} given'.format(info.name(), s, act), t)
        # Construct the correct number of type arguments, as
        # otherwise the type checker may crash as it expects
        # things to be right.
        t.args = [AnyType(TypeOfAny.from_error) for _ in info.type_vars]
        t.invalid = True
    elif info.defn.type_vars:
        # Check type argument values.
        # TODO: Calling is_subtype and is_same_types in semantic analysis is a bad idea
        for (i, arg), tvar in zip(enumerate(t.args), info.defn.type_vars):
            if tvar.values:
                if isinstance(arg, TypeVarType):
                    arg_values = arg.values
                    if not arg_values:
                        self.fail(
                            'Type variable "{}" not valid as type '
                            'argument value for "{}"'.format(arg.name, info.name()),
                            t,
                        )
                        continue
                else:
                    arg_values = [arg]
                self.check_type_var_values(
                    info, arg_values, tvar.name, tvar.values, i + 1, t
                )
            # TODO: These hacks will be not necessary when this will be moved to later stage.
            arg = self.update_type(arg)
            bound = self.update_type(tvar.upper_bound)
            if not is_subtype(arg, bound):
                self.fail(
                    'Type argument "{}" of "{}" must be a subtype of "{}"'.format(
                        arg, info.name(), bound
                    ),
                    t,
                )
    for arg in t.args:
        arg.accept(self)
    if info.is_newtype:
        for base in info.bases:
            base.accept(self)