Skip to content

Commit 029bbed

Browse files
committed
Hide UNSET in the callback
Refs: #3030 (comment)
1 parent a2b699a commit 029bbed

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

CHANGES.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ Unreleased
1313
This allow ``default`` to be of any type, including ``bool`` or ``None``, fixing
1414
inconsistencies reported in: :issue:`1992` :issue:`2012` :issue:`2514`
1515
:issue:`2610` :issue:`3024` :pr:`3030`
16-
- Custom ``callback`` function are now allowed to receive the ``UNSET`` sentinel
17-
value. :pr:`3030`
1816
- Allow ``default`` to be set on ``Argument`` for ``nargs = -1``. :issue:`2164`
1917
:pr:`3030`
2018
- Show correct auto complete value for ``nargs`` option in combination with flag

src/click/core.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,12 +2363,9 @@ def process_value(self, ctx: Context, value: t.Any) -> t.Any:
23632363
2. Check if the value is missing (see: :meth:`value_is_missing`), and raise
23642364
:exc:`MissingParameter` if it is required.
23652365
3. If a :attr:`callback` is set, call it to have the value replaced by the
2366-
result of the callback. The callback is receiving the value as-is, which let
2367-
the developer decide how to handle the different cases.
2368-
2369-
.. versionchanged:: 8.3
2370-
The :attr:`callback` gets an internal sentinel if the parameter was not set
2371-
by the user and no default was specified.
2366+
result of the callback. If the value was not set, the callback receive
2367+
``None``. This keep the legacy behavior as it was before the introduction of
2368+
the :attr:`UNSET` sentinel.
23722369
23732370
:meta private:
23742371
"""
@@ -2378,6 +2375,10 @@ def process_value(self, ctx: Context, value: t.Any) -> t.Any:
23782375
raise MissingParameter(ctx=ctx, param=self)
23792376

23802377
if self.callback is not None:
2378+
# Legacy case: UNSET is not exposed directly to the callback, but converted
2379+
# to None.
2380+
if value is UNSET:
2381+
value = None
23812382
value = self.callback(ctx, self, value)
23822383

23832384
return value

tests/test_options.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,8 @@ def cmd(foo):
14781478
("xMl", [], "xMl"),
14791479
(" ᕕ( ᐛ )ᕗ ", [], " ᕕ( ᐛ )ᕗ "),
14801480
(None, [], None),
1481-
(UNSET, [], UNSET),
1481+
# Legacy case: UNSET is not exposed directly to the callback, but converted to None.
1482+
(UNSET, [], None),
14821483
# Legacy case: if default=True and flag_value is set, The value returned is the
14831484
# flag_value, not default itself.
14841485
(True, [], "js"),

0 commit comments

Comments
 (0)