From 991f21f1c7dd7f85fda5f0da5384decc3195682f Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Tue, 31 Dec 2024 09:46:53 +0800 Subject: [PATCH] fix the error type Signed-off-by: Frost Ming --- src/pdm/models/requirements.py | 2 +- src/pdm/utils.py | 76 +++++++++++++++------------------- tests/test_utils.py | 2 +- 3 files changed, 36 insertions(+), 44 deletions(-) diff --git a/src/pdm/models/requirements.py b/src/pdm/models/requirements.py index 72ce0e7e12..a268212e6d 100644 --- a/src/pdm/models/requirements.py +++ b/src/pdm/models/requirements.py @@ -321,7 +321,7 @@ def _parse_url(self) -> None: if relpath is None: try: self.path = Path(url_to_path(url)) - except AssertionError: + except ValueError: pass else: self.path = Path(relpath) diff --git a/src/pdm/utils.py b/src/pdm/utils.py index 5ce8b34e34..b2b8768f67 100644 --- a/src/pdm/utils.py +++ b/src/pdm/utils.py @@ -179,52 +179,44 @@ def cd(path: str | Path) -> Iterator: os.chdir(_old_cwd) -if sys.version_info >= (3, 13): - - def url_to_path(url: str) -> str: - """ - Convert a file: URL to a path. - """ - return Path.from_uri(url).as_posix() -else: - - def url_to_path(url: str) -> str: - """ - Convert a file: URL to a path. - """ - from urllib.request import url2pathname +def url_to_path(url: str) -> str: + """ + Convert a file: URL to a path. + """ + from urllib.request import url2pathname - WINDOWS = sys.platform == "win32" + WINDOWS = sys.platform == "win32" - assert url.startswith("file:"), f"You can only turn file: urls into filenames (not {url!r})" + if not url.startswith("file:"): + raise ValueError(f"You can only turn file: urls into filenames (not {url!r})") - _, netloc, path, _, _ = parse.urlsplit(url) + _, netloc, path, _, _ = parse.urlsplit(url) - if not netloc or netloc == "localhost": - # According to RFC 8089, same as empty authority. - netloc = "" - elif WINDOWS: - # If we have a UNC path, prepend UNC share notation. - netloc = "\\\\" + netloc - else: - raise ValueError(f"non-local file URIs are not supported on this platform: {url!r}") - - path = url2pathname(netloc + path) - - # On Windows, urlsplit parses the path as something like "/C:/Users/foo". - # This creates issues for path-related functions like io.open(), so we try - # to detect and strip the leading slash. - if ( - WINDOWS - and not netloc # Not UNC. - and len(path) >= 3 - and path[0] == "/" # Leading slash to strip. - and path[1].isalpha() # Drive letter. - and path[2:4] in (":", ":/") # Colon + end of string, or colon + absolute path. - ): - path = path[1:] - - return path + if not netloc or netloc == "localhost": + # According to RFC 8089, same as empty authority. + netloc = "" + elif WINDOWS: + # If we have a UNC path, prepend UNC share notation. + netloc = "\\\\" + netloc + else: + raise ValueError(f"non-local file URIs are not supported on this platform: {url!r}") + + path = url2pathname(netloc + path) + + # On Windows, urlsplit parses the path as something like "/C:/Users/foo". + # This creates issues for path-related functions like io.open(), so we try + # to detect and strip the leading slash. + if ( + WINDOWS + and not netloc # Not UNC. + and len(path) >= 3 + and path[0] == "/" # Leading slash to strip. + and path[1].isalpha() # Drive letter. + and path[2:4] in (":", ":/") # Colon + end of string, or colon + absolute path. + ): + path = path[1:] + + return path def split_path_fragments(path: Path) -> tuple[Path, str]: diff --git a/tests/test_utils.py b/tests/test_utils.py index 6e61d8c294..de728b96dd 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -155,7 +155,7 @@ def test_add_ssh_scheme_to_git_uri(given, expected): class TestUrlToPath: def test_non_file_url(self): - with pytest.raises(AssertionError): + with pytest.raises(ValueError): utils.url_to_path("not_a_file_scheme://netloc/path") @pytest.mark.skipif(sys.platform.startswith("win"), reason="Non-Windows test")