Skip to content

Fix negative flag (flag_value=False) ignoring default=True#3308

Closed
vigneshakaviki wants to merge 2 commits intopallets:mainfrom
vigneshakaviki:fix/negative-flag-true-default
Closed

Fix negative flag (flag_value=False) ignoring default=True#3308
vigneshakaviki wants to merge 2 commits intopallets:mainfrom
vigneshakaviki:fix/negative-flag-true-default

Conversation

@vigneshakaviki
Copy link
Copy Markdown

Fixes #3111

A negative flag (flag_value=False, default=True) was incorrectly returning False when the flag was not passed.

Root cause: Option.get_default() unconditionally converted default=True to flag_value for all flag types. For negative flags where flag_value=False, this silently overrode the explicit default.

Fix: Skip the conversion when flag_value is False:

if value is True and self.is_flag and self.flag_value is not False:
    value = self.flag_value

This preserves legacy behaviour for all other flag types while correctly keeping default=True for negative flags.

Tests: Added test_negative_flag_with_true_default regression test; updated existing parametrized cases that encoded the buggy behaviour. Full suite: 1386 passed, 21 skipped.

A flag option with flag_value=False (a negative flag) and default=True
was incorrectly returning False when the flag was not passed.

get_default() was unconditionally converting default=True to flag_value
for all flag types, including negative flags where flag_value=False. This
caused default=True to be silently overridden with False.

Fix the condition to skip the conversion when flag_value is explicitly
False, preserving default=True as the actual default for negative flags.

Fixes pallets#3111
@vigneshakaviki
Copy link
Copy Markdown
Author

Closing this in favour of the existing fix in #3239 by @kdeldycke, which covers this case more comprehensively. Thanks!

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.

New "default" behaviour in Click 8.3.x is broken for negative boolean flags

1 participant