# Buggy code
def shell_complete(cli, prog_name, complete_var, instruction):
    """Perform shell completion for the given CLI program.

    :param cli: Command being called.
    :param prog_name: Name of the executable in the shell.
    :param complete_var: Name of the environment variable that holds
        the completion instruction.
    :param instruction: Value of ``complete_var`` with the completion
        instruction and shell, in the form ``instruction_shell``.
    :return: Status code to exit with.
    """
    instruction, _, shell = instruction.partition("_")
    comp_cls = get_completion_class(shell)

    if comp_cls is None:
        return 1

    comp = comp_cls(cli, prog_name, complete_var)

    if instruction == "source":
        echo(comp.source())
        return 0

    if instruction == "complete":
        echo(comp.complete())
        return 0

    return 1


# Traceback (TypeError)
$ ./testclick subcommand <TAB>Traceback (most recent call last):
File "./testclick", line 23, in <module>
entrypoint(obj={'completions': ['abc', 'def', 'ghi', ]})
File "/home/user/testclick/venv/lib/python3.6/site-packages/click/core.py", line 731, in __call__
return self.main(*args, **kwargs)
File "/home/user/testclick/venv/lib/python3.6/site-packages/click/core.py", line 701, in main
_bashcomplete(self, prog_name, complete_var)
File "/home/user/testclick/venv/lib/python3.6/site-packages/click/core.py", line 46, in _bashcomplete
if bashcomplete(cmd, prog_name, complete_var, complete_instr):
File "/home/user/testclick/venv/lib/python3.6/site-packages/click/_bashcomplete.py", line 216, in bashcomplete
return do_complete(cli, prog_name)
File "/home/user/testclick/venv/lib/python3.6/site-packages/click/_bashcomplete.py", line 205, in do_complete
for item in get_choices(cli, prog_name, args, incomplete):
File "/home/user/testclick/venv/lib/python3.6/site-packages/click/_bashcomplete.py", line 186, in get_choices
ctx, all_args, incomplete, param))
File "/home/user/testclick/venv/lib/python3.6/site-packages/click/_bashcomplete.py", line 124, in get_user_autocompletions
incomplete=incomplete)
File "./testclick", line 7, in _complete
return ctx.obj['completions']
TypeError: 'NoneType' object is not subscriptable

# Fixed code
def shell_complete(cli, ctx_args, prog_name, complete_var, instruction):
    """Perform shell completion for the given CLI program.

    :param cli: Command being called.
    :param ctx_args: Extra arguments to pass to
        ``cli.make_context``.
    :param prog_name: Name of the executable in the shell.
    :param complete_var: Name of the environment variable that holds
        the completion instruction.
    :param instruction: Value of ``complete_var`` with the completion
        instruction and shell, in the form ``instruction_shell``.
    :return: Status code to exit with.
    """
    instruction, _, shell = instruction.partition("_")
    comp_cls = get_completion_class(shell)

    if comp_cls is None:
        return 1

    comp = comp_cls(cli, ctx_args, prog_name, complete_var)

    if instruction == "source":
        echo(comp.source())
        return 0

    if instruction == "complete":
        echo(comp.complete())
        return 0

    return 1