Skip to content
Myr

Revert "Use `default=True` as a sentinel for non-boolean flags"

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This reverts commit 6c4a77ba24854dab793a8ff72110a0a24c403c9f.

authored by Kevin Deldycke · committed by Kevin Deldycke · parent 6c4a77b · Browse files · 1 file+5 added−25 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+5 added−25 removed

  • other:+5 added−25 removed

1 file+5 added−25 removed

modified:src/click/core.py+5 added-25 removed
2889 unmodified lines
28902891 def get_default(2892 self, ctx: Context, call: bool = True2893 ) -> t.Any | t.Callable[[], t.Any] | None:2894 """For non-boolean flag options, ``default=True`` is treated as a2895 sentinel meaning "activate this flag by default" and is resolved to2896 :attr:`flag_value`. This resolution is performed lazily here (rather2897 than eagerly in :meth:`__init__`) to prevent callable ``flag_value``2898 values (like classes) from being instantiated prematurely2899 (:issue:`3121`).29002901 For example, with ``--upper/--lower`` feature switches where2902 ``flag_value="upper"`` and ``default=True``, the default resolves2903 to ``"upper"``.29042905 .. caution::2906 This substitution only applies to **non-boolean** flags2907 (:attr:`is_bool_flag` is ``False``). For boolean flags, ``True`` is2908 not a sentinel but a legitimate Python value, so ``default=True`` is2909 returned as-is. Without this distinction, ``flag_value=False,2910 default=True`` would silently always return ``False``, regardless of2911 whether the flag was passed or not.29122913 .. versionchanged:: 8.32914 ``default=True`` is no longer substituted with ``flag_value`` for2915 boolean flags, fixing negative boolean flags like ``flag_value=False,2916 default=True``. :issue:`3111`2917 """2894 value = super().get_default(ctx, call=False)28952896 # Lazily resolve default=True to flag_value. Doing this here2897 # (instead of eagerly in __init__) prevents callable flag_values2898 # (like classes) from being instantiated by the callable check below.2899 # https://github.com/pallets/click/issues/31212920 if value is True and self.is_flag and not self.is_bool_flag:2900 if value is True and self.is_flag:2901 value = self.flag_value2902 elif call and callable(value):2903 value = value()2904
533 unmodified lines