Skip to content
Myr

Merge pull request #2223 from pallets/env-var

Before adoption: this commit came in with the history the project adopted, so no landing records it.

treat empty auto_envvar as None

authored by David Lord · committed by GitHub · parents ef11be6, 8d7f03d · Browse files · 3 files+11 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+11 added−4 removed

  • tests:+4 added−3 removed
  • other:+7 added−1 removed

3 files+11 added−4 removed

modified:src/click/core.py+4 added-1 removed
2832 unmodified lines
2833 ):2834 envvar = f"{ctx.auto_envvar_prefix}_{self.name.upper()}"2835 rv = os.environ.get(envvar)28362837 if rv:2837 return rv2838 return rv28392840 return None28412842 def value_from_envvar(self, ctx: Context) -> t.Optional[t.Any]:2843 rv: t.Optional[t.Any] = self.resolve_envvar_value(ctx)28442845 if rv is None:
124 unmodified lines
modified:tests/test_options.py+4 added-3 removed
151 unmodified lines
152 with pytest.raises(ValueError, match="default"):153 click.Option(["-a"], type=type, multiple=multiple, nargs=nargs, default=default)154155156@pytest.mark.parametrize("env_key", ["MYPATH", "AUTO_MYPATH"])156def test_empty_envvar(runner):157def test_empty_envvar(runner, env_key):158 @click.command()159 @click.option("--mypath", type=click.Path(exists=True), envvar="MYPATH")160 def cli(mypath):161 click.echo(f"mypath: {mypath}")162162 result = runner.invoke(cli, [], env={"MYPATH": ""})163 assert result.exit_code == 0163 result = runner.invoke(cli, env={env_key: ""}, auto_envvar_prefix="AUTO")164 assert result.exception is None165 assert result.output == "mypath: None\n"166167168def test_multiple_envvar(runner):
738 unmodified lines
modified:CHANGES.rst+3 added
50 unmodified lines
51- ``to_info_dict`` will not fail if a ``ParamType`` doesn't define a52 ``name``. :issue:`2168`53- Shell completion prioritizes option values with option prefixes over54 new options. :issue:`2040`55- Options that get an environment variable value using56 ``autoenvvar_prefix`` treat an empty value as ``None``, consistent57 with a direct ``envvar``. :issue:`2146`585960Version 8.0.461-------------
948 unmodified lines