Skip to content

Commit 56b7497

Browse files
committed
Make the remote URI rule match pip exactly
This fixes handling of Windows paths where the drive-letter can be misinterpreted as a URI scheme. Matching the `pip` implementation details in this case is the simplest way to handle the inherent ambiguity.
1 parent 710e43a commit 56b7497

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

piptools/_compat/pip_compat.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ def _relativize_comes_from_location(original_comes_from: str, /) -> str:
141141
# split on the space
142142
prefix, space_sep, suffix = original_comes_from.partition(" ")
143143

144-
# if the value part is a URI, return the original
145-
if _is_uri(suffix):
144+
# if the value part is a remote URI for pip, return the original
145+
if _is_remote_pip_uri(suffix):
146146
return original_comes_from
147147

148148
file_path = pathlib.Path(suffix)
@@ -174,32 +174,24 @@ def _normalize_comes_from_location(original_comes_from: str, /) -> str:
174174
# split on the space
175175
prefix, space_sep, suffix = original_comes_from.partition(" ")
176176

177-
# if the value part is a URI, return the original
178-
if _is_uri(suffix):
177+
# if the value part is a remote URI for pip, return the original
178+
if _is_remote_pip_uri(suffix):
179179
return original_comes_from
180180

181181
# convert to a posix-style path
182182
suffix = pathlib.Path(suffix).as_posix()
183183
return f"{prefix}{space_sep}{suffix}"
184184

185185

186-
def _is_uri(value: str) -> bool:
186+
def _is_remote_pip_uri(value: str) -> bool:
187187
"""
188-
Test a string to see if it is a URI.
188+
Test a string to see if it is a URI treated as a remote file in ``pip``.
189+
Specifically this means that it's a 'file', 'http', or 'https' URI.
189190
190-
The test is performed by trying a URL parse and seeing is a scheme is populated.
191-
192-
This means that according to this rule, valid URLs such as
193-
``example.com/data/my_pip_constraints.txt`` may fail to count as URIs.
194-
However, we cannot safely distinguish such strings from real filesystem paths.
195-
e.g., ``./example.com/`` may be a directory.
196-
197-
Importantly, realistic usage such as
198-
``-c https://example.com/constraints.txt``
199-
is properly detected by this technique.
191+
The test is performed by trying a URL parse and reading the scheme.
200192
"""
201-
parse_result = urllib.parse.urlparse(value)
202-
return parse_result.scheme != ""
193+
scheme = urllib.parse.urlsplit(value).scheme
194+
return scheme in ("http", "https", "file")
203195

204196

205197
def create_wheel_cache(cache_dir: str, format_control: str | None = None) -> WheelCache:

0 commit comments

Comments
 (0)