Skip to content
Myr

Merge branch 'stable' into hide-unset-in-lookup_default

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

authored by Rowlando13 · committed by GitHub · parents aaf99a3, fa7f035 · Browse files · 4 files+88 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+88 added−5 removed

  • tests:+88 added−5 removed

4 files+88 added−5 removed

modified:tests/test_arguments.py+11 added
397 unmodified lines
398 [{"nargs": -1, "default": {1, 2, 3, 4, 5}}, [], (1, 2, 3, 4, 5)],399 ],400)401def test_good_defaults_for_nargs(runner, argument_params, args, expected):402 """Comprehensive check of default-value processing for arguments with403 ``nargs``.404405 .. hint::406 An option-specific equivalent is available in407 ``test_options.py::test_good_defaults_for_multiple``.408409 A smoke test covering a single basic case is in410 ``test_defaults.py::test_nargs_plus_multiple``.411 """412413 @click.command()414 @click.argument("a", type=int, **argument_params)415 def cmd(a):416 click.echo(repr(a), nl=False)
213 unmodified lines
modified:tests/test_basic.py+6 added
314 unmodified lines
315 """Check how default is processed when options compete for the same variable name.316317 Covers the regression reported in318 https://github.com/pallets/click/issues/3024#issuecomment-3146199461319320 .. hint::321 Similar to ``test_options.py::test_default_dual_option_callback``.322323 ``test_defaults.py::test_shared_param_prefers_first_default``324 is a smoke-test complement that exercises both default placements.325 """326327 @click.command()328 @click.option("--upper", "case", flag_value="upper", default=default)
413 unmodified lines
modified:tests/test_defaults.py+53 added-5 removed
1 unmodified line
23import click456def test_basic_defaults(runner):6@pytest.mark.parametrize(7 ("default", "type", "expected_output", "expected_type"),8 [9 (42, click.FLOAT, "42.0", float),10 ("42", click.INT, "42", int),11 (1.5, click.STRING, "1.5", str),12 ("1.5", click.FLOAT, "1.5", float),13 ("true", click.BOOL, "True", bool),14 ("0", click.BOOL, "False", bool),15 ],16)17def test_basic_defaults(runner, default, type, expected_output, expected_type):18 """Smoke test: a single option's default is type-coerced.1920 This covers basic single-option default type coercion.21 """2223 @click.command()8 @click.option("--foo", default=42, type=click.FLOAT)24 @click.option("--foo", default=default, type=type)25 def cli(foo):10 assert isinstance(foo, float)26 assert isinstance(foo, expected_type)27 click.echo(f"FOO:[{foo}]")2829 result = runner.invoke(cli, [])30 assert not result.exception15 assert "FOO:[42.0]" in result.output31 assert f"FOO:[{expected_output}]" in result.output323334def test_multiple_defaults(runner):35 """Smoke test: each element in a multiple-option default is type-coerced.3637 .. hint::38 ``test_options.py::test_good_defaults_for_multiple``39 covers the structural default processing (``list`` to40 ``tuple``, various ``nargs``) exhaustively.4142 This test fills the gap of explicit43 ``type=click.FLOAT`` coercion on the elements.44 """4546 @click.command()47 @click.option("--foo", default=[23, 42], type=click.FLOAT, multiple=True)48 def cli(foo):49 for item in foo:
5 unmodified lines
55 assert result.output.splitlines() == ["23.0", "42.0"]565758def test_nargs_plus_multiple(runner):59 """Smoke test: option with ``nargs=2`` + ``multiple=True`` and a60 tuple-of-tuples default.6162 .. hint::63 ``test_options.py::test_good_defaults_for_multiple``64 expands this with many more edge cases with various65 ``nargs``/``multiple``/``default`` combinations.6667 An argument-specific equivalent is in68 ``test_arguments.py::test_good_defaults_for_nargs``.69 """7071 @click.command()72 @click.option(73 "--arg", default=((1, 2), (3, 4)), nargs=2, multiple=True, type=click.INT74 )
52 unmodified lines
127 assert "default: name" in result.output128129130def test_shared_param_prefers_first_default(runner):92 """test that the first default is chosen when multiple flags share a param name"""131 """The first ``default=True`` wins when multiple ``flag_value`` options share132 a parameter name, regardless of which positional option carries it.133134 .. hint::135 ``test_basic.py::test_flag_value_dual_options`` and136 ``test_options.py::test_default_dual_option_callback`` are wider137 parametrized sibling tests covering many more default-value types (``None``,138 ``UNSET``, strings, numbers) but always place the default on the first139 option. This test complements them by exercising both placements.140 """141142 @click.command143 @click.option("--red", "color", flag_value="red")144 @click.option("--green", "color", flag_value="green", default=True)145 def prefers_green(color):
166 unmodified lines
modified:tests/test_options.py+18 added
221 unmodified lines
222 (False, 2, [1, 2], (1, 2)),223 ],224)225def test_good_defaults_for_multiple(runner, multiple, nargs, default, expected):226 """Comprehensive check of default-value processing for options with227 ``multiple=True`` and/or ``nargs > 1``.228229 .. hint::230 An argument-specific equivalent is in231 ``test_arguments.py::test_good_defaults_for_nargs``.232233 Smoke tests are in ``test_defaults.py``:234 ``test_multiple_defaults`` (explicit ``type=FLOAT``)235 and ``test_nargs_plus_multiple`` (``nargs=2``).236 """237238 @click.command()239 @click.option("-a", multiple=multiple, nargs=nargs, default=default)240 def cmd(a):241 click.echo(repr(a), nl=False)
1302 unmodified lines
1544 variable name.15451546 Reproduction of the issue reported in1547 https://github.com/pallets/click/pull/3030#discussion_r227157181915481549 .. hint::1550 Similar to ``test_basic.py::test_flag_value_dual_options``.15511552 ``test_defaults.py::test_shared_param_prefers_first_default``1553 is a smoke-test complement that exercises both default placements.1554 """15551556 def _my_func(ctx, param, value):1557 # Print the value received by the callback as-is, so we can check for it.
782 unmodified lines