From 8860f5e40251cf6c8324ee8877e11b24a9b8a130 Mon Sep 17 00:00:00 2001 From: David Hotham Date: Sat, 17 Feb 2024 19:21:22 +0000 Subject: [PATCH 1/3] remove unused code --- src/poetry/utils/env/site_packages.py | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/poetry/utils/env/site_packages.py b/src/poetry/utils/env/site_packages.py index a637f5ad1b8..dca35777a46 100644 --- a/src/poetry/utils/env/site_packages.py +++ b/src/poetry/utils/env/site_packages.py @@ -112,19 +112,6 @@ def find_distribution( return distribution return None - def find_distribution_files_with_suffix( - self, distribution_name: str, suffix: str, writable_only: bool = False - ) -> Iterable[Path]: - for distribution in self.distributions( - name=distribution_name, writable_only=writable_only - ): - files = [] if distribution.files is None else distribution.files - for file in files: - if file.name.endswith(suffix): - path = distribution.locate_file(file) - assert isinstance(path, Path) - yield path - def find_distribution_files_with_name( self, distribution_name: str, name: str, writable_only: bool = False ) -> Iterable[Path]: From 740f34708c1d9863e79145a2e04a7f9388dd19fa Mon Sep 17 00:00:00 2001 From: David Hotham Date: Sun, 17 Mar 2024 19:04:11 +0000 Subject: [PATCH 2/3] simplify treatment of get_highest_priority_hash_type --- src/poetry/repositories/http_repository.py | 10 +++------- src/poetry/utils/cache.py | 4 +--- src/poetry/utils/helpers.py | 3 ++- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/poetry/repositories/http_repository.py b/src/poetry/repositories/http_repository.py index 6c000ac981f..54c7f0da1e9 100644 --- a/src/poetry/repositories/http_repository.py +++ b/src/poetry/repositories/http_repository.py @@ -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)( @@ -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]}" @@ -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 diff --git a/src/poetry/utils/cache.py b/src/poetry/utils/cache.py index 913fdb51beb..1f0f18ed5e5 100644 --- a/src/poetry/utils/cache.py +++ b/src/poetry/utils/cache.py @@ -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: diff --git a/src/poetry/utils/helpers.py b/src/poetry/utils/helpers.py index 81e9a281aee..4dcbab6e17c 100644 --- a/src/poetry/utils/helpers.py +++ b/src/poetry/utils/helpers.py @@ -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 @@ -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 From c7f17ba9942671f9f0d46fe3af4f74b85787c974 Mon Sep 17 00:00:00 2001 From: David Hotham Date: Thu, 21 Mar 2024 23:17:33 +0000 Subject: [PATCH 3/3] simplify importlib.metadata.version treatment --- src/poetry/console/commands/about.py | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/poetry/console/commands/about.py b/src/poetry/console/commands/about.py index cf2548d6f59..56d8e7acdf0 100644 --- a/src/poetry/console/commands/about.py +++ b/src/poetry/console/commands/about.py @@ -1,14 +1,8 @@ from __future__ import annotations -from typing import TYPE_CHECKING - from poetry.console.commands.command import Command -if TYPE_CHECKING: - from collections.abc import Callable - - class AboutCommand(Command): name = "about" @@ -17,16 +11,12 @@ class AboutCommand(Command): def handle(self) -> int: from poetry.utils._compat import metadata - # The metadata.version that we import for Python 3.7 is untyped, work around - # that. - version: Callable[[str], str] = metadata.version - self.line( f"""\ Poetry - Package Management for Python -Version: {version('poetry')} -Poetry-Core Version: {version('poetry-core')} +Version: {metadata.version('poetry')} +Poetry-Core Version: {metadata.version('poetry-core')} Poetry is a dependency manager tracking local dependencies of your projects\ and libraries.