Skip to content

Commit 1cb8609

Browse files
authored
Better error message for bad parameter default (pallets#1805)
raise TypeError when multiple parameter has single default
1 parent 4c7399e commit 1cb8609

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ Unreleased
182182
previous item. :issue:`1353`
183183
- The ``Path`` param type can be passed ``path_type=pathlib.Path`` to
184184
return a path object instead of a string. :issue:`405`
185+
- ``TypeError`` is raised when parameter with ``multiple=True`` or
186+
``nargs > 1`` has non-iterable default. :issue:`1749`
185187

186188

187189
Version 7.1.2

src/click/core.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2011,7 +2011,15 @@ def _convert(value, level):
20112011
if level == 0:
20122012
return self.type(value, self, ctx)
20132013

2014-
return tuple(_convert(x, level - 1) for x in value)
2014+
try:
2015+
iter_value = iter(value)
2016+
except TypeError:
2017+
raise TypeError(
2018+
"Value for parameter with multiple = True or nargs > 1"
2019+
" should be an iterable."
2020+
)
2021+
2022+
return tuple(_convert(x, level - 1) for x in iter_value)
20152023

20162024
return _convert(value, (self.nargs != 1) + bool(self.multiple))
20172025

tests/test_options.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ def cli(message):
117117
assert "Error: Missing option '-m' / '--message'." in result.output
118118

119119

120+
def test_multiple_bad_default(runner):
121+
@click.command()
122+
@click.option("--flags", multiple=True, default=False)
123+
def cli(flags):
124+
pass
125+
126+
result = runner.invoke(cli, [])
127+
assert result.exception
128+
assert (
129+
"Value for parameter with multiple = True or nargs > 1 should be an iterable."
130+
in result.exception.args
131+
)
132+
133+
120134
def test_empty_envvar(runner):
121135
@click.command()
122136
@click.option("--mypath", type=click.Path(exists=True), envvar="MYPATH")

0 commit comments

Comments
 (0)