allow `multiple=True` with flags (#2550)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by GitHub · parents d34d31f, b36e3ed · Browse files · 4 files·+29 added−8 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+29 added−8 removed
- tests:+21 added−4 removed
- other:+8 added−4 removed
Provenance
- Commit
c77966e- Tree
07cd203- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
d34d31f- Parent 2
b36e3ed
4 files changed+29 added−8 removed
modified:src/click/core.py+6 added-4 removed
2568 unmodified lines2569 # Not a flag, and prompt is not enabled, can be used as a2570 # flag if flag_value is set.2571 self._flag_needs_value = flag_value is not None2572 2573+ self.default: t.Union[t.Any, t.Callable[[], t.Any]]2574+2575 if is_flag and default_is_missing and not self.required:2574- self.default: t.Union[t.Any, t.Callable[[], t.Any]] = False2576+ if multiple:2577+ self.default = ()2578+ else:2579+ self.default = False2580 2581 if flag_value is None:2582 flag_value = not self.default2583 42 unmodified lines2626 2627 if self.is_flag:2628 raise TypeError("'count' is not valid with 'is_flag'.")2629 2625- if self.multiple and self.is_flag:2626- raise TypeError("'multiple' is not valid with 'is_flag', use 'count'.")2627-2630 def to_info_dict(self) -> t.Dict[str, t.Any]:2631 info_dict = super().to_info_dict()2632 info_dict.update(2633 help=self.help,409 unmodified linesmodified:tests/test_defaults.py+21 added
36 unmodified lines37 38 result = runner.invoke(cli, [])39 assert not result.exception40 assert result.output.splitlines() == ["<1|2>", "<3|4>"]41+42+43+def test_multiple_flag_default(runner):44+ """Default default for flags when multiple=True should be empty tuple."""45+46+ @click.command47+ # flag due to secondary token48+ @click.option("-y/-n", multiple=True)49+ # flag due to is_flag50+ @click.option("-f", is_flag=True, multiple=True)51+ # flag due to flag_value52+ @click.option("-v", "v", flag_value=1, multiple=True)53+ @click.option("-q", "v", flag_value=-1, multiple=True)54+ def cli(y, f, v):55+ return y, f, v56+57+ result = runner.invoke(cli, standalone_mode=False)58+ assert result.return_value == ((), (), ())59+60+ result = runner.invoke(cli, ["-y", "-n", "-f", "-v", "-q"], standalone_mode=False)61+ assert result.return_value == ((True, False), (True,), (1, -1))modified:tests/test_options.py-4 removed
909 unmodified lines910 ("kwargs", "message"),911 [912 ({"count": True, "multiple": True}, "'count' is not valid with 'multiple'."),913 ({"count": True, "is_flag": True}, "'count' is not valid with 'is_flag'."),914- (915- {"multiple": True, "is_flag": True},916- "'multiple' is not valid with 'is_flag', use 'count'.",917- ),914 ],915 )916 def test_invalid_flag_combinations(runner, kwargs, message):917 with pytest.raises(TypeError) as e:3 unmodified linesmodified:CHANGES.rst+2 added
20 unmodified lines21 - ``EOFError`` and ``KeyboardInterrupt`` tracebacks are not suppressed when22 ``standalone_mode`` is disabled. :issue:`2380`23 - ``@group.command`` does not fail if the group was created with a custom24 ``command_class``. :issue:`2416`25+- ``multiple=True`` is allowed for flag options again and does not require26+ setting ``default=()``. :issue:`2246, 2292, 2295`27 28 29 Version 8.1.330 -------------1040 unmodified lines