Skip to content

Do not offer value completions for boolean flags (fixes #116) - #136

Merged
auvipy merged 2 commits into
click-contrib:masterfrom
apoorvdarshan:fix/flag-value-completion-116
Aug 4, 2026
Merged

Do not offer value completions for boolean flags (fixes #116)#136
auvipy merged 2 commits into
click-contrib:masterfrom
apoorvdarshan:fix/flag-value-completion-116

Conversation

@apoorvdarshan

Copy link
Copy Markdown
Contributor

Summary

Fixes #116.

A click option declared with is_flag=True (a store_true-style flag) does not
consume a value. However, ClickCompleter matched such flags against
BoolParamType and offered true/false value completions once the flag had
been typed. Selecting one of those completions inserted the value into the
positional-argument slot, so the command then errored with an unexpected extra
argument. Flags declared with a secondary option (--foo/--no-foo) were affected
in the same way.

Fix

Value completions are produced in ClickCompleter._get_completion_from_params.
The fix gates that logic on the parameter not being a flag: if
param.is_flag is true (which also covers --foo/--no-foo secondary opts, since
click sets is_flag=True for those too), we return no value completions. This is
localized to the completer and leaves genuine boolean value options
(type=click.BOOL, no is_flag) untouched — they still complete true/false.

Tests

Added two regression tests in
tests/test_completion/test_common_tests/test_option_completion.py:

  • test_flag_option_offers_no_value — a command with a STRING argument plus an
    is_flag=True option; after the flag is typed, no true/false completions
    are offered (matching the minimal reproduction in the issue).
  • test_boolean_flag_with_secondary_opts_offers_no_value — a --shout/--no-shout
    flag offers no value completions for either opt.

Both tests fail on the pristine code and pass with the fix. The full suite
(pytest) passes (67 passed, 5 skipped) and flake8 click_repl tests is clean.
test_boolean_option (a real type=click.BOOL value option) still passes,
confirming non-flag boolean options are unaffected.

Disclosure: prepared with AI assistance; reviewed and verified locally.

A click option declared with is_flag=True (including --foo/--no-foo style
flags with secondary opts) does not consume a value. The completer was
matching such flags against BoolParamType and offering true/false
completions; selecting one inserted the value into the positional-argument
slot, causing the command to error.

Gate value completions in _get_completion_from_params on the param not
being a flag, and add completer regression tests.

Fixes click-contrib#116.
@auvipy
auvipy requested review from auvipy and a lite review from Copilot August 4, 2026 07:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes boolean flag options (is_flag=True, including --foo/--no-foo) incorrectly receiving true/false value completions, which could insert an extra positional argument and cause Click to error.

Changes:

  • Prevent value completions from being generated for Click parameters that are flags (param.is_flag=True).
  • Add regression tests to ensure flag options do not offer true/false completions, including secondary option forms.
  • Document the change in the changelog under “Unreleased”.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
click_repl/_completer.py Gates boolean (and other) value completion generation to skip flag parameters.
tests/test_completion/test_common_tests/test_option_completion.py Adds regression tests asserting no value completions are offered for is_flag=True options.
Changelog.rst Adds an Unreleased entry describing the fixed completion behavior for boolean flags.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread click_repl/_completer.py Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@auvipy
auvipy merged commit 5d4184b into click-contrib:master Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flags are treated as regular boolean values

3 participants