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
Provenance
- Commit
4538229- Tree
dac0c60- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
aaf99a3- Parent 2
fa7f035
4 files changed+88 added−5 removed
modified:tests/test_arguments.py+11 added
397 unmodified lines398 [{"nargs": -1, "default": {1, 2, 3, 4, 5}}, [], (1, 2, 3, 4, 5)],399 ],400 )401 def test_good_defaults_for_nargs(runner, argument_params, args, expected):402+ """Comprehensive check of default-value processing for arguments with403+ ``nargs``.404+405+ .. hint::406+ An option-specific equivalent is available in407+ ``test_options.py::test_good_defaults_for_multiple``.408+409+ A smoke test covering a single basic case is in410+ ``test_defaults.py::test_nargs_plus_multiple``.411+ """412+413 @click.command()414 @click.argument("a", type=int, **argument_params)415 def cmd(a):416 click.echo(repr(a), nl=False)213 unmodified linesmodified:tests/test_basic.py+6 added
314 unmodified lines315 """Check how default is processed when options compete for the same variable name.316 317 Covers the regression reported in318 https://github.com/pallets/click/issues/3024#issuecomment-3146199461319+320+ .. hint::321+ Similar to ``test_options.py::test_default_dual_option_callback``.322+323+ ``test_defaults.py::test_shared_param_prefers_first_default``324+ is a smoke-test complement that exercises both default placements.325 """326 327 @click.command()328 @click.option("--upper", "case", flag_value="upper", default=default)413 unmodified linesmodified:tests/test_defaults.py+53 added-5 removed
1 unmodified line2 3 import click4 5 6-def 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+)17+def test_basic_defaults(runner, default, type, expected_output, expected_type):18+ """Smoke test: a single option's default is type-coerced.19+20+ This covers basic single-option default type coercion.21+ """22+23 @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}]")28 29 result = runner.invoke(cli, [])30 assert not result.exception15- assert "FOO:[42.0]" in result.output31+ assert f"FOO:[{expected_output}]" in result.output32 33 34 def test_multiple_defaults(runner):35+ """Smoke test: each element in a multiple-option default is type-coerced.36+37+ .. hint::38+ ``test_options.py::test_good_defaults_for_multiple``39+ covers the structural default processing (``list`` to40+ ``tuple``, various ``nargs``) exhaustively.41+42+ This test fills the gap of explicit43+ ``type=click.FLOAT`` coercion on the elements.44+ """45+46 @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 lines55 assert result.output.splitlines() == ["23.0", "42.0"]56 57 58 def test_nargs_plus_multiple(runner):59+ """Smoke test: option with ``nargs=2`` + ``multiple=True`` and a60+ tuple-of-tuples default.61+62+ .. hint::63+ ``test_options.py::test_good_defaults_for_multiple``64+ expands this with many more edge cases with various65+ ``nargs``/``multiple``/``default`` combinations.66+67+ An argument-specific equivalent is in68+ ``test_arguments.py::test_good_defaults_for_nargs``.69+ """70+71 @click.command()72 @click.option(73 "--arg", default=((1, 2), (3, 4)), nargs=2, multiple=True, type=click.INT74 )52 unmodified lines127 assert "default: name" in result.output128 129 130 def 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.133 134+ .. 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+ """141+142 @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 linesmodified:tests/test_options.py+18 added
221 unmodified lines222 (False, 2, [1, 2], (1, 2)),223 ],224 )225 def 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``.228+229+ .. hint::230+ An argument-specific equivalent is in231+ ``test_arguments.py::test_good_defaults_for_nargs``.232+233+ Smoke tests are in ``test_defaults.py``:234+ ``test_multiple_defaults`` (explicit ``type=FLOAT``)235+ and ``test_nargs_plus_multiple`` (``nargs=2``).236+ """237+238 @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 lines1544 variable name.1545 1546 Reproduction of the issue reported in1547 https://github.com/pallets/click/pull/3030#discussion_r22715718191548+1549+ .. hint::1550+ Similar to ``test_basic.py::test_flag_value_dual_options``.1551+1552+ ``test_defaults.py::test_shared_param_prefers_first_default``1553+ is a smoke-test complement that exercises both default placements.1554 """1555 1556 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