Skip to content

Commit 8d7f03d

Browse files
committed
treat empty auto_envvar as None
1 parent ef11be6 commit 8d7f03d

3 files changed

Lines changed: 11 additions & 4 deletions

File tree

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ Version 8.1.0
5252
``name``. :issue:`2168`
5353
- Shell completion prioritizes option values with option prefixes over
5454
new options. :issue:`2040`
55+
- Options that get an environment variable value using
56+
``autoenvvar_prefix`` treat an empty value as ``None``, consistent
57+
with a direct ``envvar``. :issue:`2146`
5558

5659

5760
Version 8.0.4

src/click/core.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2834,7 +2834,10 @@ def resolve_envvar_value(self, ctx: Context) -> t.Optional[str]:
28342834
envvar = f"{ctx.auto_envvar_prefix}_{self.name.upper()}"
28352835
rv = os.environ.get(envvar)
28362836

2837-
return rv
2837+
if rv:
2838+
return rv
2839+
2840+
return None
28382841

28392842
def value_from_envvar(self, ctx: Context) -> t.Optional[t.Any]:
28402843
rv: t.Optional[t.Any] = self.resolve_envvar_value(ctx)

tests/test_options.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,15 @@ def test_init_bad_default_list(runner, multiple, nargs, default):
153153
click.Option(["-a"], type=type, multiple=multiple, nargs=nargs, default=default)
154154

155155

156-
def test_empty_envvar(runner):
156+
@pytest.mark.parametrize("env_key", ["MYPATH", "AUTO_MYPATH"])
157+
def test_empty_envvar(runner, env_key):
157158
@click.command()
158159
@click.option("--mypath", type=click.Path(exists=True), envvar="MYPATH")
159160
def cli(mypath):
160161
click.echo(f"mypath: {mypath}")
161162

162-
result = runner.invoke(cli, [], env={"MYPATH": ""})
163-
assert result.exit_code == 0
163+
result = runner.invoke(cli, env={env_key: ""}, auto_envvar_prefix="AUTO")
164+
assert result.exception is None
164165
assert result.output == "mypath: None\n"
165166

166167

0 commit comments

Comments
 (0)