Skip to content
Myr

Hide UNSET in the callback

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 a2b699a · Browse files · 3 files+9 added−9 removed
This commit

State

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

Trailers

  • Refs: https://github.com/pallets/click/pull/3030#discussion_r2291553604

Diff stats

3 files+9 added−9 removed

  • tests:+2 added−1 removed
  • other:+7 added−8 removed

3 files+9 added−9 removed

modified:src/click/core.py+7 added-6 removed
2361 unmodified lines
2362 1. Type cast the value using :meth:`type_cast_value`.2363 2. Check if the value is missing (see: :meth:`value_is_missing`), and raise2364 :exc:`MissingParameter` if it is required.2365 3. If a :attr:`callback` is set, call it to have the value replaced by the2366 result of the callback. The callback is receiving the value as-is, which let2367 the developer decide how to handle the different cases.2366 result of the callback. If the value was not set, the callback receive2367 ``None``. This keep the legacy behavior as it was before the introduction of2368 the :attr:`UNSET` sentinel.23692369 .. versionchanged:: 8.32370 The :attr:`callback` gets an internal sentinel if the parameter was not set2371 by the user and no default was specified.23722370 :meta private:2371 """2372 value = self.type_cast_value(ctx, value)23732374 if self.required and self.value_is_missing(value):2375 raise MissingParameter(ctx=ctx, param=self)23762377 if self.callback is not None:2378 # Legacy case: UNSET is not exposed directly to the callback, but converted2379 # to None.2380 if value is UNSET:2381 value = None2382 value = self.callback(ctx, self, value)23832384 return value2385
949 unmodified lines
modified:tests/test_options.py+2 added-1 removed
1476 unmodified lines
1477 ("jS", [], "jS"),1478 ("xMl", [], "xMl"),1479 (" ᕕ( ᐛ )ᕗ ", [], " ᕕ( ᐛ )ᕗ "),1480 (None, [], None),1481 # Legacy case: UNSET is not exposed directly to the callback, but converted to None.1481 (UNSET, [], UNSET),1482 (UNSET, [], None),1483 # Legacy case: if default=True and flag_value is set, The value returned is the1484 # flag_value, not default itself.1485 (True, [], "js"),1486 # Non-string defaults are process as strings by the default Parameter's type.
722 unmodified lines
modified:CHANGES.rst-2 removed
11 unmodified lines
12 ``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`16- Custom ``callback`` function are now allowed to receive the ``UNSET`` sentinel17 value. :pr:`3030`16- Allow ``default`` to be set on ``Argument`` for ``nargs = -1``. :issue:`2164`17 :pr:`3030`18- Show correct auto complete value for ``nargs`` option in combination with flag19 option :issue:`2813`
1266 unmodified lines