diff --git a/tests/test_types.py b/tests/test_types.py index caeef451aa..db6dae08da 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -31,7 +31,21 @@ def hello_no_choices( @app.command() -def hello_all(names: list[str] = typer.Argument(["World"], envvar="NAMES")) -> None: +def hello_all_options(name: list[str] = typer.Option(["World"])) -> None: + for n in name: + print(f"Hello {n}!") + + +@app.command() +def hello_all_args(names: list[str] = typer.Argument(["World"])) -> None: + for name in names: + print(f"Hello {name}!") + + +@app.command() +def hello_all_envvar( + names: list[str] = typer.Argument(["World"], envvar="NAMES"), +) -> None: for name in names: print(f"Hello {name}!") @@ -100,10 +114,40 @@ def test_enum_choice_missing_message() -> None: assert "morty" in result.output +def test_list() -> None: + result = runner.invoke( + app, ["hello-all-options", "--name", "Rick", "--name", "Morty"] + ) + assert result.exit_code == 0 + assert "Hello World!" not in result.output + assert "Hello Rick!" in result.output + assert "Hello Morty!" in result.output + + result = runner.invoke(app, ["hello-all-args", "Rick", "Morty"]) + assert result.exit_code == 0 + assert "Hello World!" not in result.output + assert "Hello Rick!" in result.output + assert "Hello Morty!" in result.output + + +def test_list_empty() -> None: + result = runner.invoke(app, ["hello-all-args"]) + assert result.exit_code == 0 + assert "Hello World!" in result.output + + result = runner.invoke(app, ["hello-all-options"]) + assert result.exit_code == 0 + assert "Hello World!" in result.output + + result = runner.invoke(app, ["hello-all-envvar"]) + assert result.exit_code == 0 + assert "Hello World!" in result.output + + def test_split_envvar_value(monkeypatch) -> None: # This will use split_envvar_value to produce two strings from the envvar monkeypatch.setenv("NAMES", "Rick Morty") - result = runner.invoke(app, ["hello-all"]) + result = runner.invoke(app, ["hello-all-envvar"]) assert result.exit_code == 0 assert "Hello Rick!" in result.output assert "Hello Morty!" in result.output diff --git a/typer/_click/parser.py b/typer/_click/parser.py index 71eb3003cc..3eb5acd632 100644 --- a/typer/_click/parser.py +++ b/typer/_click/parser.py @@ -187,9 +187,9 @@ def process( f"Argument {self.dest!r} takes {self.nargs} values." ) - if self.nargs == -1 and self.obj.envvar is not None and value == (): - # Replace empty tuple with None so that a value from the - # environment may be tried. + if self.nargs == -1 and value == (): + # Replace empty tuple with None so regular default resolution + # (env var, default map, and parameter default) can be tried. value = None state.opts[self.dest] = value # type: ignore