handle default=None for nargs and multiple
Before adoption: this commit came in with the history the project adopted, so no landing records it.
If a default isn't given, return () for multiple or nargs=-1, or return None for nargs > 1.
authored by David Lord · committed by David Lord · parent e79c2b4 · Browse files · 4 files·+68 added−15 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+68 added−15 removed
- tests:+39 added−12 removed
- other:+29 added−3 removed
Provenance
- Commit
25a8879- Tree
88a64b0- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
e79c2b4
4 files changed+68 added−15 removed
modified:src/click/core.py+25 added-3 removed
140 unmodified lines141 source for a parameter by name.142 143 .. versionchanged:: 8.0144 Use :class:`~enum.Enum` and drop the ``validate`` method.145+146+ .. versionchanged:: 8.0147+ Added the ``PROMPT`` value.148 """149 150 COMMANDLINE = enum.auto()151 """The value was provided by the command line args."""585 unmodified lines737 value was actually taken from the default.738 739 :param name: The name of the parameter.740 :rtype: ParameterSource741+742+ .. versionchanged:: 8.0743+ Returns ``None`` if the parameter was not provided from any744+ source.745 """746 return self._parameter_source.get(name)747 748 1038 unmodified lines1787 semantics described above. The old name is deprecated and will1788 be removed in 8.1, until then it will be wrapped to match the1789 new requirements.1790 1791+ .. versionchanged:: 8.01792+ For ``multiple=True, nargs>1``, the default must be a list of1793+ tuples.1794+1795+ .. versionchanged:: 8.01796+ Setting a default is no longer required for ``nargs>1``, it will1797+ default to ``None``. ``multiple=True`` or ``nargs=-1`` will1798+ default to ``()``.1799+1800 .. versionchanged:: 7.11801 Empty environment variables are ignored rather than taking the1802 empty string value. This makes it possible for scripts to clear1803 variables if they can't unset them.150 unmodified lines1954 """Given a value this runs it properly through the type system.1955 This automatically handles things like `nargs` and `multiple` as1956 well as composite types.1957 """1958+ if value is None:1959+ return () if self.multiple or self.nargs == -1 else None1960+1961 if self.type.is_composite:1962 if self.nargs <= 1:1963 raise TypeError(1964 "Attempted to invoke composite type but nargs has"1965 f" been set to {self.nargs}. This is not supported;"1966 " nargs needs to be set to a fixed value > 1."1967 )1968+1969 if self.multiple:1950- return tuple(self.type(x or (), self, ctx) for x in value or ())1951- return self.type(value or (), self, ctx)1970+ return tuple(self.type(x, self, ctx) for x in value)1971 1972+ return self.type(value, self, ctx)1973+1974 def _convert(value, level):1975 if level == 0:1976 return self.type(value, self, ctx)1956- return tuple(_convert(x, level - 1) for x in value or ())1977 1978+ return tuple(_convert(x, level - 1) for x in value)1979+1980 return _convert(value, (self.nargs != 1) + bool(self.multiple))1981 1982 def process_value(self, ctx, value):1983 """Given a value and context this runs the logic to convert the539 unmodified linesmodified:tests/test_arguments.py+15 added-12 removed
162 unmodified lines163 assert result.output == "Foo bar baz\n"164 165 166 @pytest.mark.parametrize(167- ("nargs", "value", "code", "output"),167+ ("nargs", "value", "expect"),168 [169- (2, "", 2, "Argument 'arg' takes 2 values but 0 were given."),170- (2, "a", 2, "Argument 'arg' takes 2 values but 1 was given."),171- (2, "a b", 0, "len 2"),172- (2, "a b c", 2, "Argument 'arg' takes 2 values but 3 were given."),173- (-1, "a b c", 0, "len 3"),174- (-1, "", 0, "len 0"),169+ (2, "", None),170+ (2, "a", "Argument 'arg' takes 2 values but 1 was given."),171+ (2, "a b", ("a", "b")),172+ (2, "a b c", "Argument 'arg' takes 2 values but 3 were given."),173+ (-1, "a b c", ("a", "b", "c")),174+ (-1, "", ()),175 ],176 )177-def test_nargs_envvar(runner, nargs, value, code, output):177+def test_nargs_envvar(runner, nargs, value, expect):178 if nargs == -1:179 param = click.argument("arg", envvar="X", nargs=nargs)180 else:181 param = click.option("--arg", envvar="X", nargs=nargs)182 183 @click.command()184 @param185 def cmd(arg):186- click.echo(f"len {len(arg)}")186+ return arg187 188- result = runner.invoke(cmd, env={"X": value})189- assert result.exit_code == code190- assert output in result.output188+ result = runner.invoke(cmd, env={"X": value}, standalone_mode=False)189 190+ if isinstance(expect, str):191+ assert expect in str(result.exception)192+ else:193+ assert result.return_value == expect194 195+196 def test_empty_nargs(runner):197 @click.command()198 @click.argument("arg", nargs=-1)199 def cmd(arg):175 unmodified linesmodified:tests/test_types.py+24 added
52 unmodified lines53 sneaky.clamp = True54 55 with pytest.raises(RuntimeError):56 sneaky.convert("1.5", None, None)57+58+59+@pytest.mark.parametrize(60+ ("nargs", "multiple", "default", "expect"),61+ [62+ (2, False, None, None),63+ (2, False, (None, None), (None, None)),64+ (None, True, None, ()),65+ (None, True, (None, None), (None, None)),66+ (2, True, None, ()),67+ (2, True, [(None, None)], ((None, None),)),68+ (-1, None, None, ()),69+ ],70+)71+def test_cast_multi_default(runner, nargs, multiple, default, expect):72+ if nargs == -1:73+ param = click.Argument(["a"], nargs=nargs, default=default)74+ else:75+ param = click.Option(["-a"], nargs=nargs, multiple=multiple, default=default)76+77+ cli = click.Command("cli", params=[param], callback=lambda a: a)78+ result = runner.invoke(cli, standalone_mode=False)79+ assert result.exception is None80+ assert result.return_value == expectmodified:CHANGES.rst+4 added
125 unmodified lines126 - For shell completion, an argument is considered incomplete if its127 value did not come from the command line args. :issue:`1649`128 - Added ``ParameterSource.PROMPT`` to track parameter values that were129 prompted for. :issue:`1649`130+- Options with ``nargs`` > 1 no longer raise an error if a default is131+ not given. Parameters with ``nargs`` > 1 default to ``None``, and132+ parameters with ``multiple=True`` or ``nargs=-1`` default to an133+ empty tuple. :issue:`472`134 135 136 Version 7.1.2137 -------------635 unmodified lines