Skip to content

Commit

Permalink
simplify treatment of get_highest_priority_hash_type
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby authored and abn committed Mar 24, 2024
1 parent a665b47 commit fafb6d7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 11 deletions.
10 changes: 3 additions & 7 deletions src/poetry/repositories/http_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def _get_info_from_metadata(self, link: Link) -> PackageInfo | None:
response = self.session.get(link.metadata_url)
if link.metadata_hashes and (
hash_name := get_highest_priority_hash_type(
set(link.metadata_hashes.keys()), f"{link.filename}.metadata"
link.metadata_hashes, f"{link.filename}.metadata"
)
):
metadata_hash = getattr(hashlib, hash_name)(
Expand Down Expand Up @@ -351,9 +351,7 @@ def _links_to_data(self, links: list[Link], data: PackageInfo) -> dict[str, Any]
file_hash = self.calculate_sha256(link)

if file_hash is None and (
hash_type := get_highest_priority_hash_type(
set(link.hashes.keys()), link.filename
)
hash_type := get_highest_priority_hash_type(link.hashes, link.filename)
):
file_hash = f"{hash_type}:{link.hashes[hash_type]}"

Expand All @@ -372,9 +370,7 @@ def _links_to_data(self, links: list[Link], data: PackageInfo) -> dict[str, Any]

def calculate_sha256(self, link: Link) -> str | None:
with self._cached_or_downloaded_file(link) as filepath:
hash_name = get_highest_priority_hash_type(
set(link.hashes.keys()), link.filename
)
hash_name = get_highest_priority_hash_type(link.hashes, link.filename)
known_hash = None
with suppress(ValueError, AttributeError):
# Handle ValueError here as well since under FIPS environments
Expand Down
4 changes: 1 addition & 3 deletions src/poetry/utils/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,7 @@ def __init__(self, *, cache_dir: Path) -> None:
def get_cache_directory_for_link(self, link: Link) -> Path:
key_parts = {"url": link.url_without_fragment}

if hash_name := get_highest_priority_hash_type(
set(link.hashes.keys()), link.filename
):
if hash_name := get_highest_priority_hash_type(link.hashes, link.filename):
key_parts[hash_name] = link.hashes[hash_name]

if link.subdirectory_fragment:
Expand Down
3 changes: 2 additions & 1 deletion src/poetry/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

if TYPE_CHECKING:
from collections.abc import Callable
from collections.abc import Collection
from collections.abc import Iterator
from types import TracebackType

Expand Down Expand Up @@ -332,7 +333,7 @@ def get_file_hash(path: Path, hash_name: str = "sha256") -> str:


def get_highest_priority_hash_type(
hash_types: set[str], archive_name: str
hash_types: Collection[str], archive_name: str
) -> str | None:
if not hash_types:
return None
Expand Down

0 comments on commit fafb6d7

Please sign in to comment.