diff --git a/src/click/types.py b/src/click/types.py index e71c1c21e4..171e8e96e4 100644 --- a/src/click/types.py +++ b/src/click/types.py @@ -973,7 +973,13 @@ def convert( ) -> str | bytes | os.PathLike[str]: rv = value - is_dash = self.file_okay and self.allow_dash and rv in (b"-", "-") + if self.file_okay and self.allow_dash: + if isinstance(rv, bytes): + is_dash = rv == b"-" + else: + is_dash = rv == "-" + else: + is_dash = False if not is_dash: if self.resolve_path: diff --git a/tests/test_types.py b/tests/test_types.py index 75434f1042..f135496f24 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -1,6 +1,8 @@ import os.path import pathlib import platform +import subprocess +import sys import tempfile import pytest @@ -106,6 +108,27 @@ def test_path_type(runner, cls, expect): assert result.return_value == expect +def test_path_bytes_dash_no_byteswarning(): + code = ( + "from click.types import Path; Path(allow_dash=True).convert(b'-', None, None)" + ) + + result = subprocess.run( + [ + sys.executable, + "-bb", + "-c", + code, + ], + cwd=pathlib.Path(__file__).resolve().parents[1], + env={**os.environ, "PYTHONPATH": "src"}, + capture_output=True, + text=True, + ) + + assert result.returncode == 0, result.stderr + + def _symlinks_supported(): with tempfile.TemporaryDirectory(prefix="click-pytest-") as tempdir: target = os.path.join(tempdir, "target")