Skip to content

Commit ae9c5a0

Browse files
author
Akshay Laddha
committed
Fix Path bytes dash check under Python bytes warnings
1 parent 04ef3a6 commit ae9c5a0

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/click/types.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,13 @@ def convert(
973973
) -> str | bytes | os.PathLike[str]:
974974
rv = value
975975

976-
is_dash = self.file_okay and self.allow_dash and rv in (b"-", "-")
976+
if self.file_okay and self.allow_dash:
977+
if isinstance(rv, bytes):
978+
is_dash = rv == b"-"
979+
else:
980+
is_dash = rv == "-"
981+
else:
982+
is_dash = False
977983

978984
if not is_dash:
979985
if self.resolve_path:

tests/test_types.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import os.path
22
import pathlib
33
import platform
4+
import subprocess
5+
import sys
46
import tempfile
57

68
import pytest
@@ -106,6 +108,23 @@ def test_path_type(runner, cls, expect):
106108
assert result.return_value == expect
107109

108110

111+
def test_path_bytes_dash_no_byteswarning():
112+
result = subprocess.run(
113+
[
114+
sys.executable,
115+
"-bb",
116+
"-c",
117+
"from click.types import Path; Path(allow_dash=True).convert(b'-', None, None)",
118+
],
119+
cwd=pathlib.Path(__file__).resolve().parents[1],
120+
env={**os.environ, "PYTHONPATH": "src"},
121+
capture_output=True,
122+
text=True,
123+
)
124+
125+
assert result.returncode == 0, result.stderr
126+
127+
109128
def _symlinks_supported():
110129
with tempfile.TemporaryDirectory(prefix="click-pytest-") as tempdir:
111130
target = os.path.join(tempdir, "target")

0 commit comments

Comments
 (0)