Skip to content

Commit e32ec0a

Browse files
authored
Merge pull request #11679 from sbidoul/direct_url-hashes-sbi
Allow multiple hashes in direct_url.json
2 parents 29bd6f2 + 38681f3 commit e32ec0a

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

news/11312.feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Change the hashes in the installation report to be a mapping. Emit the
2+
``archive_info.hashes`` dictionary in ``direct_url.json``.

src/pip/_internal/models/direct_url.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,28 @@ class ArchiveInfo:
103103
def __init__(
104104
self,
105105
hash: Optional[str] = None,
106+
hashes: Optional[Dict[str, str]] = None,
106107
) -> None:
108+
if hash is not None:
109+
# Auto-populate the hashes key to upgrade to the new format automatically.
110+
# We don't back-populate the legacy hash key.
111+
hash_name, hash_value = hash.split("=", 1)
112+
if hashes is None:
113+
hashes = {hash_name: hash_value}
114+
elif hash_name not in hash:
115+
hashes = hashes.copy()
116+
hashes[hash_name] = hash_value
107117
self.hash = hash
118+
self.hashes = hashes
108119

109120
@classmethod
110121
def _from_dict(cls, d: Optional[Dict[str, Any]]) -> Optional["ArchiveInfo"]:
111122
if d is None:
112123
return None
113-
return cls(hash=_get(d, str, "hash"))
124+
return cls(hash=_get(d, str, "hash"), hashes=_get(d, dict, "hashes"))
114125

115126
def _to_dict(self) -> Dict[str, Any]:
116-
return _filter_none(hash=self.hash)
127+
return _filter_none(hash=self.hash, hashes=self.hashes)
117128

118129

119130
class DirInfo:

tests/unit/test_direct_url.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ def test_archive_info() -> None:
3939
assert (
4040
direct_url.info.hash == direct_url_dict["archive_info"]["hash"] # type: ignore
4141
)
42+
# test we add the hashes key automatically
43+
direct_url_dict["archive_info"]["hashes"] = { # type: ignore
44+
"sha1": "1b8c5bc61a86f377fea47b4276c8c8a5842d2220"
45+
}
4246
assert direct_url.to_dict() == direct_url_dict
4347

4448

tests/unit/test_direct_url_helpers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ def test_from_link_archive() -> None:
146146
)
147147
assert isinstance(direct_url.info, ArchiveInfo)
148148
assert direct_url.info.hash == "sha1=1b8c5bc61a86f377fea47b4276c8c8a5842d2220"
149+
# Test the hashes key has been automatically populated.
150+
assert direct_url.info.hashes == {
151+
"sha1": "1b8c5bc61a86f377fea47b4276c8c8a5842d2220"
152+
}
149153

150154

151155
def test_from_link_dir(tmpdir: Path) -> None:

0 commit comments

Comments
 (0)