Regroup sentinels (#3048)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This is a small refactor to regroup all sentinels used across the Click codebase to the same private _utils.py file.
authored by Rowlando13 · committed by GitHub · parents b0ffa61, 9c07924 · Browse files · 3 files·+24 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+24 added−14 removed
- other:+24 added−14 removed
Provenance
- Commit
0afce31- Tree
6741a7d- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
b0ffa61- Parent 2
9c07924
3 files changed+24 added−14 removed
modified:src/click/_utils.py+12 added-1 removed
11 unmodified lines12 `PEP 661 - Sentinel Values <https://peps.python.org/pep-0661/>`_.13 """14 15 UNSET = object()16+ FLAG_NEEDS_VALUE = object()17 18 def __repr__(self) -> str:19 return f"{self.__class__.__name__}.{self.name}"20 21 22 UNSET = Sentinel.UNSET22-"""A sentinel object used to indicate that a value is not set."""23+"""Sentinel used to indicate that a value is not set."""24 25+FLAG_NEEDS_VALUE = Sentinel.FLAG_NEEDS_VALUE26+"""Sentinel used to indicate an option was passed as a flag without a27+value but is not a flag option.28+29+``Option.consume_value`` uses this to prompt or use the ``flag_value``.30+"""31+32 T_UNSET = t.Literal[UNSET] # type: ignore[valid-type]33 """Type hint for the :data:`UNSET` sentinel value."""34+35+T_FLAG_NEEDS_VALUE = t.Literal[FLAG_NEEDS_VALUE] # type: ignore[valid-type]36+"""Type hint for the :data:`FLAG_NEEDS_VALUE` sentinel value."""modified:src/click/core.py+5 added-5 removed
17 unmodified lines18 from itertools import repeat19 from types import TracebackType20 21 from . import types22+from ._utils import FLAG_NEEDS_VALUE23 from ._utils import UNSET24 from .exceptions import Abort25 from .exceptions import BadParameter26 from .exceptions import ClickException4 unmodified lines31 from .formatting import HelpFormatter32 from .formatting import join_options33 from .globals import pop_context34 from .globals import push_context34-from .parser import _flag_needs_value35 from .parser import _OptionParser36 from .parser import _split_opt37 from .termui import confirm38 from .termui import prompt3145 unmodified lines3184 value, source = super().consume_value(ctx, opts)3185 3186 # The parser will emit a sentinel value if the option is allowed to as a flag3187 # without a value.3188- if value is _flag_needs_value:3188+ if value is FLAG_NEEDS_VALUE:3189 # If the option allows for a prompt, we start an interaction with the user.3190 if self.prompt is not None and not ctx.resilient_parsing:3191 value = self.prompt_for_value(ctx)3192 source = ParameterSource.PROMPT13 unmodified lines3206 value = self.flag_value3207 3208 # Re-interpret a multiple option which has been sent as-is by the parser.3209 # Here we replace each occurrence of value-less flags (marked by the3210- # _flag_needs_value sentinel) with the flag_value.3210+ # FLAG_NEEDS_VALUE sentinel) with the flag_value.3211 elif (3212 self.multiple3213 and value is not UNSET3214 and source not in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP)3215- and any(v is _flag_needs_value for v in value)3215+ and any(v is FLAG_NEEDS_VALUE for v in value)3216 ):3217- value = [self.flag_value if v is _flag_needs_value else v for v in value]3217+ value = [self.flag_value if v is FLAG_NEEDS_VALUE else v for v in value]3218 source = ParameterSource.COMMANDLINE3219 3220 # The value wasn't set, or used the param's default, prompt for one to the user3221 # if prompting is enabled.123 unmodified linesmodified:src/click/parser.py+7 added-8 removed
28 unmodified lines29 from collections import deque30 from gettext import gettext as _31 from gettext import ngettext32 33+from ._utils import FLAG_NEEDS_VALUE34 from ._utils import UNSET35 from .exceptions import BadArgumentUsage36 from .exceptions import BadOptionUsage37 from .exceptions import NoSuchOption38 from .exceptions import UsageError39 40 if t.TYPE_CHECKING:41+ from ._utils import T_FLAG_NEEDS_VALUE42 from ._utils import T_UNSET43 from .core import Argument as CoreArgument44 from .core import Context45 from .core import Option as CoreOption46 from .core import Parameter as CoreParameter47 48 V = t.TypeVar("V")49 48-# Sentinel value that indicates an option was passed as a flag without a49-# value but is not a flag option. Option.consume_value uses this to50-# prompt or use the flag_value.51-_flag_needs_value = object()50 53-51 def _unpack_args(52 args: cabc.Sequence[str], nargs_spec: cabc.Sequence[int]53 ) -> tuple[cabc.Sequence[str | cabc.Sequence[str | None] | None], list[str]]:54 """Given an iterable of arguments and an iterable of nargs specifications,372 unmodified lines427 state.largs.append(f"{prefix}{''.join(unknown_options)}")428 429 def _get_value_from_state(430 self, option_name: str, option: _Option, state: _ParsingState434- ) -> t.Any:431+ ) -> str | cabc.Sequence[str] | T_FLAG_NEEDS_VALUE:432 nargs = option.nargs433 434+ value: str | cabc.Sequence[str] | T_FLAG_NEEDS_VALUE435+436 if len(state.rargs) < nargs:437 if option.obj._flag_needs_value:438 # Option allows omitting the value.440- value = _flag_needs_value439+ value = FLAG_NEEDS_VALUE440 else:441 raise BadOptionUsage(442 option_name,443 ngettext(12 unmodified lines456 and len(next_rarg) > 1457 ):458 # The next arg looks like the start of an option, don't459 # use it as the value if omitting the value is allowed.461- value = _flag_needs_value460+ value = FLAG_NEEDS_VALUE461 else:462 value = state.rargs.pop(0)463 else:464 value = tuple(state.rargs[:nargs])68 unmodified lines