Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

treat empty auto_envvar as None #2223

Merged
merged 1 commit into from Mar 28, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -52,6 +52,9 @@ Version 8.1.0
``name``. :issue:`2168`
- Shell completion prioritizes option values with option prefixes over
new options. :issue:`2040`
- Options that get an environment variable value using
``autoenvvar_prefix`` treat an empty value as ``None``, consistent
with a direct ``envvar``. :issue:`2146`


Version 8.0.4
Expand Down
5 changes: 4 additions & 1 deletion src/click/core.py
Expand Up @@ -2834,7 +2834,10 @@ def resolve_envvar_value(self, ctx: Context) -> t.Optional[str]:
envvar = f"{ctx.auto_envvar_prefix}_{self.name.upper()}"
rv = os.environ.get(envvar)

return rv
if rv:
return rv

return None

def value_from_envvar(self, ctx: Context) -> t.Optional[t.Any]:
rv: t.Optional[t.Any] = self.resolve_envvar_value(ctx)
Expand Down
7 changes: 4 additions & 3 deletions tests/test_options.py
Expand Up @@ -153,14 +153,15 @@ def test_init_bad_default_list(runner, multiple, nargs, default):
click.Option(["-a"], type=type, multiple=multiple, nargs=nargs, default=default)


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

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


Expand Down