Skip to content

Commit 06fce7d

Browse files
🐛 Ensure that the default of a list argument is used correctly (#1821)
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 7f888b2 commit 06fce7d

2 files changed

Lines changed: 49 additions & 5 deletions

File tree

‎tests/test_types.py‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,21 @@ def hello_no_choices(
3131

3232

3333
@app.command()
34-
def hello_all(names: list[str] = typer.Argument(["World"], envvar="NAMES")) -> None:
34+
def hello_all_options(name: list[str] = typer.Option(["World"])) -> None:
35+
for n in name:
36+
print(f"Hello {n}!")
37+
38+
39+
@app.command()
40+
def hello_all_args(names: list[str] = typer.Argument(["World"])) -> None:
41+
for name in names:
42+
print(f"Hello {name}!")
43+
44+
45+
@app.command()
46+
def hello_all_envvar(
47+
names: list[str] = typer.Argument(["World"], envvar="NAMES"),
48+
) -> None:
3549
for name in names:
3650
print(f"Hello {name}!")
3751

@@ -100,10 +114,40 @@ def test_enum_choice_missing_message() -> None:
100114
assert "morty" in result.output
101115

102116

117+
def test_list() -> None:
118+
result = runner.invoke(
119+
app, ["hello-all-options", "--name", "Rick", "--name", "Morty"]
120+
)
121+
assert result.exit_code == 0
122+
assert "Hello World!" not in result.output
123+
assert "Hello Rick!" in result.output
124+
assert "Hello Morty!" in result.output
125+
126+
result = runner.invoke(app, ["hello-all-args", "Rick", "Morty"])
127+
assert result.exit_code == 0
128+
assert "Hello World!" not in result.output
129+
assert "Hello Rick!" in result.output
130+
assert "Hello Morty!" in result.output
131+
132+
133+
def test_list_empty() -> None:
134+
result = runner.invoke(app, ["hello-all-args"])
135+
assert result.exit_code == 0
136+
assert "Hello World!" in result.output
137+
138+
result = runner.invoke(app, ["hello-all-options"])
139+
assert result.exit_code == 0
140+
assert "Hello World!" in result.output
141+
142+
result = runner.invoke(app, ["hello-all-envvar"])
143+
assert result.exit_code == 0
144+
assert "Hello World!" in result.output
145+
146+
103147
def test_split_envvar_value(monkeypatch) -> None:
104148
# This will use split_envvar_value to produce two strings from the envvar
105149
monkeypatch.setenv("NAMES", "Rick Morty")
106-
result = runner.invoke(app, ["hello-all"])
150+
result = runner.invoke(app, ["hello-all-envvar"])
107151
assert result.exit_code == 0
108152
assert "Hello Rick!" in result.output
109153
assert "Hello Morty!" in result.output

‎typer/_click/parser.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,9 @@ def process(
187187
f"Argument {self.dest!r} takes {self.nargs} values."
188188
)
189189

190-
if self.nargs == -1 and self.obj.envvar is not None and value == ():
191-
# Replace empty tuple with None so that a value from the
192-
# environment may be tried.
190+
if self.nargs == -1 and value == ():
191+
# Replace empty tuple with None so regular default resolution
192+
# (env var, default map, and parameter default) can be tried.
193193
value = None
194194

195195
state.opts[self.dest] = value # type: ignore

0 commit comments

Comments
 (0)