Skip to content
Myr

Reduce blast-radius of `UNSET` in `default_map` (#3240)

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

authored by Rowlando13 · committed by GitHub · parents c7e1ba8, c07bb93 · 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.

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
696 unmodified lines
697 if rv is None:698 self.obj = rv = object_type()699 return rv700701 def _default_map_has(self, name: str | None) -> bool:702 """Check if :attr:`default_map` contains a real value for ``name``.703704 Returns ``False`` when the key is absent, the map is ``None``,705 ``name`` is ``None``, or the stored value is the internal706 :data:`UNSET` sentinel.707 """708 return (709 name is not None710 and self.default_map is not None711 and name in self.default_map712 and self.default_map[name] is not UNSET713 )714715 @t.overload716 def lookup_default(717 self, name: str, call: t.Literal[True] = True718 ) -> t.Any | None: ...
12 unmodified lines
731732 .. versionchanged:: 8.0733 Added the ``call`` parameter.734 """721 if self.default_map is not None:722 value = self.default_map.get(name)735 if not self._default_map_has(name):736 return None737724 if call and callable(value):725 return value()738 # Assert to make the type checker happy.739 assert self.default_map is not None740 value = self.default_map[name]741742 if call and callable(value):727 return value743 return value()744729 return None745 return value746747 def fail(self, message: str) -> t.NoReturn:748 """Aborts the execution of the program with a specific error749 message.
1559 unmodified lines
2309 """2310 name = self.name2311 value = ctx.lookup_default(name, call=False) if name is not None else None23122297 if value is None and not (2298 ctx.default_map is not None and name is not None and name in ctx.default_map2299 ):2313 if value is None and not ctx._default_map_has(name):2314 value = self.default23152316 if call and callable(value):2317 value = value()
33 unmodified lines
2351 source = ParameterSource.ENVIRONMENT23522353 if value is UNSET:2354 default_map_value = ctx.lookup_default(self.name) # type: ignore[arg-type]2341 if default_map_value is not None or (2342 ctx.default_map is not None and self.name in ctx.default_map2343 ):2355 if default_map_value is not None or ctx._default_map_has(self.name):2356 value = default_map_value2357 source = ParameterSource.DEFAULT_MAP23582359 if value is UNSET:
1119 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`19- Patch ``pdb.Pdb`` in ``CliRunner`` isolation so ``pdb.set_trace()``,20 ``breakpoint()``, and debuggers subclassing ``pdb.Pdb`` (ipdb, pdbpp) can21 interact with the real terminal instead of the captured I/O streams.22 :issue:`654` :issue:`824` :issue:`843` :pr:`951` :pr:`3235`
1347 unmodified lines