Skip to content
Myr

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

4 files+68 added−15 removed

modified:src/click/core.py+25 added-3 removed
140 unmodified lines
141 source for a parameter by name.142143 .. versionchanged:: 8.0144 Use :class:`~enum.Enum` and drop the ``validate`` method.145146 .. versionchanged:: 8.0147 Added the ``PROMPT`` value.148 """149150 COMMANDLINE = enum.auto()151 """The value was provided by the command line args."""
585 unmodified lines
737 value was actually taken from the default.738739 :param name: The name of the parameter.740 :rtype: ParameterSource741742 .. versionchanged:: 8.0743 Returns ``None`` if the parameter was not provided from any744 source.745 """746 return self._parameter_source.get(name)747748
1038 unmodified lines
1787 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.17901791 .. versionchanged:: 8.01792 For ``multiple=True, nargs>1``, the default must be a list of1793 tuples.17941795 .. 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 ``()``.17991800 .. 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 lines
1954 """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 None19601961 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 )19681969 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)19711972 return self.type(value, self, ctx)19731974 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 ())19771978 return tuple(_convert(x, level - 1) for x in value)19791980 return _convert(value, (self.nargs != 1) + bool(self.multiple))19811982 def process_value(self, ctx, value):1983 """Given a value and context this runs the logic to convert the
539 unmodified lines
modified:tests/test_arguments.py+15 added-12 removed
162 unmodified lines
163 assert result.output == "Foo bar baz\n"164165166@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)177def test_nargs_envvar(runner, nargs, value, code, output):177def 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)182183 @click.command()184 @param185 def cmd(arg):186 click.echo(f"len {len(arg)}")186 return arg187188 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)189190 if isinstance(expect, str):191 assert expect in str(result.exception)192 else:193 assert result.return_value == expect194195196def test_empty_nargs(runner):197 @click.command()198 @click.argument("arg", nargs=-1)199 def cmd(arg):
175 unmodified lines
modified:tests/test_types.py+24 added
52 unmodified lines
53 sneaky.clamp = True5455 with pytest.raises(RuntimeError):56 sneaky.convert("1.5", None, None)575859@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)71def 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)7677 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 == expect
modified:CHANGES.rst+4 added
125 unmodified lines
126- 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`134135136Version 7.1.2137-------------
635 unmodified lines