Skip to content

Comments

Fix flag_value class instantiation when default=True#3201

Closed
veeceey wants to merge 1 commit intopallets:mainfrom
veeceey:fix/issue-3121-flag-value-instantiation
Closed

Fix flag_value class instantiation when default=True#3201
veeceey wants to merge 1 commit intopallets:mainfrom
veeceey:fix/issue-3121-flag-value-instantiation

Conversation

@veeceey
Copy link

@veeceey veeceey commented Feb 8, 2026

Summary

Fixes #3121

When using a class or callable as flag_value with default=True, the class was being instantiated instead of being used as-is in Click 8.3.0.

Root Cause

In Click 8.3.0, when default=True and flag_value is set, the Option initialization automatically sets self.default = self.flag_value to maintain backward compatibility. However, when get_default() is later called, it checks if the default is callable and calls it, which causes classes and other callables to be instantiated.

Solution

  • Track whether the default was auto-converted from True to flag_value using a new _default_is_auto_flag_value flag
  • In get_default(), only call the default if it's not an auto-converted flag value
  • When users explicitly set default=SomeCallable, it still gets called as expected (preserving existing behavior)

Test Plan

  • All existing tests pass (1319 passed)
  • Updated test expectations for cases that were testing the buggy behavior
  • Verified fix with reproduction cases:
    • default=True, flag_value=SomeClass → returns class (not instance)
    • default=SomeClass, flag_value=SomeClass → returns instance (as intended)
    • --flag explicitly passed → returns class (correct behavior)

When using a class or callable as flag_value with default=True, the
class was being instantiated instead of used as-is. This occurred
because the option initialization set self.default = self.flag_value
when default=True, and later get_default() would call any callable
defaults.

The fix tracks whether the default was auto-set from True to flag_value
and prevents calling it in that case. When users explicitly set
default=SomeCallable, it still gets called as expected.

Fixes pallets#3121
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using a class as a flag_value causes the default to be instantiated in click 8.3.0

3 participants