Skip to content

Commit 232cc9d

Browse files
committed
Parse hash data before passing to MetadataFile
1 parent 93b274e commit 232cc9d

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/pip/_internal/models/link.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,20 @@ class MetadataFile:
101101

102102
hashes: Optional[Dict[str, str]]
103103

104-
# TODO: Do we care about stripping out unsupported hash methods?
105-
def __init__(self, hashes: Optional[Dict[str, str]]):
106-
if hashes:
107-
hashes = {n: v for n, v in hashes.items() if n in _SUPPORTED_HASHES}
108-
# We need to use this as this is a frozen dataclass
109-
object.__setattr__(self, "hashes", hashes)
104+
def __post_init__(self) -> None:
105+
if self.hashes is not None:
106+
assert all(name in _SUPPORTED_HASHES for name in self.hashes)
107+
108+
109+
def supported_hashes(hashes: Optional[Dict[str, str]]) -> Optional[Dict[str, str]]:
110+
# Remove any unsupported hash types from the mapping. If this leaves no
111+
# supported hashes, return None
112+
if hashes is None:
113+
return None
114+
hashes = {n: v for n, v in hashes.items() if n in _SUPPORTED_HASHES}
115+
if len(hashes) > 0:
116+
return hashes
117+
return None
110118

111119

112120
def _clean_url_path_part(part: str) -> str:
@@ -273,7 +281,7 @@ def from_json(
273281
metadata_info = file_data.get("dist-info-metadata", False)
274282
if isinstance(metadata_info, dict):
275283
# The file exists, and hashes have been supplied
276-
metadata_file_data = MetadataFile(metadata_info)
284+
metadata_file_data = MetadataFile(supported_hashes(metadata_info))
277285
elif metadata_info:
278286
# The file exists, but there are no hashes
279287
metadata_file_data = MetadataFile(None)
@@ -328,7 +336,7 @@ def from_element(
328336
# The file exists, and hashes have been supplied
329337
hashname, sep, hashval = metadata_info.partition("=")
330338
if sep == "=":
331-
metadata_file_data = MetadataFile({hashname: hashval})
339+
metadata_file_data = MetadataFile(supported_hashes({hashname: hashval}))
332340
else:
333341
# Error - data is wrong. Treat as no hashes supplied.
334342
logger.debug(

tests/unit/test_collector.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1085,10 +1085,10 @@ def test_link_hash_parsing(url: str, result: Optional[LinkHash]) -> None:
10851085
[
10861086
("sha256=aa113592bbe", MetadataFile({"sha256": "aa113592bbe"})),
10871087
("sha256=", MetadataFile({"sha256": ""})),
1088-
("sha500=aa113592bbe", MetadataFile({})),
1088+
("sha500=aa113592bbe", MetadataFile(None)),
10891089
("true", MetadataFile(None)),
10901090
(None, None),
1091-
# TODO: Are these correct?
1091+
# Attribute is present but invalid
10921092
("", MetadataFile(None)),
10931093
("aa113592bbe", MetadataFile(None)),
10941094
],
@@ -1104,4 +1104,3 @@ def test_metadata_file_info_parsing_html(
11041104
base_url = "https://index.url/simple"
11051105
link = Link.from_element(attribs, page_url, base_url)
11061106
assert link is not None and link.metadata_file_data == expected
1107-
# TODO: Do we need to do something for the JSON data?

0 commit comments

Comments
 (0)