# Buggy code
def build_namedtuple_typeinfo(
    self,
    name: str,
    items: List[str],
    types: List[Type],
    default_items: Dict[str, Expression],
) -> TypeInfo:
    strtype = self.str_type()
    implicit_any = AnyType(TypeOfAny.special_form)
    basetuple_type = self.named_type("__builtins__.tuple", [implicit_any])
    dictype = (
        self.named_type_or_none("builtins.dict", [strtype, implicit_any])
        or self.object_type()
    )
    # Actual signature should return OrderedDict[str, Union[types]]
    ordereddictype = (
        self.named_type_or_none("builtins.dict", [strtype, implicit_any])
        or self.object_type()
    )
    # 'builtins.tuple' has only one type parameter.
    #
    # TODO: The corresponding type argument in the fallback instance should be a join of
    #       all item types, but we can't do joins during this pass of semantic analysis
    #       and we are using Any as a workaround.
    fallback = self.named_type("__builtins__.tuple", [implicit_any])
    # Note: actual signature should accept an invariant version of Iterable[UnionType[types]].
    # but it can't be expressed. 'new' and 'len' should be callable types.
    iterable_type = self.named_type_or_none("typing.Iterable", [implicit_any])
    function_type = self.named_type("__builtins__.function")

    info = self.basic_new_typeinfo(name, fallback)
    info.is_named_tuple = True
    info.tuple_type = TupleType(types, fallback)

    def add_field(
        var: Var, is_initialized_in_class: bool = False, is_property: bool = False
    ) -> None:
        var.info = info
        var.is_initialized_in_class = is_initialized_in_class
        var.is_property = is_property
        info.names[var.name()] = SymbolTableNode(MDEF, var)

    vars = [Var(item, typ) for item, typ in zip(items, types)]
    for var in vars:
        add_field(var, is_property=True)

    tuple_of_strings = TupleType([strtype for _ in items], basetuple_type)
    add_field(Var("_fields", tuple_of_strings), is_initialized_in_class=True)
    add_field(Var("_field_types", dictype), is_initialized_in_class=True)
    add_field(Var("_field_defaults", dictype), is_initialized_in_class=True)
    add_field(Var("_source", strtype), is_initialized_in_class=True)
    add_field(Var("__annotations__", ordereddictype), is_initialized_in_class=True)
    add_field(Var("__doc__", strtype), is_initialized_in_class=True)

    tvd = TypeVarDef("NT", 1, [], info.tuple_type)
    selftype = TypeVarType(tvd)

    def add_method(
        funcname: str,
        ret: Type,
        args: List[Argument],
        name: str = None,
        is_classmethod: bool = False,
    ) -> None:
        if is_classmethod:
            first = [
                Argument(Var("cls"), TypeType.make_normalized(selftype), None, ARG_POS)
            ]
        else:
            first = [Argument(Var("self"), selftype, None, ARG_POS)]
        args = first + args

        types = [arg.type_annotation for arg in args]
        items = [arg.variable.name() for arg in args]
        arg_kinds = [arg.kind for arg in args]
        signature = CallableType(
            types,
            arg_kinds,
            items,
            ret,
            function_type,
            name=name or info.name() + "." + funcname,
        )
        signature.variables = [tvd]
        func = FuncDef(funcname, args, Block([]), typ=signature)
        func.info = info
        func.is_class = is_classmethod
        if is_classmethod:
            v = Var(funcname, signature)
            v.is_classmethod = True
            v.info = info
            dec = Decorator(func, [NameExpr("classmethod")], v)
            info.names[funcname] = SymbolTableNode(MDEF, dec)
        else:
            info.names[funcname] = SymbolTableNode(MDEF, func)

    add_method(
        "_replace",
        ret=selftype,
        args=[Argument(var, var.type, EllipsisExpr(), ARG_NAMED_OPT) for var in vars],
    )

    def make_init_arg(var: Var) -> Argument:
        default = default_items.get(var.name(), None)
        kind = ARG_POS if default is None else ARG_OPT
        return Argument(var, var.type, default, kind)

    add_method(
        "__init__",
        ret=NoneTyp(),
        name=info.name(),
        args=[make_init_arg(var) for var in vars],
    )
    add_method("_asdict", args=[], ret=ordereddictype)
    special_form_any = AnyType(TypeOfAny.special_form)
    add_method(
        "_make",
        ret=selftype,
        is_classmethod=True,
        args=[
            Argument(Var("iterable", iterable_type), iterable_type, None, ARG_POS),
            Argument(Var("new"), special_form_any, EllipsisExpr(), ARG_NAMED_OPT),
            Argument(Var("len"), special_form_any, EllipsisExpr(), ARG_NAMED_OPT),
        ],
    )
    return info


