From e65efc32a0ba17f800f382d471f8783ecda13f38 Mon Sep 17 00:00:00 2001 From: akhil-vempali Date: Wed, 21 Aug 2024 13:30:17 -0400 Subject: [PATCH 1/2] [FIX] fixes #2644 https://github.com/pallets/click/issues/2644 --- src/click/core.py | 2 +- src/click/shell_completion.py | 61 +++++++++++++++++------------------ 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/click/core.py b/src/click/core.py index 30a4fd8f4..92d0f590d 100644 --- a/src/click/core.py +++ b/src/click/core.py @@ -1127,7 +1127,7 @@ def make_context( ctx = self.context_class(self, info_name=info_name, parent=parent, **extra) - with ctx.scope(cleanup=False): + with ctx: self.parse_args(ctx, args) return ctx diff --git a/src/click/shell_completion.py b/src/click/shell_completion.py index 6fd9e5422..af7a8a75c 100644 --- a/src/click/shell_completion.py +++ b/src/click/shell_completion.py @@ -544,44 +544,43 @@ def _resolve_context( :param args: List of complete args before the incomplete value. """ ctx_args["resilient_parsing"] = True - ctx = cli.make_context(prog_name, args.copy(), **ctx_args) - args = ctx._protected_args + ctx.args + with cli.make_context(prog_name, args.copy(), **ctx_args) as ctx: + args = ctx._protected_args + ctx.args - while args: - command = ctx.command + while args: + command = ctx.command - if isinstance(command, Group): - if not command.chain: - name, cmd, args = command.resolve_command(ctx, args) - - if cmd is None: - return ctx - - ctx = cmd.make_context(name, args, parent=ctx, resilient_parsing=True) - args = ctx._protected_args + ctx.args - else: - sub_ctx = ctx - - while args: + if isinstance(command, Group): + if not command.chain: name, cmd, args = command.resolve_command(ctx, args) if cmd is None: return ctx - sub_ctx = cmd.make_context( - name, - args, - parent=ctx, - allow_extra_args=True, - allow_interspersed_args=False, - resilient_parsing=True, - ) - args = sub_ctx.args - - ctx = sub_ctx - args = [*sub_ctx._protected_args, *sub_ctx.args] - else: - break + with cmd.make_context(name, args, parent=ctx, resilient_parsing=True) as sub_ctx: + args = sub_ctx._protected_args + sub_ctx.args + ctx = sub_ctx + else: + sub_ctx = ctx + + while args: + name, cmd, args = command.resolve_command(ctx, args) + + if cmd is None: + return ctx + + with cmd.make_context( + name, + args, + parent=ctx, + allow_extra_args=True, + allow_interspersed_args=False, + resilient_parsing=True, + ) as sub_ctx: + args = sub_ctx.args + ctx = sub_ctx + else: + break return ctx From 65232e56f8b810e7709b550ae8bf1c4519c4f95c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 21 Aug 2024 17:33:51 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/click/shell_completion.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/click/shell_completion.py b/src/click/shell_completion.py index af7a8a75c..560441ee1 100644 --- a/src/click/shell_completion.py +++ b/src/click/shell_completion.py @@ -557,7 +557,9 @@ def _resolve_context( if cmd is None: return ctx - with cmd.make_context(name, args, parent=ctx, resilient_parsing=True) as sub_ctx: + with cmd.make_context( + name, args, parent=ctx, resilient_parsing=True + ) as sub_ctx: args = sub_ctx._protected_args + sub_ctx.args ctx = sub_ctx else: