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
Provenance
- Commit
c8da1fc- Tree
fa27d23- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
8a2b489- Parent 2
a014796
5 files changed+75 added−0 removed
modified:docs/commands.md+29 added
349 unmodified lines350 }351 })352 ```353 354+### Multi-value parameters355+356+When 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 an358+environment variable would be. By default, values are split on whitespace. See359+[Multiple Options from Environment360+Values](options.md#multiple-options-from-environment-values) for details on361+splitting behavior.362+363+```python364+default_map = {365+ "draw": {366+ "point": "3 4", # split into ("3", "4") for nargs=2367+ "color": "red", # passed as-is for nargs=1368+ }369+}370+```371+372+You can also pass an already-structured tuple or list, which will be used as-is373+without splitting:374+375+```python376+default_map = {377+ "draw": {378+ "point": (3, 4), # used directly379+ }380+}381+```382+383 ## Context Defaults384 385 ```{versionadded} 2.0386 ```81 unmodified linesmodified:docs/conf.py+1 added
29 unmodified lines30 }31 intersphinx_mapping = {32 "python": ("https://docs.python.org/3/", None),33 }34+myst_heading_anchors = 335 36 # HTML -----------------------------------------------------------------37 38 html_theme = "click"20 unmodified linesmodified:src/click/core.py+5 added
2354 unmodified lines2355 if default_map_value is not None or ctx._default_map_has(self.name):2356 value = default_map_value2357 source = ParameterSource.DEFAULT_MAP2358 2359+ # 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)2363+2364 if value is UNSET:2365 default_value = self.get_default(ctx)2366 if default_value is not UNSET:2367 value = default_value1114 unmodified linesmodified:tests/test_defaults.py+37 added
355 unmodified lines356 assert result.exit_code == 0357 assert result.output == repr(expected)358 359 360+@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+)380+def 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.383+384+ Regression test for https://github.com/pallets/click/issues/2745.385+ """386+387+ @click.command()388+ @click.option("--point", **option_kwargs)389+ def cli(point):390+ click.echo(repr(point))391+392+ result = runner.invoke(cli, cli_args, default_map=default_map)393+ assert result.exit_code == 0394+ assert result.output.strip() == repr(expected)395+396+397 def 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 linesmodified:CHANGES.rst+3 added
15 unmodified lines16 :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`23 24 Version 8.3.325 -------------26 1364 unmodified lines