summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPradyun Gedam <pradyunsg@gmail.com>2023-01-28 20:36:01 +0000
committerGitHub <noreply@github.com>2023-01-28 20:36:01 +0000
commite32ec0a8c3113fb81e3cfc2834c9367d2a6b2711 (patch)
tree7ef30b36942cb1afe5bb809ab9ed22dc00233539
parent29bd6f209981731c5bae38d268c3723f56874dc8 (diff)
parent38681f3d6669754c7e919f0eb051b12931bfb0f2 (diff)
downloadpip-e32ec0a8c3113fb81e3cfc2834c9367d2a6b2711.tar.gz
Merge pull request #11679 from sbidoul/direct_url-hashes-sbi
Allow multiple hashes in direct_url.json
-rw-r--r--news/11312.feature.rst2
-rw-r--r--src/pip/_internal/models/direct_url.py15
-rw-r--r--tests/unit/test_direct_url.py4
-rw-r--r--tests/unit/test_direct_url_helpers.py4
4 files changed, 23 insertions, 2 deletions
diff --git a/news/11312.feature.rst b/news/11312.feature.rst
new file mode 100644
index 000000000..493dde830
--- /dev/null
+++ b/news/11312.feature.rst
@@ -0,0 +1,2 @@
+Change the hashes in the installation report to be a mapping. Emit the
+``archive_info.hashes`` dictionary in ``direct_url.json``.
diff --git a/src/pip/_internal/models/direct_url.py b/src/pip/_internal/models/direct_url.py
index e75feda9c..09b540f91 100644
--- a/src/pip/_internal/models/direct_url.py
+++ b/src/pip/_internal/models/direct_url.py
@@ -103,17 +103,28 @@ class ArchiveInfo:
def __init__(
self,
hash: Optional[str] = None,
+ hashes: Optional[Dict[str, str]] = None,
) -> None:
+ if hash is not None:
+ # Auto-populate the hashes key to upgrade to the new format automatically.
+ # We don't back-populate the legacy hash key.
+ hash_name, hash_value = hash.split("=", 1)
+ if hashes is None:
+ hashes = {hash_name: hash_value}
+ elif hash_name not in hash:
+ hashes = hashes.copy()
+ hashes[hash_name] = hash_value
self.hash = hash
+ self.hashes = hashes
@classmethod
def _from_dict(cls, d: Optional[Dict[str, Any]]) -> Optional["ArchiveInfo"]:
if d is None:
return None
- return cls(hash=_get(d, str, "hash"))
+ return cls(hash=_get(d, str, "hash"), hashes=_get(d, dict, "hashes"))
def _to_dict(self) -> Dict[str, Any]:
- return _filter_none(hash=self.hash)
+ return _filter_none(hash=self.hash, hashes=self.hashes)
class DirInfo:
diff --git a/tests/unit/test_direct_url.py b/tests/unit/test_direct_url.py
index c81e51292..e1708ae93 100644
--- a/tests/unit/test_direct_url.py
+++ b/tests/unit/test_direct_url.py
@@ -39,6 +39,10 @@ def test_archive_info() -> None:
assert (
direct_url.info.hash == direct_url_dict["archive_info"]["hash"] # type: ignore
)
+ # test we add the hashes key automatically
+ direct_url_dict["archive_info"]["hashes"] = { # type: ignore
+ "sha1": "1b8c5bc61a86f377fea47b4276c8c8a5842d2220"
+ }
assert direct_url.to_dict() == direct_url_dict
diff --git a/tests/unit/test_direct_url_helpers.py b/tests/unit/test_direct_url_helpers.py
index 3ab253462..692ee299c 100644
--- a/tests/unit/test_direct_url_helpers.py
+++ b/tests/unit/test_direct_url_helpers.py
@@ -146,6 +146,10 @@ def test_from_link_archive() -> None:
)
assert isinstance(direct_url.info, ArchiveInfo)
assert direct_url.info.hash == "sha1=1b8c5bc61a86f377fea47b4276c8c8a5842d2220"
+ # Test the hashes key has been automatically populated.
+ assert direct_url.info.hashes == {
+ "sha1": "1b8c5bc61a86f377fea47b4276c8c8a5842d2220"
+ }
def test_from_link_dir(tmpdir: Path) -> None: