From dc20f20f4ec48fc7103cadb78ed3f7dae3cffee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Thu, 12 Jun 2025 23:36:24 +0200 Subject: [PATCH] Adjust path_to_url et al. to produce the same results on Python 3.14+ See https://github.com/python/cpython/issues/125974 and https://github.com/pypa/pip/pull/13138#issuecomment-2567715303 and https://discuss.python.org/t/pathname2url-changes-in-python-3-14-breaking-pip-tests/97091 --- news/13423.bugfix.rst | 1 + src/pip/_internal/models/link.py | 7 ++++++- src/pip/_internal/utils/urls.py | 2 +- tests/unit/test_urls.py | 2 +- 4 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 news/13423.bugfix.rst diff --git a/news/13423.bugfix.rst b/news/13423.bugfix.rst new file mode 100644 index 00000000000..75125d922b0 --- /dev/null +++ b/news/13423.bugfix.rst @@ -0,0 +1 @@ +Fix remaining test failures in Python 3.14 by adjusting ``path_to_url`` and similar functions. diff --git a/src/pip/_internal/models/link.py b/src/pip/_internal/models/link.py index 87651c76e25..9b2af74a517 100644 --- a/src/pip/_internal/models/link.py +++ b/src/pip/_internal/models/link.py @@ -6,6 +6,7 @@ import os import posixpath import re +import sys import urllib.parse from collections.abc import Mapping from dataclasses import dataclass @@ -131,7 +132,11 @@ def _clean_file_url_path(part: str) -> str: # should not be quoted. On Linux where drive letters do not # exist, the colon should be quoted. We rely on urllib.request # to do the right thing here. - return urllib.request.pathname2url(urllib.request.url2pathname(part)) + ret = urllib.request.pathname2url(urllib.request.url2pathname(part)) + if sys.version_info >= (3, 14): + # https://discuss.python.org/t/pathname2url-changes-in-python-3-14-breaking-pip-tests/97091 + ret = ret.removeprefix("//") + return ret # percent-encoded: / diff --git a/src/pip/_internal/utils/urls.py b/src/pip/_internal/utils/urls.py index 9f34f882a1a..e951a5e4e47 100644 --- a/src/pip/_internal/utils/urls.py +++ b/src/pip/_internal/utils/urls.py @@ -12,7 +12,7 @@ def path_to_url(path: str) -> str: quoted path parts. """ path = os.path.normpath(os.path.abspath(path)) - url = urllib.parse.urljoin("file:", urllib.request.pathname2url(path)) + url = urllib.parse.urljoin("file://", urllib.request.pathname2url(path)) return url diff --git a/tests/unit/test_urls.py b/tests/unit/test_urls.py index 0c145255080..2a56e459f1d 100644 --- a/tests/unit/test_urls.py +++ b/tests/unit/test_urls.py @@ -11,7 +11,7 @@ def test_path_to_url_unix() -> None: assert path_to_url("/tmp/file") == "file:///tmp/file" path = os.path.join(os.getcwd(), "file") - assert path_to_url("file") == "file://" + urllib.request.pathname2url(path) + assert path_to_url("file") == "file://" + path @pytest.mark.skipif("sys.platform != 'win32'")