# 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 build_namedtuple_typeinfo(
    self,
    name: str,
    items: List[str],
    types: List[Type],
    default_items: Dict[str, Expression],
) -> TypeInfo:
    strtype = self.str_type()
    implicit_any = AnyType(TypeOfAny.special_form)
    basetuple_type = self.named_type("__builtins__.tuple", [implicit_any])
    dictype = (
        self.named_type_or_none("builtins.dict", [strtype, implicit_any])
        or self.object_type()
    )
    # Actual signature should return OrderedDict[str, Union[types]]
    ordereddictype = (
        self.named_type_or_none("builtins.dict", [strtype, implicit_any])
        or self.object_type()
    )
    fallback = self.named_type("__builtins__.tuple", [implicit_any])
    # Note: actual signature should accept an invariant version of Iterable[UnionType[types]].
    # but it can't be expressed. 'new' and 'len' should be callable types.
    iterable_type = self.named_type_or_none("typing.Iterable", [implicit_any])
    function_type = self.named_type("__builtins__.function")

    info = self.basic_new_typeinfo(name, fallback)
    info.is_named_tuple = True
    info.tuple_type = TupleType(types, fallback)

    def patch() -> None:
        # Calculate the correct value type for the fallback Mapping.
        fallback.args[0] = join.join_type_list(list(info.tuple_type.items))

    # We can't calculate the complete fallback type until after semantic
    # analysis, since otherwise MROs might be incomplete. Postpone a callback
    # function that patches the fallback.
    self.patches.append(patch)

    def add_field(
        var: Var, is_initialized_in_class: bool = False, is_property: bool = False
    ) -> None:
        var.info = info
        var.is_initialized_in_class = is_initialized_in_class
        var.is_property = is_property
        info.names[var.name()] = SymbolTableNode(MDEF, var)

    vars = [Var(item, typ) for item, typ in zip(items, types)]
    for var in vars:
        add_field(var, is_property=True)

    tuple_of_strings = TupleType([strtype for _ in items], basetuple_type)
    add_field(Var("_fields", tuple_of_strings), is_initialized_in_class=True)
    add_field(Var("_field_types", dictype), is_initialized_in_class=True)
    add_field(Var("_field_defaults", dictype), is_initialized_in_class=True)
    add_field(Var("_source", strtype), is_initialized_in_class=True)
    add_field(Var("__annotations__", ordereddictype), is_initialized_in_class=True)
    add_field(Var("__doc__", strtype), is_initialized_in_class=True)

    tvd = TypeVarDef("NT", 1, [], info.tuple_type)
    selftype = TypeVarType(tvd)

    def add_method(
        funcname: str,
        ret: Type,
        args: List[Argument],
        name: str = None,
        is_classmethod: bool = False,
    ) -> None:
        if is_classmethod:
            first = [
                Argument(Var("cls"), TypeType.make_normalized(selftype), None, ARG_POS)
            ]
        else:
            first = [Argument(Var("self"), selftype, None, ARG_POS)]
        args = first + args

        types = [arg.type_annotation for arg in args]
        items = [arg.variable.name() for arg in args]
        arg_kinds = [arg.kind for arg in args]
        signature = CallableType(
            types,
            arg_kinds,
            items,
            ret,
            function_type,
            name=name or info.name() + "." + funcname,
        )
        signature.variables = [tvd]
        func = FuncDef(funcname, args, Block([]), typ=signature)
        func.info = info
        func.is_class = is_classmethod
        if is_classmethod:
            v = Var(funcname, signature)
            v.is_classmethod = True
            v.info = info
            dec = Decorator(func, [NameExpr("classmethod")], v)
            info.names[funcname] = SymbolTableNode(MDEF, dec)
        else:
            info.names[funcname] = SymbolTableNode(MDEF, func)

    add_method(
        "_replace",
        ret=selftype,
        args=[Argument(var, var.type, EllipsisExpr(), ARG_NAMED_OPT) for var in vars],
    )

    def make_init_arg(var: Var) -> Argument:
        default = default_items.get(var.name(), None)
        kind = ARG_POS if default is None else ARG_OPT
        return Argument(var, var.type, default, kind)

    add_method(
        "__init__",
        ret=NoneTyp(),
        name=info.name(),
        args=[make_init_arg(var) for var in vars],
    )
    add_method("_asdict", args=[], ret=ordereddictype)
    special_form_any = AnyType(TypeOfAny.special_form)
    add_method(
        "_make",
        ret=selftype,
        is_classmethod=True,
        args=[
            Argument(Var("iterable", iterable_type), iterable_type, None, ARG_POS),
            Argument(Var("new"), special_form_any, EllipsisExpr(), ARG_NAMED_OPT),
            Argument(Var("len"), special_form_any, EllipsisExpr(), ARG_NAMED_OPT),
        ],
    )
    return info