# Buggy code
def visit_unbound_type(self, t: UnboundType) -> Type:
    if t.optional:
        t.optional = False
        # We don't need to worry about double-wrapping Optionals or
        # wrapping Anys: Union simplification will take care of that.
        return make_optional_type(self.visit_unbound_type(t))
    sym = self.lookup(t.name, t)
    if sym is not None:
        if sym.node is None:
            # UNBOUND_IMPORTED can happen if an unknown name was imported.
            if sym.kind != UNBOUND_IMPORTED:
                self.fail("Internal error (node is None, kind={})".format(sym.kind), t)
            return AnyType(TypeOfAny.special_form)
        fullname = sym.node.fullname()
        hook = self.plugin.get_type_analyze_hook(fullname)
        if hook:
            return hook(AnalyzeTypeContext(t, t, self))
        if (
            fullname in nongen_builtins
            and t.args
            and not sym.normalized
            and not self.allow_unnormalized
        ):
            self.fail(no_subscript_builtin_alias(fullname), t)
        tvar_def = self.tvar_scope.get_binding(sym)
        if sym.kind == TVAR and tvar_def is not None:
            if len(t.args) > 0:
                self.fail('Type variable "{}" used with arguments'.format(t.name), t)
            return TypeVarType(tvar_def, t.line)
        elif fullname == "builtins.None":
            return NoneTyp()
        elif fullname == "typing.Any" or fullname == "builtins.Any":
            return AnyType(TypeOfAny.explicit)
        elif fullname == "typing.Tuple":
            if len(t.args) == 0 and not t.empty_tuple_index:
                # Bare 'Tuple' is same as 'tuple'
                if (
                    "generics" in self.options.disallow_any
                    and not self.is_typeshed_stub
                ):
                    self.fail(messages.BARE_GENERIC, t)
                typ = self.named_type("builtins.tuple", line=t.line, column=t.column)
                typ.from_generic_builtin = True
                return typ
            if len(t.args) == 2 and isinstance(t.args[1], EllipsisType):
                # Tuple[T, ...] (uniform, variable-length tuple)
                instance = self.named_type(
                    "builtins.tuple", [self.anal_type(t.args[0])]
                )
                instance.line = t.line
                return instance
            return self.tuple_type(self.anal_array(t.args))
        elif fullname == "typing.Union":
            items = self.anal_array(t.args)
            return UnionType.make_union(items)
        elif fullname == "typing.Optional":
            if len(t.args) != 1:
                self.fail("Optional[...] must have exactly one type argument", t)
                return AnyType(TypeOfAny.from_error)
            item = self.anal_type(t.args[0])
            return make_optional_type(item)
        elif fullname == "typing.Callable":
            return self.analyze_callable_type(t)
        elif fullname == "typing.Type":
            if len(t.args) == 0:
                any_type = AnyType(
                    TypeOfAny.from_omitted_generics, line=t.line, column=t.column
                )
                return TypeType(any_type, line=t.line, column=t.column)
            if len(t.args) != 1:
                self.fail("Type[...] must have exactly one type argument", t)
            item = self.anal_type(t.args[0])
            return TypeType.make_normalized(item, line=t.line)
        elif fullname == "typing.ClassVar":
            if self.nesting_level > 0:
                self.fail("Invalid type: ClassVar nested inside other type", t)
            if len(t.args) == 0:
                return AnyType(
                    TypeOfAny.from_omitted_generics, line=t.line, column=t.column
                )
            if len(t.args) != 1:
                self.fail("ClassVar[...] must have at most one type argument", t)
                return AnyType(TypeOfAny.from_error)
            item = self.anal_type(t.args[0])
            if isinstance(item, TypeVarType) or get_type_vars(item):
                self.fail("Invalid type: ClassVar cannot be generic", t)
                return AnyType(TypeOfAny.from_error)
            return item
        elif fullname in ("mypy_extensions.NoReturn", "typing.NoReturn"):
            return UninhabitedType(is_noreturn=True)
        elif sym.kind == TYPE_ALIAS:
            override = sym.type_override
            all_vars = sym.alias_tvars
            assert override is not None
            an_args = self.anal_array(t.args)
            if all_vars is not None:
                exp_len = len(all_vars)
            else:
                exp_len = 0
            act_len = len(an_args)
            if exp_len > 0 and act_len == 0:
                # Interpret bare Alias same as normal generic, i.e., Alias[Any, Any, ...]
                assert all_vars is not None
                return set_any_tvars(override, all_vars, t.line, t.column)
            if exp_len == 0 and act_len == 0:
                return override
            if act_len != exp_len:
                self.fail(
                    "Bad number of arguments for type alias, expected: %s, given: %s"
                    % (exp_len, act_len),
                    t,
                )
                return set_any_tvars(
                    override, all_vars or [], t.line, t.column, implicit=False
                )
            assert all_vars is not None
            return replace_alias_tvars(override, all_vars, an_args, t.line, t.column)
        elif not isinstance(sym.node, TypeInfo):
            name = sym.fullname
            if name is None:
                name = sym.node.name()
            if isinstance(sym.node, Var) and isinstance(sym.node.type, AnyType):
                # Something with an Any type -- make it an alias for Any in a type
                # context. This is slightly problematic as it allows using the type 'Any'
                # as a base class -- however, this will fail soon at runtime so the problem
                # is pretty minor.
                return AnyType(TypeOfAny.from_unimported_type)
            # Allow unbound type variables when defining an alias
            if not (
                self.aliasing
                and sym.kind == TVAR
                and self.tvar_scope.get_binding(sym) is None
            ):
                self.fail('Invalid type "{}"'.format(name), t)
            return t
        info = sym.node  # type: TypeInfo
        if len(t.args) > 0 and info.fullname() == "builtins.tuple":
            fallback = Instance(info, [AnyType(TypeOfAny.special_form)], t.line)
            return TupleType(self.anal_array(t.args), fallback, t.line)
        else:
            # Analyze arguments and construct Instance type. The
            # number of type arguments and their values are
            # checked only later, since we do not always know the
            # valid count at this point. Thus we may construct an
            # Instance with an invalid number of type arguments.
            instance = Instance(info, self.anal_array(t.args), t.line, t.column)
            instance.from_generic_builtin = sym.normalized
            tup = info.tuple_type
            if tup is not None:
                # The class has a Tuple[...] base class so it will be
                # represented as a tuple type.
                if t.args:
                    self.fail("Generic tuple types not supported", t)
                    return AnyType(TypeOfAny.from_error)
                return tup.copy_modified(
                    items=self.anal_array(tup.items), fallback=instance
                )
            td = info.typeddict_type
            if td is not None:
                # The class has a TypedDict[...] base class so it will be
                # represented as a typeddict type.
                if t.args:
                    self.fail("Generic TypedDict types not supported", t)
                    return AnyType(TypeOfAny.from_error)
                # Create a named TypedDictType
                return td.copy_modified(
                    item_types=self.anal_array(list(td.items.values())),
                    fallback=instance,
                )
            return instance
    else:
        return AnyType(TypeOfAny.special_form)


