Skip to content
Myr

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.

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

State

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

Trailers

  • Refs: #3111

Diff stats

1 file+25 added−5 removed

  • other:+25 added−5 removed

1 file+25 added−5 removed

modified:src/click/core.py+25 added-5 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 """2918 value = super().get_default(ctx, call=False)29192896 # 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/31212900 if value is True and self.is_flag:2920 if value is True and self.is_flag and not self.is_bool_flag:2921 value = self.flag_value2922 elif call and callable(value):2923 value = value()2924
533 unmodified lines