Skip to content

[rejected AI] Fix count=True ignoring non-zero default - #3842

Closed
ShamikOfficial wants to merge 1 commit into
pallets:mainfrom
ShamikOfficial:fix/3841-count-default-baseline
Closed

[rejected AI] Fix count=True ignoring non-zero default#3842
ShamikOfficial wants to merge 1 commit into
pallets:mainfrom
ShamikOfficial:fix/3841-count-default-baseline

Conversation

@ShamikOfficial

@ShamikOfficial ShamikOfficial commented Sep 5, 2026

Copy link
Copy Markdown

When count=True options have a non-zero default, Click previously counted from 0 whenever the flag appeared on the command line. That could produce a value lower than the default (for example default=3 with one -v yielded 1).

This change makes command-line counts additive relative to the option default (and Context.default_map when present), matching argparse action="count".

fixes #3841

  • Added parametrized tests for non-zero defaults and a default_map baseline test.
  • Documented behavior in the Option docstring (.. versionchanged:: 8.5.1).
  • Added a CHANGES.md entry under 8.5.1.

Count increments relative to the option default (and default_map)
when flags are passed on the command line, matching argparse.
Fixes pallets#3841.

Co-authored-by: Cursor <cursoragent@cursor.com>
@roli-lpci

Copy link
Copy Markdown

count=True also accepts string defaults, which Click normally converts through
the option's integer type. On this head, the new addition happens before that
conversion, so an explicit count crashes for either default source:

@click.command()
@click.option("-v", count=True, default="3")
def cli(v):
    click.echo(v)

runner.invoke(cli, []).output       # "3\n" (an int in the callback)
runner.invoke(cli, ["-v"]).exception
# TypeError: can only concatenate str (not "int") to str

The same exception occurs with default_map={"v": "3"}. Both baselines are
still raw strings in consume_value, while the parser count is an int; normal
type conversion only runs later in handle_parse_result. Could the baseline be
converted through the option type before adding it, with coverage for string
values from both default and default_map?

@davidism

davidism commented Sep 6, 2026

Copy link
Copy Markdown
Member

@davidism davidism closed this Sep 6, 2026
@davidism davidism added the rejected AI Contribution rejected because of its untrustworthy AI origin label Sep 6, 2026
@davidism davidism changed the title Fix count=True ignoring non-zero default [rejected AI] Fix count=True ignoring non-zero default Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rejected AI Contribution rejected because of its untrustworthy AI origin

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[rejected AI] count=True ignores non-zero default when counting flags

3 participants