Skip to content

Allow multiple hashes in direct_url.json #11679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions news/11312.feature.rst
Original file line number Diff line number Diff line change
@@ -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``.
15 changes: 13 additions & 2 deletions src/pip/_internal/models/direct_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/test_direct_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
4 changes: 4 additions & 0 deletions tests/unit/test_direct_url_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down