Skip to content
Closed
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
2 changes: 1 addition & 1 deletion src/click/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ def convert(
) -> str | bytes | os.PathLike[str]:
rv = value

is_dash = self.file_okay and self.allow_dash and rv in (b"-", "-")
is_dash = self.file_okay and self.allow_dash and rv == "-"
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rv == "-" avoids the str vs bytes tuple membership warning, but if value is ever a bytes path (e.g. passed programmatically or via CliRunner.invoke with bytes args), this comparison will still emit BytesWarning under -bb. Using os.fsdecode(rv) == "-" for the dash check would avoid any bytes/str comparison and matches the approach used in open_stream for handling Path("-").

Suggested change
is_dash = self.file_okay and self.allow_dash and rv == "-"
is_dash = self.file_okay and self.allow_dash and os.fsdecode(rv) == "-"

Copilot uses AI. Check for mistakes.

if not is_dash:
if self.resolve_path:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,22 @@ def foo(input):
assert result.exit_code == 0


def test_path_allow_dash_no_bytes_warning(runner):
"""Path with allow_dash should not compare str against bytes."""
import warnings

@click.command()
@click.argument("input", type=click.Path(allow_dash=True))
def foo(input):
click.echo(input)

with warnings.catch_warnings():
warnings.simplefilter("error", BytesWarning)
result = runner.invoke(foo, ["-"])
assert result.exit_code == 0
Comment on lines +139 to +151
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test likely won't catch the original regression in normal test runs. BytesWarning for bytes/str comparisons is only emitted when Python is started with -b/-bb (as in the issue report); setting warnings.simplefilter("error", BytesWarning) doesn't enable emission if sys.flags.bytes_warning == 0. Consider asserting the behavior via a subprocess started with sys.executable -bb -Werror (similar to tests/test_imports.py), or otherwise ensure the test only runs / is meaningful when sys.flags.bytes_warning is enabled.

Copilot uses AI. Check for mistakes.
assert result.output == "-\n"


def test_file_atomics(runner):
@click.command()
@click.argument("output", type=click.File("wb", atomic=True))
Expand Down
Loading