Skip to content
Myr

Split string values from `default_map` for multi-value parameters (#3364)

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

authored by Rowlando13 · committed by GitHub · parents 8a2b489, a014796 · Browse files · 5 files+75 added−0 removed
This commit

State

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

Diff stats

5 files+75 added−0 removed

  • tests:+37 added−0 removed
  • other:+38 added−0 removed

5 files+75 added−0 removed

modified:docs/commands.md+29 added
349 unmodified lines
350 }351 })352```353354### Multi-value parameters355356When a `default_map` value is a string for a parameter with `nargs > 1` or a357{class}`Tuple` type, the string is split automatically, the same way an358environment variable would be. By default, values are split on whitespace. See359[Multiple Options from Environment360Values](options.md#multiple-options-from-environment-values) for details on361splitting behavior.362363```python364default_map = {365 "draw": {366 "point": "3 4", # split into ("3", "4") for nargs=2367 "color": "red", # passed as-is for nargs=1368 }369}370```371372You can also pass an already-structured tuple or list, which will be used as-is373without splitting:374375```python376default_map = {377 "draw": {378 "point": (3, 4), # used directly379 }380}381```382383## Context Defaults384385```{versionadded} 2.0386```
81 unmodified lines
modified:docs/conf.py+1 added
29 unmodified lines
30}31intersphinx_mapping = {32 "python": ("https://docs.python.org/3/", None),33}34myst_heading_anchors = 33536# HTML -----------------------------------------------------------------3738html_theme = "click"
20 unmodified lines
modified:src/click/core.py+5 added
2354 unmodified lines
2355 if default_map_value is not None or ctx._default_map_has(self.name):2356 value = default_map_value2357 source = ParameterSource.DEFAULT_MAP23582359 # A string from default_map must be split for multi-value2360 # parameters, matching value_from_envvar behavior.2361 if isinstance(value, str) and self.nargs != 1:2362 value = self.type.split_envvar_value(value)23632364 if value is UNSET:2365 default_value = self.get_default(ctx)2366 if default_value is not UNSET:2367 value = default_value
1114 unmodified lines
modified:tests/test_defaults.py+37 added
355 unmodified lines
356 assert result.exit_code == 0357 assert result.output == repr(expected)358359360@pytest.mark.parametrize(361 ("default_map", "option_kwargs", "cli_args", "expected"),362 [363 # String is split for nargs=2 option.364 ({"point": "3 4"}, {"nargs": 2, "type": int}, [], (3, 4)),365 # String is split for explicit Tuple type.366 ({"point": "hello world"}, {"type": (str, str)}, [], ("hello", "world")),367 # Already-structured tuple passes through unchanged.368 ({"point": ("a", "b")}, {"nargs": 2}, [], ("a", "b")),369 # Already-structured list passes through unchanged.370 ({"point": [5, 6]}, {"nargs": 2, "type": int}, [], (5, 6)),371 # CLI args override default_map for nargs > 1.372 (373 {"point": "3 4"},374 {"nargs": 2, "type": int},375 ["--point", "10", "20"],376 (10, 20),377 ),378 ],379)380def test_default_map_nargs(runner, default_map, option_kwargs, cli_args, expected):381 """A string in ``default_map`` for an option with ``nargs > 1`` should be382 split the same way an environment variable string is split.383384 Regression test for https://github.com/pallets/click/issues/2745.385 """386387 @click.command()388 @click.option("--point", **option_kwargs)389 def cli(point):390 click.echo(repr(point))391392 result = runner.invoke(cli, cli_args, default_map=default_map)393 assert result.exit_code == 0394 assert result.output.strip() == repr(expected)395396397def test_unset_in_default_map(runner):398 """An ``UNSET`` value in ``default_map`` should be treated as if399 the key is absent, and so fallback to the parameter's own default.400
13 unmodified lines
modified:CHANGES.rst+3 added
15 unmodified lines
16 :class:`~typing.TypedDict` subclasses instead of17 ``dict[str, Any]``.18 - :class:`CompositeParamType` and the number-range base are now19 generic with abstract methods.20- Split string values from ``default_map`` for parameters with ``nargs > 1``21 or :class:`Tuple` type, matching environment variable behavior.22 :issue:`2745` :pr:`3364`2324Version 8.3.325-------------26
1364 unmodified lines