Fix get_parameter_source() during type conversion and eager callbacks
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Record parameter source on the context immediately after consume_value(), before process_value() runs. In 8.4.0, set_parameter_source() was deferred until after type conversion and flag-group arbitration (0f71fe7, #3403), so get_parameter_source() returned None inside ParamType.convert() and eager callbacks (#3458). When several options share one parameter name, the losing option still sets a provisional source before process_value(); restore the previous source if arbitration rejects it so the winner's origin is not replaced unintentionally.
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+196 added−5 removed
- tests:+185 added−0 removed
- other:+11 added−5 removed
Provenance
- Commit
7d05a59- Tree
dd2aee0- Author
Yuri Shevtsov <yuri.shevtsov@mobiledefense.com>- Committer
Kevin Deldycke <kevin@deldycke.com>- Parent
4b24a6c
4 files changed+196 added−5 removed
modified:src/click/core.py+9 added-5 removed
2607 unmodified lines2608 2609 with augment_usage_errors(ctx, param=self):2610 value, source = self.consume_value(ctx, opts)2611 2612+ # Record the source before processing so eager callbacks and type2613+ # conversion can inspect it. Restored after arbitration if this2614+ # option loses a feature-switch group.2615+ ctx.set_parameter_source(self.name, source)2616+2617 # Display a deprecation warning if necessary.2618 if (2619 self.deprecated2620 and value is not UNSET37 unmodified lines2658 or (same_source and not auto_would_downgrade_explicit)2659 )2660 2661 if is_winner:2657- ctx.set_parameter_source(self.name, source)2662 if self.expose_value:2663 ctx.params[self.name] = value2664 ctx._param_default_explicit[self.name] = self._default_explicit2661- elif existing_source is None:2662- # Nothing has claimed the slot yet. Record at least our source so downstream2663- # lookups don't return ``None``.2664- ctx.set_parameter_source(self.name, source)2665+ elif existing_source is not None:2666+ # Lost arbitration; restore the winning option's source.2667+ ctx.set_parameter_source(self.name, existing_source)2668+ # else: keep the provisional source recorded before process_value.2669 2670 return value, args2671 2672 def get_help_record(self, ctx: Context) -> tuple[str, str] | None:867 unmodified linesmodified:tests/test_defaults.py+123 added
1+import os2+3 import pytest4 5 import click6 from click import UNPROCESSED7 from click._utils import UNSET8+from click.core import ParameterSource9 10 11 @pytest.mark.parametrize(12 ("default", "type", "expected_output", "expected_type"),254 unmodified lines267 assert f"name={expected_value!r}" in result.output268 assert f"source={expected_source}" in result.output269 270 271+def test_parameter_source_during_paramtype_convert(runner):272+ """``get_parameter_source()`` is available during ``ParamType.convert``.273+274+ Uses the reproducer from https://github.com/pallets/click/issues/3458.275+ """276+277+ class Source(click.ParamType):278+ name = "source"279+280+ def convert(self, value, param, ctx):281+ return {282+ "value": value,283+ "source": ctx.get_parameter_source(param.name),284+ }285+286+ @click.command()287+ @click.option("--default", type=Source(), default="/tmp/file")288+ @click.option("--nodefault", type=Source())289+ def cli(default, nodefault):290+ click.echo(f"default: {default}")291+ click.echo(f"nodefault: {nodefault}")292+293+ result = runner.invoke(cli, [])294+ assert not result.exception295+ assert "default: {'value': '/tmp/file', 'source': " in result.output296+ assert "'source': None}" not in result.output.split("default:")[1].split("\n")[0]297+ assert (298+ result.output == "default: {'value': '/tmp/file', 'source': "299+ f"{ParameterSource.DEFAULT!r}}}\nnodefault: None\n"300+ )301+302+ result = runner.invoke(cli, ["--default", "cli", "--nodefault", "also"])303+ assert not result.exception304+ assert (305+ "default: {'value': 'cli', 'source': "306+ f"{ParameterSource.COMMANDLINE!r}}}" in result.output307+ )308+ assert (309+ "nodefault: {'value': 'also', 'source': "310+ f"{ParameterSource.COMMANDLINE!r}}}" in result.output311+ )312+313+314+def test_parameter_source_during_eager_callback(runner):315+ """``get_parameter_source()`` is available during eager callbacks.316+317+ Regression test for https://github.com/pallets/click/issues/3458.318+ """319+320+ def eager_cb(ctx, param, value):321+ source = ctx.get_parameter_source(param.name)322+ click.echo(f"callback source={source.name if source else None}")323+324+ @click.command()325+ @click.option(326+ "--flag/--no-flag",327+ default=False,328+ is_eager=True,329+ callback=eager_cb,330+ expose_value=False,331+ )332+ def cli():333+ source = click.get_current_context().get_parameter_source("flag")334+ click.echo(f"final source={source.name}")335+336+ result = runner.invoke(cli, [])337+ assert not result.exception338+ assert "callback source=DEFAULT" in result.output339+ assert "final source=DEFAULT" in result.output340+341+ result = runner.invoke(cli, ["--flag"])342+ assert not result.exception343+ assert "callback source=COMMANDLINE" in result.output344+ assert "final source=COMMANDLINE" in result.output345+346+347+def test_flask_debug_env_not_stomped_by_default_flag(runner, monkeypatch):348+ """Eager callback must not overwrite env when the flag used its default.349+350+ Covers the Flask ``_set_debug`` pattern (pallets/flask#6025). Regression test351+ for https://github.com/pallets/click/issues/3458.352+ """353+354+ monkeypatch.delenv("APP_DEBUG", raising=False)355+356+ def set_debug(ctx, param, value):357+ source = ctx.get_parameter_source(param.name)358+ if source is not None and source in (359+ ParameterSource.DEFAULT,360+ ParameterSource.DEFAULT_MAP,361+ ):362+ return None363+ os.environ["APP_DEBUG"] = "1" if value else "0"364+ return value365+366+ @click.command()367+ @click.option(368+ "--debug/--no-debug",369+ default=False,370+ is_eager=True,371+ expose_value=False,372+ callback=set_debug,373+ )374+ def cli():375+ click.echo(f"APP_DEBUG={os.environ.get('APP_DEBUG', '')}")376+377+ monkeypatch.setenv("APP_DEBUG", "1")378+ result = runner.invoke(cli, [])379+ assert result.exit_code == 0380+ assert result.output.strip() == "APP_DEBUG=1"381+382+ result = runner.invoke(cli, ["--debug"])383+ assert result.exit_code == 0384+ assert result.output.strip() == "APP_DEBUG=1"385+386+ result = runner.invoke(cli, ["--no-debug"])387+ assert result.exit_code == 0388+ assert result.output.strip() == "APP_DEBUG=0"389+390+391 def test_lookup_default_override_respected(runner):392 """A subclass override of ``lookup_default()`` should be called by Click393 internals, not bypassed by a private method.394 142 unmodified linesmodified:tests/test_options.py+62 added
2894 unmodified lines2895 assert result.output == repr(expected)2896 2897 2898 @pytest.mark.parametrize(2899+ ("opts", "args", "invoke_kwargs", "expected_value", "expected_source"),2900+ [2901+ # https://github.com/pallets/click/issues/34582902+ pytest.param(2903+ [2904+ ("--without-xyz", {"flag_value": False}),2905+ ("--with-xyz", {"flag_value": True, "default": True}),2906+ ],2907+ [],2908+ {},2909+ True,2910+ "DEFAULT",2911+ id="explicit-default-wins",2912+ ),2913+ pytest.param(2914+ [2915+ ("--without-xyz", {"flag_value": False}),2916+ ("--with-xyz", {"flag_value": True, "default": True}),2917+ ],2918+ ["--without-xyz"],2919+ {},2920+ False,2921+ "COMMANDLINE",2922+ id="cmdline-wins",2923+ ),2924+ pytest.param(2925+ [2926+ ("--without-xyz", {"flag_value": False}),2927+ ("--with-xyz", {"flag_value": True, "default": True}),2928+ ],2929+ ["--without-xyz"],2930+ {"default_map": {"enable_xyz": True}},2931+ False,2932+ "COMMANDLINE",2933+ id="loser-default-map-restores-winner-source",2934+ ),2935+ ],2936+)2937+def test_bool_flag_group_parameter_source(2938+ runner, opts, args, invoke_kwargs, expected_value, expected_source2939+):2940+ """``get_parameter_source()`` stays correct for feature-switch groups.2941+2942+ Regression test for https://github.com/pallets/click/issues/3458.2943+ """2944+2945+ @click.command()2946+ @click.pass_context2947+ def cli(ctx, enable_xyz):2948+ source = ctx.get_parameter_source("enable_xyz")2949+ click.echo(f"value={enable_xyz!r} source={source.name}")2950+2951+ for opt_name, opt_kwargs in opts:2952+ cli = click.option(opt_name, "enable_xyz", **opt_kwargs)(cli)2953+2954+ result = runner.invoke(cli, args, **invoke_kwargs)2955+ assert result.exit_code == 0, result.output2956+ assert f"value={expected_value!r}" in result.output2957+ assert f"source={expected_source}" in result.output2958+2959+2960+@pytest.mark.parametrize(2961 ("opts", "args", "expected"),2962 [2963 # Non-boolean feature switch group: classic --upper/--lower2964 # pattern. The option with ``default=True`` acts as the default539 unmodified linesmodified:CHANGES.rst+2 added
3 unmodified lines4 -------------5 6 Unreleased7 8+- ``get_parameter_source()`` is available during eager callbacks and type9+ conversion again. :issue:`3458`10 - Zsh completion scripts parse correctly on Windows. :issue:`3277`11 - Shell completion of `Choice` `Enum` values produces a valid completion12 result. :issue:`3015`13 1466 unmodified lines