Fix sentinel typing and its uses in parser (#3396)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Andreas Backx · committed by GitHub · parent fed9049 · Browse files · 3 files·+17 added−16 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+17 added−16 removed
- other:+17 added−16 removed
Provenance
- Commit
73e1550- Tree
f6c2de6- Author
Andreas Backx <AndreasBackx@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
fed9049
3 files changed+17 added−16 removed
modified:src/click/_utils.py+4 added-4 removed
17 unmodified lines18 def __repr__(self) -> str:19 return f"{self.__class__.__name__}.{self.name}"20 21 22-UNSET = Sentinel.UNSET22+UNSET: t.Literal[Sentinel.UNSET] = Sentinel.UNSET23 """Sentinel used to indicate that a value is not set."""24 25-FLAG_NEEDS_VALUE = Sentinel.FLAG_NEEDS_VALUE25+FLAG_NEEDS_VALUE: t.Literal[Sentinel.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]32+T_UNSET: t.TypeAlias = t.Literal[Sentinel.UNSET]33 """Type hint for the :data:`UNSET` sentinel value."""34 35-T_FLAG_NEEDS_VALUE = t.Literal[FLAG_NEEDS_VALUE] # type: ignore[valid-type]35+T_FLAG_NEEDS_VALUE: t.TypeAlias = t.Literal[Sentinel.FLAG_NEEDS_VALUE]36 """Type hint for the :data:`FLAG_NEEDS_VALUE` sentinel value."""modified:src/click/core.py+1 added
3352 unmodified lines3353 # FLAG_NEEDS_VALUE sentinel) with the flag_value.3354 elif (3355 self.multiple3356 and value is not UNSET3357+ and isinstance(value, cabc.Iterable)3358 and source < ParameterSource.DEFAULT_MAP3359 and any(v is FLAG_NEEDS_VALUE for v in value)3360 ):3361 value = [self.flag_value if v is FLAG_NEEDS_VALUE else v for v in value]135 unmodified linesmodified:src/click/parser.py+12 added-12 removed
48 unmodified lines49 50 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]]:53+) -> tuple[cabc.Sequence[str | cabc.Sequence[str | T_UNSET] | T_UNSET], list[str]]:54 """Given an iterable of arguments and an iterable of nargs specifications,55 it returns a tuple with all the unpacked arguments at the first index56 and all remaining arguments as the second.57 6 unmodified lines64 nargs_spec = deque(nargs_spec)65 rv: list[str | tuple[str | T_UNSET, ...] | T_UNSET] = []66 spos: int | None = None67 68- def _fetch(c: deque[V]) -> V | T_UNSET:68+ def _fetch(c: deque[str]) -> str | T_UNSET:69 try:70 if spos is None:71 return c.popleft()72 else:1 unmodified line74 except IndexError:75 return UNSET76 77 while nargs_spec:78- nargs = _fetch(nargs_spec)78+ if spos is None:79+ nargs = nargs_spec.popleft()80+ else:81+ nargs = nargs_spec.pop()82 80- if nargs is None:81- continue82-83 if nargs == 1:84- rv.append(_fetch(args)) # type: ignore[arg-type]84+ rv.append(_fetch(args))85 elif nargs > 1:86- x = [_fetch(args) for _ in range(nargs)]86+ x: list[str | T_UNSET] = [_fetch(args) for _ in range(nargs)]87 88 # If we're reversed, we're pulling in the arguments in reverse,89 # so we need to turn them around.90 if spos is not None:94 unmodified lines185 self.obj = obj186 187 def process(188 self,189- value: str | cabc.Sequence[str | None] | None | T_UNSET,189+ value: str | cabc.Sequence[str | T_UNSET] | T_UNSET,190 state: _ParsingState,191 ) -> None:192 if self.nargs > 1:193 assert isinstance(value, cabc.Sequence)194- holes = sum(1 for x in value if x is UNSET)194+ holes = sum(x is UNSET for x in value)195 if holes == len(value):196 value = UNSET197 elif holes != 0:198 raise BadArgumentUsage(225 unmodified lines424 state.largs.append(f"{prefix}{''.join(unknown_options)}")425 426 def _get_value_from_state(427 self, option_name: str, option: _Option, state: _ParsingState428- ) -> str | cabc.Sequence[str] | T_FLAG_NEEDS_VALUE:428+ ) -> str | cabc.Sequence[str] | T_UNSET | T_FLAG_NEEDS_VALUE:429 nargs = option.nargs430 431- value: str | cabc.Sequence[str] | T_FLAG_NEEDS_VALUE431+ value: str | cabc.Sequence[str] | T_UNSET | T_FLAG_NEEDS_VALUE432 433 if len(state.rargs) < nargs:434 if option.obj._flag_needs_value:435 # Option allows omitting the value.94 unmodified lines