Skip to content
Myr

🐛 Ensure that the default of a list argument is used correctly (#1821)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sofie Van Landeghem · committed by GitHub · parent 7f888b2 · Browse files · 2 files+49 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>

Diff stats

2 files+49 added−5 removed

  • tests:+46 added−2 removed
  • other:+3 added−3 removed

2 files+49 added−5 removed

modified:tests/test_types.py+46 added-2 removed
29 unmodified lines
30 print(f"Hello {name.value}!")313233@app.command()34def hello_all(names: list[str] = typer.Argument(["World"], envvar="NAMES")) -> None:34def hello_all_options(name: list[str] = typer.Option(["World"])) -> None:35 for n in name:36 print(f"Hello {n}!")373839@app.command()40def hello_all_args(names: list[str] = typer.Argument(["World"])) -> None:41 for name in names:42 print(f"Hello {name}!")434445@app.command()46def hello_all_envvar(47 names: list[str] = typer.Argument(["World"], envvar="NAMES"),48) -> None:49 for name in names:50 print(f"Hello {name}!")515253@app.command()54def split_variadic_and_pair(items: list[str], pair: tuple[str, str]) -> None:55 print(f"items={items}")56 print(f"pair={pair}")57
55 unmodified lines
113 assert "rick" in result.output114 assert "morty" in result.output115116117def test_list() -> None:118 result = runner.invoke(119 app, ["hello-all-options", "--name", "Rick", "--name", "Morty"]120 )121 assert result.exit_code == 0122 assert "Hello World!" not in result.output123 assert "Hello Rick!" in result.output124 assert "Hello Morty!" in result.output125126 result = runner.invoke(app, ["hello-all-args", "Rick", "Morty"])127 assert result.exit_code == 0128 assert "Hello World!" not in result.output129 assert "Hello Rick!" in result.output130 assert "Hello Morty!" in result.output131132133def test_list_empty() -> None:134 result = runner.invoke(app, ["hello-all-args"])135 assert result.exit_code == 0136 assert "Hello World!" in result.output137138 result = runner.invoke(app, ["hello-all-options"])139 assert result.exit_code == 0140 assert "Hello World!" in result.output141142 result = runner.invoke(app, ["hello-all-envvar"])143 assert result.exit_code == 0144 assert "Hello World!" in result.output145146147def test_split_envvar_value(monkeypatch) -> None:148 # This will use split_envvar_value to produce two strings from the envvar149 monkeypatch.setenv("NAMES", "Rick Morty")106 result = runner.invoke(app, ["hello-all"])150 result = runner.invoke(app, ["hello-all-envvar"])151 assert result.exit_code == 0152 assert "Hello Rick!" in result.output153 assert "Hello Morty!" in result.output154
11 unmodified lines
modified:typer/_click/parser.py+3 added-3 removed
185 unmodified lines
186 raise BadArgumentUsage(187 f"Argument {self.dest!r} takes {self.nargs} values."188 )189190 if self.nargs == -1 and self.obj.envvar is not None and value == ():191 # Replace empty tuple with None so that a value from the192 # environment may be tried.190 if self.nargs == -1 and value == ():191 # Replace empty tuple with None so regular default resolution192 # (env var, default map, and parameter default) can be tried.193 value = None194195 state.opts[self.dest] = value # type: ignore196 state.order.append(self.obj)
263 unmodified lines