# 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:
            # Insert implicit 'Any' type arguments.
            t.args = [AnyType()] * 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)
    elif info.defn.type_vars:
        # Check type argument values.
        for arg, TypeVar in zip(t.args, info.defn.type_vars):
            if TypeVar.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, TypeVar.values, t)
    for arg in t.args:
        arg.accept(self)


# Traceback (IndexError)
Traceback (most recent call last):
...
cb = infer_constraints(mapped.args[i], instance.args[i],
IndexError: list index out of range

# Fixed 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:
            # Insert implicit 'Any' type arguments.
            t.args = [AnyType()] * 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() for _ in info.type_vars]
    elif info.defn.type_vars:
        # Check type argument values.
        for arg, TypeVar in zip(t.args, info.defn.type_vars):
            if TypeVar.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, TypeVar.values, t)
    for arg in t.args:
        arg.accept(self)