Skip to content

Commit ac172d9

Browse files
committed
Add implied assertion in WheelInfo
Some simple indexes (anaconda), only list the full-pathname of download URL (assuming same domain, which make sens). WheelInfo.download does not work on this case at it tries to make the requests on the current page domain. Thus we implicitely assume that URL start with http (well https:// would be better, but when you prototype local index could be http.) The error can be weird if we let it propagate (bad ziplife as you try to decode often a 404 html page as ZIP). This thus just add an assert at construction time to catch the error early and that we start with actual protocol. It should in the end be pushed earler in the code (likely at parsing time), where we are likely to know the index URL and be able to resolve URLs at that time. I think there was also a missing comma in test parametrisations, and I added a !r in a few places as I had some error with empty filenames, without quotes, so hard to see. The conftest was also update to explicitely pass a fake URL instead of just a filename. I'm not entirely sure we should allow constructing URLs with things that don't have a protocol though, as this can be confusing.
1 parent 4044983 commit ac172d9

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

micropip/_utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ def check_compatible(filename: str) -> None:
142142
try:
143143
tags = parse_tags(filename)
144144
except InvalidWheelFilename:
145-
raise ValueError(f"Wheel filename is invalid: {filename}") from None
145+
raise ValueError(f"Wheel filename is invalid: {filename!r}") from None
146146
except InvalidVersion:
147-
raise ValueError(f"Wheel version is invalid: {filename}") from None
147+
raise ValueError(f"Wheel version is invalid: {filename!r}") from None
148148

149149
tag: Tag = next(iter(tags))
150150
if "emscripten" not in tag.platform:

micropip/wheelinfo.py

+6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ class WheelInfo:
5454
_dist_info: Path | None = None
5555

5656
def __post_init__(self):
57+
assert (
58+
self.url.startwith(p) for p in ("http:", "https:", "emfs:", "file:")
59+
), self.url
5760
self._project_name = safe_name(self.name)
5861

5962
@classmethod
@@ -63,7 +66,10 @@ def from_url(cls, url: str) -> "WheelInfo":
6366
See https://www.python.org/dev/peps/pep-0427/#file-name-convention
6467
"""
6568
parsed_url = urlparse(url)
69+
if parsed_url.scheme == "":
70+
url = "file:///" + url
6671
file_name = Path(parsed_url.path).name
72+
assert file_name, (url, file_name, parsed_url)
6773
name, version, build, tags = parse_wheel_filename(file_name)
6874
return WheelInfo(
6975
name=name,

tests/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def add_pkg_version(
270270
releases[version] = [
271271
{
272272
"filename": filename,
273-
"url": filename,
273+
"url": f"http://fake.domain/f/{filename}",
274274
"digests": {
275275
"sha256": Wildcard(),
276276
},

tests/test_install.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def _mock_fetch_bytes(arg, *args, **kwargs):
257257

258258
msg = "Access-Control-Allow-Origin"
259259
with pytest.raises(ValueError, match=msg):
260-
await micropip.install("htps://x.com/xxx-1.0.0-py3-none-any.whl")
260+
await micropip.install("https://x.com/xxx-1.0.0-py3-none-any.whl")
261261

262262

263263
@pytest.mark.skip_refcount_check

tests/test_transaction.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@
77
"path",
88
[
99
SNOWBALL_WHEEL,
10-
f"/{SNOWBALL_WHEEL}" f"a/{SNOWBALL_WHEEL}",
10+
f"/{SNOWBALL_WHEEL}",
11+
f"a/{SNOWBALL_WHEEL}",
1112
f"/a/{SNOWBALL_WHEEL}",
1213
f"//a/{SNOWBALL_WHEEL}",
1314
],
1415
)
15-
@pytest.mark.parametrize("protocol", ["https:", "file:", "emfs:", ""])
16+
@pytest.mark.parametrize(
17+
"protocol",
18+
["https:", "file:", "emfs:", ""],
19+
)
1620
def test_parse_wheel_url1(protocol, path):
1721
pytest.importorskip("packaging")
1822
from micropip.transaction import WheelInfo
1923

20-
url = protocol + path
24+
url = protocol + path if protocol else "file:///" + path
2125
wheel = WheelInfo.from_url(url)
2226
assert wheel.name == "snowballstemmer"
2327
assert str(wheel.version) == "2.0.0"

0 commit comments

Comments
 (0)