Skip to content
Myr

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

4 files+44 added−5 removed

modified:docs/advanced.md+12 added-2 removed
142 unmodified lines
143 if fp is not None:144 click.echo(f"{url}: {fp.code}")145146In this case the callback returns the URL unchanged but also passes a147second `fp` value to the callback. What's more recommended is to pass148the information in a wrapper, however:147second `fp` value to the callback.148149.. caution::150151 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.157158What's more recommended is to pass the information in a wrapper, however:159160.. click:example::161162 import urllib163
262 unmodified lines
modified:src/click/core.py+4 added-1 removed
2663 unmodified lines
2664 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``.26722673 return value, args26742675 def get_help_record(self, ctx: Context) -> tuple[str, str] | None:
867 unmodified lines
modified:tests/test_defaults.py+27 added
386 unmodified lines
387 assert result.exit_code == 0388 assert result.output.strip() == "APP_DEBUG=0"389390391def 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``.394395 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 """399400 def hijack(ctx, param, value):401 ctx.params["target"] = "hijacked"402 return value403404 @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'}")411412 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.output416417418def 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 lines
modified:CHANGES.rst+1 added-2 removed
4 unmodified lines
56Unreleased78- ``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`131414Version 8.4.015-------------1617Released 2026-05-17
1461 unmodified lines