correct interaction of default=tuple with multiple=True
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent b631e5a · Browse files · 3 files·+66 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+66 added−26 removed
- tests:+30 added−14 removed
- other:+36 added−12 removed
Provenance
- Commit
94cc293- Tree
c528c1d- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
b631e5a
3 files changed+66 added−26 removed
modified:src/click/core.py+5 added-1 removed
1986 unmodified lines1987 # For bounded nargs (!= -1), validate the number of values.1988 if (1989 not ctx.resilient_parsing1990 and self.nargs > 11991- and self.nargs != len(value)1991 and isinstance(value, (tuple, list))1992+ and (1993+ any(len(v) != self.nargs for v in value)1994+ if self.multiple1995+ else len(value) != self.nargs1996+ )1997 ):1998 were = "was" if len(value) == 1 else "were"1999 ctx.fail(2000 f"Argument {self.name!r} takes {self.nargs} values but"497 unmodified linesmodified:src/click/types.py+31 added-11 removed
816 unmodified lines817 return tuple(ty(x, param, ctx) for ty, x in zip(self.types, value))818 819 820 def convert_type(ty, default=None):821- """Converts a callable or python type into the most appropriate822- param type.821+ """Find the most appropriate :class:`ParamType` for the given Python822+ type. If the type isn't provided, it can be inferred from a default823+ value.824 """825 guessed_type = False826+827 if ty is None and default is not None:826- if isinstance(default, tuple):827- ty = tuple(map(type, default))828+ if isinstance(default, (tuple, list)):829+ # If the default is empty, ty will remain None and will830+ # return STRING.831+ if default:832+ item = default[0]833+834+ # A tuple of tuples needs to detect the inner types.835+ # Can't call convert recursively because that would836+ # incorrectly unwind the tuple to a single type.837+ if isinstance(item, (tuple, list)):838+ ty = tuple(map(type, item))839+ else:840+ ty = type(item)841 else:842 ty = type(default)843+844 guessed_type = True845 846 if isinstance(ty, tuple):847 return Tuple(ty)848+849 if isinstance(ty, ParamType):850 return ty851+852 if ty is str or ty is None:853 return STRING854+855 if ty is int:856 return INT840- # Booleans are only okay if not guessed. This is done because for841- # flags the default value is actually a bit of a lie in that it842- # indicates which of the flags is the one we want. See get_default()843- # for more information.844- if ty is bool and not guessed_type:845- return BOOL857+858 if ty is float:859 return FLOAT860+861+ # Booleans are only okay if not guessed. For is_flag options with862+ # flag_value, default=True indicates which flag_value is the863+ # default.864+ if ty is bool and not guessed_type:865+ return BOOL866+867 if guessed_type:868 return STRING869 851- # Catch a common mistake870 if __debug__:871 try:872 if issubclass(ty, ParamType):873 raise AssertionError(874 f"Attempted to use an uninstantiated parameter type ({ty})."875 )876 except TypeError:877+ # ty is an instance (correct), so issubclass fails.878 pass879+880 return FuncParamType(ty)881 882 883 #: A dummy parameter type that just does nothing. From a user's30 unmodified linesmodified:tests/test_options.py+30 added-14 removed
186 unmodified lines187 assert "foo, bar" in result.output188 assert "1, 2" in result.output189 190 191-def test_multiple_default_type(runner):192- @click.command()193- @click.option("--arg1", multiple=True, default=("foo", "bar"))194- @click.option("--arg2", multiple=True, default=(1, "a"))195- def cmd(arg1, arg2):196- assert all(isinstance(e[0], str) for e in arg1)197- assert all(isinstance(e[1], str) for e in arg1)191+def test_multiple_default_type():192+ opt = click.Option(["-a"], multiple=True, default=(1, 2))193+ assert opt.nargs == 1194+ assert opt.multiple195+ assert opt.type is click.INT196+ ctx = click.Context(click.Command("test"))197+ assert opt.get_default(ctx) == (1, 2)198 199- assert all(isinstance(e[0], int) for e in arg2)200- assert all(isinstance(e[1], str) for e in arg2)199 202- result = runner.invoke(203- cmd, "--arg1 a b --arg1 test 1 --arg2 2 two --arg2 4 four".split()204- )205- assert not result.exception200+def test_multiple_default_composite_type():201+ opt = click.Option(["-a"], multiple=True, default=[(1, "a")])202+ assert opt.nargs == 2203+ assert opt.multiple204+ assert isinstance(opt.type, click.Tuple)205+ assert opt.type.types == [click.INT, click.STRING]206+ ctx = click.Context(click.Command("test"))207+ assert opt.get_default(ctx) == ((1, "a"),)208 209 210+def test_parse_multiple_default_composite_type(runner):211+ @click.command()212+ @click.option("-a", multiple=True, default=("a", "b"))213+ @click.option("-b", multiple=True, default=[(1, "a")])214+ def cmd(a, b):215+ click.echo(a)216+ click.echo(b)217+218+ # result = runner.invoke(cmd, "-a c -a 1 -a d -b 2 two -b 4 four".split())219+ # assert result.output == "('c', '1', 'd')\n((2, 'two'), (4, 'four'))\n"220+ result = runner.invoke(cmd)221+ assert result.output == "('a', 'b')\n((1, 'a'),)\n"222+223+224 def test_dynamic_default_help_unset(runner):225 @click.command()226 @click.option(227 "--username",38 unmodified lines266 (click.IntRange(max=32), "x<=32"),267 ],268 )269 def test_intrange_default_help_text(runner, type, expect):254- option = click.Option(["--count"], type=type, show_default=True, default=1)270+ option = click.Option(["--count"], type=type, show_default=True, default=2)271 context = click.Context(click.Command("test"))272 result = option.get_help_record(context)[1]273 assert expect in result274 374 unmodified lines