Skip to content

Commit 3028854

Browse files
authored
🐛 Ensure that an envvar set for typer.Option works as expected (#1788)
1 parent 959a356 commit 3028854

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

tests/test_others.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,18 @@ def main(
325325
assert "[]" in result.output
326326

327327

328+
def test_option_uses_envvar_when_required():
329+
app = typer.Typer()
330+
331+
@app.command()
332+
def main(user: Annotated[str, typer.Option(envvar="ME")]):
333+
print(f"Hello {user}")
334+
335+
result = runner.invoke(app, env={"ME": "rick"})
336+
assert result.exit_code == 0
337+
assert "Hello rick" in result.output
338+
339+
328340
def test_completion_argument():
329341
file_path = Path(__file__).parent / "assets/completion_argument.py"
330342
result = subprocess.run(

typer/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,7 @@ def value_from_envvar(self, ctx: _click.Context) -> Any:
672672
# Absent environment variable or an empty string is interpreted as unset.
673673
if rv is None:
674674
return None
675+
return rv
675676

676677
def resolve_envvar_value(self, ctx: _click.Context) -> str | None:
677678
rv = super().resolve_envvar_value(ctx)

0 commit comments

Comments
 (0)