Document ctx.params bypass with test and doc
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 7d05a59 · Browse files · 4 files·+44 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+44 added−5 removed
- tests:+27 added−0 removed
- other:+17 added−5 removed
Provenance
- Commit
0baa8db- Tree
e8b639b- Author
Kevin Deldycke <kevin@deldycke.com>- Committer
Kevin Deldycke <kevin@deldycke.com>- Parent
7d05a59
4 files changed+44 added−5 removed
modified:docs/advanced.md+12 added-2 removed
142 unmodified lines143 if fp is not None:144 click.echo(f"{url}: {fp.code}")145 146 In this case the callback returns the URL unchanged but also passes a147-second `fp` value to the callback. What's more recommended is to pass148-the information in a wrapper, however:147+second `fp` value to the callback.148 149+.. caution::150+151+ Writing to :attr:`~click.Context.params` directly bypasses Click's152+ per-parameter pipeline: :meth:`~click.Context.get_parameter_source` returns153+ ``None`` for it, the value is never run through any :class:`~click.ParamType`154+ conversion. And if the key collides with a declared parameter, then the155+ shared-name arbitration loses information about where the value came from.156+ Prefer the wrapper pattern below when any of those semantics matter.157+158+What's more recommended is to pass the information in a wrapper, however:159+160 .. click:example::161 162 import urllib163 262 unmodified linesmodified:src/click/core.py+4 added-1 removed
2663 unmodified lines2664 ctx._param_default_explicit[self.name] = self._default_explicit2665 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.2668+ # else: ctx.params[self.name] was populated by code that bypassed2669+ # handle_parse_result (from another option's callback for example). Keep2670+ # the provisional source recorded before process_value so downstream2671+ # lookups don't return ``None``.2672 2673 return value, args2674 2675 def get_help_record(self, ctx: Context) -> tuple[str, str] | None:867 unmodified linesmodified:tests/test_defaults.py+27 added
386 unmodified lines387 assert result.exit_code == 0388 assert result.output.strip() == "APP_DEBUG=0"389 390 391+def test_parameter_source_on_parse_result_bypass(runner):392+ """A losing option keeps its provisional source when ``ctx.params[name]``393+ is populated by code that bypassed ``handle_parse_result``.394+395+ This replicate the pattern documented in the "Parameter Modifications" section396+ of ``docs/advanced.md``. This test highlight the current behavior of397+ ``get_parameter_source()`` but is not intended as a contract enforcement.398+ """399+400+ def hijack(ctx, param, value):401+ ctx.params["target"] = "hijacked"402+ return value403+404+ @click.command()405+ @click.option("--hijacker", is_eager=True, callback=hijack, expose_value=False)406+ @click.option("--target", default="default_value")407+ @click.pass_context408+ def cli(ctx, target):409+ source = ctx.get_parameter_source("target")410+ click.echo(f"value={target} source={source.name if source else 'None'}")411+412+ result = runner.invoke(cli, ["--hijacker", "anything"])413+ assert result.exit_code == 0, result.output414+ assert "value=hijacked" in result.output415+ assert "source=DEFAULT" in result.output416+417+418 def test_lookup_default_override_respected(runner):419 """A subclass override of ``lookup_default()`` should be called by Click420 internals, not bypassed by a private method.421 142 unmodified linesmodified:CHANGES.rst+1 added-2 removed
4 unmodified lines5 6 Unreleased7 8 - ``get_parameter_source()`` is available during eager callbacks and type9- conversion again. :issue:`3458`9+ conversion again. :issue:`3458` :issue:`3484`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 14-14 Version 8.4.015 -------------16 17 Released 2026-05-171461 unmodified lines