Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}!")

Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that it's this particular test that's failing on master, i.e. what this bug fix PR addresses.


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
Expand Down
6 changes: 3 additions & 3 deletions typer/_click/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading