From ae9c5a0a5bf37137f82933029ca241cb286e310a Mon Sep 17 00:00:00 2001 From: Akshay Laddha Date: Fri, 10 Apr 2026 18:15:18 -0700 Subject: [PATCH 1/3] Fix Path bytes dash check under Python bytes warnings --- src/click/types.py | 8 +++++++- tests/test_types.py | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) 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..1cb2a5dff7 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,23 @@ def test_path_type(runner, cls, expect): assert result.return_value == expect +def test_path_bytes_dash_no_byteswarning(): + result = subprocess.run( + [ + sys.executable, + "-bb", + "-c", + "from click.types import Path; Path(allow_dash=True).convert(b'-', None, None)", + ], + 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") From 9a37737c5af44cd9a8ac2609e63c345a7eb4bac1 Mon Sep 17 00:00:00 2001 From: Akshay Laddha Date: Fri, 10 Apr 2026 18:39:00 -0700 Subject: [PATCH 2/3] Wrap subprocess code string in regression test --- tests/test_types.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/test_types.py b/tests/test_types.py index 1cb2a5dff7..e3184d05b9 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -109,12 +109,17 @@ def test_path_type(runner, cls, 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", - "from click.types import Path; Path(allow_dash=True).convert(b'-', None, None)", + code, ], cwd=pathlib.Path(__file__).resolve().parents[1], env={**os.environ, "PYTHONPATH": "src"}, From f8a0ce59539473b380ad797e132c1ddc2f672b16 Mon Sep 17 00:00:00 2001 From: Akshay Laddha Date: Fri, 10 Apr 2026 18:42:40 -0700 Subject: [PATCH 3/3] Apply ruff formatting to regression test --- tests/test_types.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_types.py b/tests/test_types.py index e3184d05b9..f135496f24 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -110,8 +110,7 @@ def test_path_type(runner, cls, expect): def test_path_bytes_dash_no_byteswarning(): code = ( - "from click.types import Path; " - "Path(allow_dash=True).convert(b'-', None, None)" + "from click.types import Path; Path(allow_dash=True).convert(b'-', None, None)" ) result = subprocess.run(