Skip to content
Myr

Treat `UNSET` in a `default_map` as absent

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 1339fd3 · Browse files · 3 files+47 added−12 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/3224#issuecomment-3968643305

Diff stats

3 files+47 added−12 removed

  • tests:+20 added−0 removed
  • other:+27 added−12 removed

3 files+47 added−12 removed

modified:src/click/core.py+24 added-12 removed
683 unmodified lines
684 if rv is None:685 self.obj = rv = object_type()686 return rv687688 def _default_map_has(self, name: str | None) -> bool:689 """Check if :attr:`default_map` contains a real value for ``name``.690691 Returns ``False`` when the key is absent, the map is ``None``,692 ``name`` is ``None``, or the stored value is the internal693 :data:`UNSET` sentinel.694 """695 return (696 name is not None697 and self.default_map is not None698 and name in self.default_map699 and self.default_map[name] is not UNSET700 )701702 @t.overload703 def lookup_default(704 self, name: str, call: t.Literal[True] = True705 ) -> t.Any | None: ...
12 unmodified lines
718719 .. versionchanged:: 8.0720 Added the ``call`` parameter.721 """708 if self.default_map is not None:709 value = self.default_map.get(name)722 if not self._default_map_has(name):723 return None724711 if call and callable(value):712 return value()725 # Assert to make the type checker happy.726 assert self.default_map is not None727 value = self.default_map[name]728729 if call and callable(value):714 return value730 return value()731716 return None732 return value733734 def fail(self, message: str) -> t.NoReturn:735 """Aborts the execution of the program with a specific error736 message.
1559 unmodified lines
2296 """2297 name = self.name2298 value = ctx.lookup_default(name, call=False) if name is not None else None22992284 if value is None and not (2285 ctx.default_map is not None and name is not None and name in ctx.default_map2286 ):2300 if value is None and not ctx._default_map_has(name):2301 value = self.default23022303 if call and callable(value):2304 value = value()
33 unmodified lines
2338 source = ParameterSource.ENVIRONMENT23392340 if value is UNSET:2341 default_map_value = ctx.lookup_default(self.name) # type: ignore[arg-type]2328 if default_map_value is not None or (2329 ctx.default_map is not None and self.name in ctx.default_map2330 ):2342 if default_map_value is not None or ctx._default_map_has(self.name):2343 value = default_map_value2344 source = ParameterSource.DEFAULT_MAP23452346 if value is UNSET:
1103 unmodified lines
modified:tests/test_defaults.py+20 added
1import pytest23import click4from click import UNPROCESSED5from click._utils import UNSET678@pytest.mark.parametrize(9 ("default", "type", "expected_output", "expected_type"),
344 unmodified lines
354 kwargs["default_map"] = default_map355 result = runner.invoke(cli, args, **kwargs)356 assert result.exit_code == 0357 assert result.output == repr(expected)358359360def test_unset_in_default_map(runner):361 """An ``UNSET`` value in ``default_map`` should be treated as if362 the key is absent, and so fallback to the parameter's own default.363364 Refs: https://github.com/pallets/click/pull/3224#issuecomment-3968643305365 """366367 @click.command(368 context_settings={"default_map": {"port": UNSET}},369 )370 @click.option("--port", default=8000)371 def cli(port):372 click.echo(f"port={port}")373374 result = runner.invoke(cli, [])375 assert result.exit_code == 0376 assert result.output.strip() == "port=8000"
modified:CHANGES.rst+3 added
11 unmodified lines
12 an object that doesn't support equality comparison with strings, such as13 ``semver.Version``. :issue:`3298` :pr:`3299`14- Fix pager test pollution under parallel execution by using pytest's15 ``tmp_path`` fixture instead of a shared temporary file path. :pr:`3238`16- Treat ``Sentinel.UNSET`` values in a ``default_map`` as absent, so they fall17 through to the next default source instead of being used as the value.18 :issue:`3224` :pr:`3240`1920Version 8.3.221-------------22
1323 unmodified lines