Tone-down the case of `default=True` for flags in documentation (#3049)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rowlando13 · committed by GitHub · parents 0afce31, a245613 · Browse files · 5 files·+33 added−51 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+33 added−51 removed
- tests:+5 added−5 removed
- other:+28 added−46 removed
Provenance
- Commit
10b77f9- Tree
26692e0- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
0afce31- Parent 2
a245613
5 files changed+33 added−51 removed
modified:docs/options.md+5 added-9 removed
333 unmodified lines334 invoke(info, args=['--lower'])335 invoke(info)336 ```337 338-````{caution}339-The `default` argument of options always give to the underlying function its value *as-is*.338+````{note}339+The `default` value is given to the underlying function as-is. So if you set `default=None`, the value passed to the function is the `None` Python value. Same for any other type.340 341-But for flags, the interaction between `flag_value` and `default` is a bit special.341+But there is a special case for flags. If a flag has a `flag_value`, then setting `default=True` is interpreted as *the flag should be activated by default*. So instead of the underlying function receiving the `True` Python value, it will receive the `flag_value`.342 343-If a flag has a `flag_value`, setting `default` to `True` means that the flag is activated by default. Not that the value passed to the underlying function is the `True` Python value. Instead, the default value will be aligned to the `flag_value` behind the scenes.343+Which means, in example above, this option:344 345-Which means, the in example above, this option:346-345 ```python346 @click.option('--upper', 'transformation', flag_value='upper', default=True)347 ```348 2 unmodified lines351 ```python352 @click.option('--upper', 'transformation', flag_value='upper', default='upper')353 ```354 357-This was implemented to support legacy behavior, that will be removed in Click 9.0 to allow for default to take any value, including `True`.358-359-In the mean time, to avoid confusion, it is recommended to always set `default` to the actual default value you want to pass to the underlying function, and not use `True`, `False` or `None`. Unless that's the precise value you want to explicitly force as default.355+Because the two are equivalent, it is recommended to always use the second form, and set `default` to the actual value you want to pass. And not use the special `True` case. This makes the code more explicit and predictable.356 ````357 358 ## Values from Environment Variables359 138 unmodified linesmodified:src/click/core.py+13 added-29 removed
2604 unmodified lines2605 :param help: the help string.2606 :param hidden: hide this option from help outputs.2607 :param attrs: Other command arguments described in :class:`Parameter`.2608 2609- .. caution::2610- Flags specifying a ``flag_value`` and whose ``default=True`` will have2611- their ``default`` aligned to the ``flag_value``.2612-2613- This means there is no way to setup a flag whose default ``True`` and2614- whose ``flag_value`` is something else than ``True``.2615-2616- This is to support legacy behavior that will be removed in Click 9.0.2617-2609 .. versionchanged:: 8.22610 ``envvar`` used with ``flag_value`` will always use the ``flag_value``,2611 previously it would use the value of the environment variable.2612 119 unmodified lines2732 if self.is_bool_flag:2733 if self.default is UNSET and not self.required:2734 self.default = False2735 2745- # XXX Support the legacy case of aligning the default value with the flag_value2746- # for flags whose default is explicitly set to True. As long as we have this2747- # condition, there is no way a flag can have a default set to True, unless its2748- # flag_value itself is set to True. Refs:2736+ # Support the special case of aligning the default value with the flag_value2737+ # for flags whose default is explicitly set to True. Note that as long as we2738+ # have this condition, there is no way a flag can have a default set to True,2739+ # and a flag_value set to something else. Refs:2740 # https://github.com/pallets/click/issues/3024#issuecomment-31461994612750- # https://github.com/pallets/click/pull/3030/files#r22889364932741+ # https://github.com/pallets/click/pull/3030/commits/06847da2742 if self.default is True and self.flag_value is not UNSET:2752- # This message is a convoluted way to explain that if you want things2753- # to be equal, make them equal.2754- # warnings.warn(2755- # "A flag's `default` value will no longer be aligned with its "2756- # "`flag_value` if `default=True` in Click 9.0. If you want the flag "2757- # "to get the same `default` as its `flag_value`, update the option "2758- # "to make its `default` parameter equal to its `flag_value`.",2759- # DeprecationWarning,2760- # stacklevel=2,2761- # )2743 self.default = self.flag_value2744 2745 # Set the default flag_value if it is not set.2746 if self.flag_value is UNSET:303 unmodified lines3050 # If we have no boolean default, we force the user to explicitly provide3051 # one.3052 if default in (UNSET, None):3053 default = None3073- # Nothing prevent you to declare an option that is auto-detected as a3074- # boolean flag, is allow to prompt but still declare a non-boolean default.3075- # So with this casting, we aligns the default value to the prompt behavior.3076- # The prompt is going to default to [Y/n]), and so not entering a value for3077- # input is expected to make the option takes True as the default.3054+ # Nothing prevent you to declare an option that is simultaneously:3055+ # 1) auto-detected as a boolean flag,3056+ # 2) allowed to prompt, and3057+ # 3) still declare a non-boolean default.3058+ # This forced casting into a boolean is necessary to align any non-boolean3059+ # default to the prompt, which is going to be a [y/n]-style confirmation3060+ # because the option is still a boolean flag. That way, instead of [y/n],3061+ # we get [Y/n] or [y/N] depending on the truthy value of the default.3062 # Refs: https://github.com/pallets/click/pull/3030#discussion_r22891802493063 else:3064 default = bool(default)3065 return confirm(self.prompt, default)263 unmodified linesmodified:tests/test_basic.py+2 added-2 removed
300 unmodified lines301 (" ᕕ( ᐛ )ᕗ ", [], " ᕕ( ᐛ )ᕗ "),302 (None, [], None),303 # Default is normalized to None if it is UNSET.304 (UNSET, [], None),305- # Legacy case: if default=True and flag_value is set, The value returned is the306- # flag_value, not default itself.305+ # Special case: if default=True and flag_value is set, the value returned is the306+ # flag_value, not the True Python value itself.307 (True, [], "upper"),308 # Non-string defaults are process as strings by the default Parameter's type.309 (False, [], "False"),310 (42, [], "42"),425 unmodified linesmodified:tests/test_options.py+3 added-3 removed
1476 unmodified lines1477 ("jS", [], "jS"),1478 ("xMl", [], "xMl"),1479 (" ᕕ( ᐛ )ᕗ ", [], " ᕕ( ᐛ )ᕗ "),1480 (None, [], None),1481- # Legacy case: UNSET is not exposed directly to the callback, but converted to1481+ # Special case: UNSET is not provided as-is to the callback, but normalized to1482 # None.1483 (UNSET, [], None),1484- # Legacy case: if default=True and flag_value is set, The value returned is the1485- # flag_value, not default itself.1484+ # Special case: if default=True and flag_value is set, the value returned is the1485+ # flag_value, not the True Python value itself.1486 (True, [], "js"),1487 # Non-string defaults are process as strings by the default Parameter's type.1488 (False, [], "False"),1489 (42, [], "42"),720 unmodified linesmodified:CHANGES.rst+10 added-8 removed
3 unmodified lines4 --------------5 6 Unreleased7 8-- Rework relationship between ``flag_value`` and ``default``: the value given to9- ``default`` is now left untouched, and keep the value it receive. So10- ``default=<desired_value>`` is respected and ``<desired_value>`` is passed on as-is11- to the CLI function. With the exception of flag options, where setting12- ``default=True`` maintain the legacy behavior of defaulting to the ``flag_value``.13- This allow ``default`` to be of any type, including ``bool`` or ``None``, fixing14- inconsistencies reported in: :issue:`1992` :issue:`2012` :issue:`2514`15- :issue:`2610` :issue:`3024` :pr:`3030`8+- **Improved flag option handling**: Reworked the relationship between ``flag_value``9+ and ``default`` parameters for better consistency:10+11+ * The ``default`` parameter value is now preserved as-is and passed directly12+ to CLI functions (no more unexpected transformations)13+ * Exception: flag options with ``default=True`` maintain backward compatibility14+ by defaulting to their ``flag_value``15+ * The ``default`` parameter can now be any type (``bool``, ``None``, etc.)16+ * Fixes inconsistencies reported in: :issue:`1992` :issue:`2514` :issue:`2610`17+ :issue:`3024` :pr:`3030`18 - Allow ``default`` to be set on ``Argument`` for ``nargs = -1``. :issue:`2164`19 :pr:`3030`20 - Show correct auto complete value for ``nargs`` option in combination with flag21 option :issue:`2813`1269 unmodified lines