Skip to content
Myr

Close contexts created during shell completion #2644. (#2800)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Andreas Backx · committed by GitHub · parent d8763b9 · Browse files · 4 files+65 added−32 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: akhil-vempali <avempali@atlassian.com>

Diff stats

4 files+65 added−32 removed

  • tests:+26 added−0 removed
  • other:+39 added−32 removed

4 files+65 added−32 removed

modified:src/click/core.py+2 added-2 removed
702 unmodified lines
703 def exit(self, code: int = 0) -> t.NoReturn:704 """Exits the application with a given exit code.705706 .. versionchanged:: 8.2707 Force closing of callbacks registered with708 :meth:`call_on_close` before exiting the CLI.707 Callbacks and context managers registered with :meth:`call_on_close`708 and :meth:`with_resource` are closed before exiting.709 """710 self.close()711 raise Exit(code)712
2326 unmodified lines
modified:src/click/shell_completion.py+34 added-30 removed
542 unmodified lines
543 :param prog_name: Name of the executable in the shell.544 :param args: List of complete args before the incomplete value.545 """546 ctx_args["resilient_parsing"] = True547 ctx = cli.make_context(prog_name, args.copy(), **ctx_args)548 args = ctx._protected_args + ctx.args547 with cli.make_context(prog_name, args.copy(), **ctx_args) as ctx:548 args = ctx._protected_args + ctx.args549550 while args:551 command = ctx.command550 while args:551 command = ctx.command552553 if isinstance(command, Group):554 if not command.chain:555 name, cmd, args = command.resolve_command(ctx, args)556557 if cmd is None:558 return ctx559560 ctx = cmd.make_context(name, args, parent=ctx, resilient_parsing=True)561 args = ctx._protected_args + ctx.args562 else:563 sub_ctx = ctx564565 while args:553 if isinstance(command, Group):554 if not command.chain:555 name, cmd, args = command.resolve_command(ctx, args)556557 if cmd is None:558 return ctx559571 sub_ctx = cmd.make_context(572 name,573 args,574 parent=ctx,575 allow_extra_args=True,576 allow_interspersed_args=False,577 resilient_parsing=True,578 )579 args = sub_ctx.args560 with cmd.make_context(561 name, args, parent=ctx, resilient_parsing=True562 ) as sub_ctx:563 args = ctx._protected_args + ctx.args564 ctx = sub_ctx565 else:566 sub_ctx = ctx567581 ctx = sub_ctx582 args = [*sub_ctx._protected_args, *sub_ctx.args]583 else:584 break568 while args:569 name, cmd, args = command.resolve_command(ctx, args)570571 if cmd is None:572 return ctx573574 with cmd.make_context(575 name,576 args,577 parent=ctx,578 allow_extra_args=True,579 allow_interspersed_args=False,580 resilient_parsing=True,581 ) as sub_sub_ctx:582 args = sub_ctx.args583 sub_ctx = sub_sub_ctx584585 ctx = sub_ctx586 args = [*sub_ctx._protected_args, *sub_ctx.args]587 else:588 break589590 return ctx591592593def _resolve_incomplete(
44 unmodified lines
modified:tests/test_shell_completion.py+26 added
1import warnings23import pytest45import click.shell_completion6from click.core import Argument
408 unmodified lines
415416 # Using `add_completion_class` as a decorator adds the new shell immediately417 assert "mysh" in click.shell_completion._available_shells418 assert click.shell_completion._available_shells["mysh"] is MyshComplete419420421# Don't make the ResourceWarning give an error422@pytest.mark.filterwarnings("default")423def test_files_closed(runner) -> None:424 with runner.isolated_filesystem():425 config_file = "foo.txt"426 with open(config_file, "w") as f:427 f.write("bar")428429 @click.group()430 @click.option(431 "--config-file",432 default=config_file,433 type=click.File(mode="r"),434 )435 @click.pass_context436 def cli(ctx, config_file):437 pass438439 with warnings.catch_warnings(record=True) as current_warnings:440 assert not current_warnings, "There should be no warnings to start"441 _get_completions(cli, args=[], incomplete="")442 assert not current_warnings, "There should be no warnings after either"
modified:CHANGES.rst+3 added
57 unmodified lines
58 invalid choice message. :issue:`2621` :pr:`2622`59- If help is shown because ``no_args_is_help`` is enabled (defaults to ``True``60 for groups, ``False`` for commands), the exit code is 2 instead of 0.61 :issue:`1489` :pr:`1489`62- Contexts created during shell completion are closed properly, fixing63 ``ResourceWarning``s when using ``click.File``. :issue:`2644` :pr:`2800`64 :pr:`2767`656667Version 8.1.868-------------
1116 unmodified lines