Do not offer value completions for boolean flags (fixes #116) - #136
Open
apoorvdarshan wants to merge 1 commit into
Open
Do not offer value completions for boolean flags (fixes #116)#136apoorvdarshan wants to merge 1 commit into
apoorvdarshan wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #116.
A click option declared with
is_flag=True(astore_true-style flag) does notconsume a value. However,
ClickCompletermatched such flags againstBoolParamTypeand offeredtrue/falsevalue completions once the flag hadbeen 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 affectedin 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_flagis true (which also covers--foo/--no-foosecondary opts, sinceclick sets
is_flag=Truefor those too), we return no value completions. This islocalized to the completer and leaves genuine boolean value options
(
type=click.BOOL, nois_flag) untouched — they still completetrue/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 aSTRINGargument plus anis_flag=Trueoption; after the flag is typed, notrue/falsecompletionsare offered (matching the minimal reproduction in the issue).
test_boolean_flag_with_secondary_opts_offers_no_value— a--shout/--no-shoutflag 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) andflake8 click_repl testsis clean.test_boolean_option(a realtype=click.BOOLvalue option) still passes,confirming non-flag boolean options are unaffected.
Disclosure: prepared with AI assistance; reviewed and verified locally.