Skip to content
Closed

AI spam #3562

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2674,10 +2674,21 @@ def handle_parse_result(
and existing_default_explicit
and not self._default_explicit
)
unprocessed_would_downgrade_callback = (
same_source
and source == ParameterSource.COMMANDLINE
and self.callback is None
and existing_value is not UNSET
and existing_value != value
)
is_winner = (
slot_empty
or more_explicit
or (same_source and not auto_would_downgrade_explicit)
or (
same_source
and not auto_would_downgrade_explicit
and not unprocessed_would_downgrade_callback
)
)

if is_winner:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -1626,6 +1626,32 @@ def main(fmt):
assert result.exit_code == 0


def test_shared_option_callback_value_is_preserved(runner):
marker = "$_fetch"

def fetch_value(ctx, param, value):
if value == marker:
return "fetched"

return value

@click.command()
@click.option("--custom", "custom")
@click.option("--fetch", "custom", flag_value=marker, callback=fetch_value)
def cli(custom):
click.echo(custom)

for args, expected in (
(["--fetch"], "fetched"),
(["--custom", "typed", "--fetch"], "fetched"),
(["--fetch", "--custom", "typed"], "typed"),
):
result = runner.invoke(cli, args)

assert result.output == f"{expected}\n"
assert result.exit_code == 0


@pytest.mark.parametrize(
("flag_value", "envvar_value", "expected"),
[
Expand Down
Loading