Skip to content
Myr

Regroup sentinels

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 b0ffa61 · 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

3 files+24 added−14 removed

modified:src/click/_utils.py+12 added-1 removed
11 unmodified lines
12 `PEP 661 - Sentinel Values <https://peps.python.org/pep-0661/>`_.13 """1415 UNSET = object()16 FLAG_NEEDS_VALUE = object()1718 def __repr__(self) -> str:19 return f"{self.__class__.__name__}.{self.name}"202122UNSET = 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."""2425FLAG_NEEDS_VALUE = Sentinel.FLAG_NEEDS_VALUE26"""Sentinel used to indicate an option was passed as a flag without a27value but is not a flag option.2829``Option.consume_value`` uses this to prompt or use the ``flag_value``.30"""3132T_UNSET = t.Literal[UNSET] # type: ignore[valid-type]33"""Type hint for the :data:`UNSET` sentinel value."""3435T_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 lines
18from itertools import repeat19from types import TracebackType2021from . import types22from ._utils import FLAG_NEEDS_VALUE23from ._utils import UNSET24from .exceptions import Abort25from .exceptions import BadParameter26from .exceptions import ClickException
4 unmodified lines
31from .formatting import HelpFormatter32from .formatting import join_options33from .globals import pop_context34from .globals import push_context34from .parser import _flag_needs_value35from .parser import _OptionParser36from .parser import _split_opt37from .termui import confirm38from .termui import prompt
3145 unmodified lines
3184 value, source = super().consume_value(ctx, opts)31853186 # 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.PROMPT
13 unmodified lines
3206 value = self.flag_value32073208 # 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.COMMANDLINE32193220 # The value wasn't set, or used the param's default, prompt for one to the user3221 # if prompting is enabled.
123 unmodified lines
modified:src/click/parser.py+7 added-8 removed
28 unmodified lines
29from collections import deque30from gettext import gettext as _31from gettext import ngettext3233from ._utils import FLAG_NEEDS_VALUE34from ._utils import UNSET35from .exceptions import BadArgumentUsage36from .exceptions import BadOptionUsage37from .exceptions import NoSuchOption38from .exceptions import UsageError3940if 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 CoreParameter4748V = t.TypeVar("V")4948# 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()505351def _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 lines
427 state.largs.append(f"{prefix}{''.join(unknown_options)}")428429 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.nargs433434 value: str | cabc.Sequence[str] | T_FLAG_NEEDS_VALUE435436 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 lines
456 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