Fix flag_value class instantiation when default=True by veeceey · Pull Request #3201 · pallets/click

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)