# 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_unbound_type(self, t: UnboundType) -> Type:
    if t.optional:
        t.optional = False
        # We don't need to worry about double-wrapping Optionals or
        # wrapping Anys: Union simplification will take care of that.
        return make_optional_type(self.visit_unbound_type(t))
    sym = self.lookup(t.name, t, suppress_errors=self.third_pass)  # type: ignore
    if sym is not None:
        if sym.node is None:
            # UNBOUND_IMPORTED can happen if an unknown name was imported.
            if sym.kind != UNBOUND_IMPORTED:
                self.fail("Internal error (node is None, kind={})".format(sym.kind), t)
            return AnyType(TypeOfAny.special_form)
        fullname = sym.node.fullname()
        hook = self.plugin.get_type_analyze_hook(fullname)
        if hook:
            return hook(AnalyzeTypeContext(t, t, self))
        if (
            fullname in nongen_builtins
            and t.args
            and not sym.normalized
            and not self.allow_unnormalized
        ):
            self.fail(no_subscript_builtin_alias(fullname), t)
        if self.tvar_scope:
            tvar_def = self.tvar_scope.get_binding(sym)
        else:
            tvar_def = None
        if sym.kind == TVAR and tvar_def is not None:
            if len(t.args) > 0:
                self.fail('Type variable "{}" used with arguments'.format(t.name), t)
            return TypeVarType(tvar_def, t.line)
        elif fullname == "builtins.None":
            return NoneTyp()
        elif fullname == "typing.Any" or fullname == "builtins.Any":
            return AnyType(TypeOfAny.explicit)
        elif fullname == "typing.Tuple":
            if len(t.args) == 0 and not t.empty_tuple_index:
                # Bare 'Tuple' is same as 'tuple'
                if (
                    "generics" in self.options.disallow_any
                    and not self.is_typeshed_stub
                ):
                    self.fail(messages.BARE_GENERIC, t)
                typ = self.named_type("builtins.tuple", line=t.line, column=t.column)
                typ.from_generic_builtin = True
                return typ
            if len(t.args) == 2 and isinstance(t.args[1], EllipsisType):
                # Tuple[T, ...] (uniform, variable-length tuple)
                instance = self.named_type(
                    "builtins.tuple", [self.anal_type(t.args[0])]
                )
                instance.line = t.line
                return instance
            return self.tuple_type(self.anal_array(t.args))
        elif fullname == "typing.Union":
            items = self.anal_array(t.args)
            return UnionType.make_union(items)
        elif fullname == "typing.Optional":
            if len(t.args) != 1:
                self.fail("Optional[...] must have exactly one type argument", t)
                return AnyType(TypeOfAny.from_error)
            item = self.anal_type(t.args[0])
            return make_optional_type(item)
        elif fullname == "typing.Callable":
            return self.analyze_callable_type(t)
        elif fullname == "typing.Type":
            if len(t.args) == 0:
                any_type = AnyType(
                    TypeOfAny.from_omitted_generics, line=t.line, column=t.column
                )
                return TypeType(any_type, line=t.line, column=t.column)
            if len(t.args) != 1:
                self.fail("Type[...] must have exactly one type argument", t)
            item = self.anal_type(t.args[0])
            return TypeType.make_normalized(item, line=t.line)
        elif fullname == "typing.ClassVar":
            if self.nesting_level > 0:
                self.fail("Invalid type: ClassVar nested inside other type", t)
            if len(t.args) == 0:
                return AnyType(
                    TypeOfAny.from_omitted_generics, line=t.line, column=t.column
                )
            if len(t.args) != 1:
                self.fail("ClassVar[...] must have at most one type argument", t)
                return AnyType(TypeOfAny.from_error)
            item = self.anal_type(t.args[0])
            if isinstance(item, TypeVarType) or get_type_vars(item):
                self.fail("Invalid type: ClassVar cannot be generic", t)
                return AnyType(TypeOfAny.from_error)
            return item
        elif fullname in ("mypy_extensions.NoReturn", "typing.NoReturn"):
            return UninhabitedType(is_noreturn=True)
        elif sym.kind == TYPE_ALIAS:
            override = sym.type_override
            all_vars = sym.alias_tvars
            assert override is not None
            an_args = self.anal_array(t.args)
            if all_vars is not None:
                exp_len = len(all_vars)
            else:
                exp_len = 0
            act_len = len(an_args)
            if exp_len > 0 and act_len == 0:
                # Interpret bare Alias same as normal generic, i.e., Alias[Any, Any, ...]
                assert all_vars is not None
                return set_any_tvars(override, all_vars, t.line, t.column)
            if exp_len == 0 and act_len == 0:
                return override
            if act_len != exp_len:
                self.fail(
                    "Bad number of arguments for type alias, expected: %s, given: %s"
                    % (exp_len, act_len),
                    t,
                )
                return set_any_tvars(
                    override, all_vars or [], t.line, t.column, implicit=False
                )
            assert all_vars is not None
            return replace_alias_tvars(override, all_vars, an_args, t.line, t.column)
        elif not isinstance(sym.node, TypeInfo):
            name = sym.fullname
            if name is None:
                name = sym.node.name()
            if isinstance(sym.node, Var) and isinstance(sym.node.type, AnyType):
                # Something with an Any type -- make it an alias for Any in a type
                # context. This is slightly problematic as it allows using the type 'Any'
                # as a base class -- however, this will fail soon at runtime so the problem
                # is pretty minor.
                return AnyType(TypeOfAny.from_unimported_type)
            # Allow unbound type variables when defining an alias
            if not (
                self.aliasing
                and sym.kind == TVAR
                and (not self.tvar_scope or self.tvar_scope.get_binding(sym) is None)
            ):
                if (
                    not self.third_pass
                    and not self.in_dynamic_func
                    and not (
                        isinstance(sym.node, (FuncDef, Decorator))
                        or isinstance(sym.node, Var)
                        and sym.node.is_ready
                    )
                    and not (sym.kind == TVAR and tvar_def is None)
                ):
                    if t.args and not self.global_scope:
                        self.fail(
                            'Unsupported forward reference to "{}"'.format(t.name), t
                        )
                        return AnyType(TypeOfAny.from_error)
                    return ForwardRef(t)
                self.fail('Invalid type "{}"'.format(name), t)
                if self.third_pass and sym.kind == TVAR:
                    self.note_func(
                        "Forward references to type variables are prohibited", t
                    )
            return t
        info = sym.node  # type: TypeInfo
        if len(t.args) > 0 and info.fullname() == "builtins.tuple":
            fallback = Instance(info, [AnyType(TypeOfAny.special_form)], t.line)
            return TupleType(self.anal_array(t.args), fallback, t.line)
        else:
            # Analyze arguments and construct Instance type. The
            # number of type arguments and their values are
            # checked only later, since we do not always know the
            # valid count at this point. Thus we may construct an
            # Instance with an invalid number of type arguments.
            instance = Instance(info, self.anal_array(t.args), t.line, t.column)
            instance.from_generic_builtin = sym.normalized
            tup = info.tuple_type
            if tup is not None:
                # The class has a Tuple[...] base class so it will be
                # represented as a tuple type.
                if t.args:
                    self.fail("Generic tuple types not supported", t)
                    return AnyType(TypeOfAny.from_error)
                return tup.copy_modified(
                    items=self.anal_array(tup.items), fallback=instance
                )
            td = info.typeddict_type
            if td is not None:
                # The class has a TypedDict[...] base class so it will be
                # represented as a typeddict type.
                if t.args:
                    self.fail("Generic TypedDict types not supported", t)
                    return AnyType(TypeOfAny.from_error)
                # Create a named TypedDictType
                return td.copy_modified(
                    item_types=self.anal_array(list(td.items.values())),
                    fallback=instance,
                )
            return instance
    else:
        if self.third_pass:
            self.fail('Invalid type "{}"'.format(t.name), t)
            return AnyType(TypeOfAny.from_error)
        return AnyType(TypeOfAny.special_form)