Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions tests/test_coercion.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,32 @@ def main(config: Path = typer.Option(..., exists=True)):
assert seen == [target]


def test_coercion_path_dash(tmp_path: Path) -> None:
"""Ensure that allow_dash=True is honored with a Path annotation."""
existing = tmp_path / "config.txt"
existing.write_text("hello\n", encoding="utf-8")
app = typer.Typer()
seen: list[Path] = []

@app.command()
def main(config: Path = typer.Argument(..., allow_dash=True, exists=True)):
seen.append(config)

# regression test, cf. #2020
result = runner.invoke(app, ["-"])
assert result.exit_code == 0, result.output
assert seen == [Path("-")]

# ensure an existing file still passes
result = runner.invoke(app, [str(existing)])
assert result.exit_code == 0, result.output
assert seen == [Path("-"), existing]

# ensure a missing file still errors
result = runner.invoke(app, [str(tmp_path / "missing.txt")])
assert result.exit_code == 2


def test_coercion_tuple_files(tmp_path: Path) -> None:
first = tmp_path / "first.txt"
second = tmp_path / "second.txt"
Expand Down
5 changes: 4 additions & 1 deletion typer/_param_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,10 @@ def coerce_cli_path(
raise BadParameter(get_error_msg(exc), ctx=ctx, param=param) from exc

is_dash = (
parameter_info.file_okay and parameter_info.allow_dash and rv in (b"-", "-")
parameter_info.file_okay
and parameter_info.allow_dash
# Use os.fspath() as rv may already have been coerced to a Path
and os.fspath(rv) in (b"-", "-")
)

if not is_dash:
Expand Down
Loading