Skip to content

Commit e7b52af

Browse files
authored
adapt to improved type hints and fix edge case issues with Package-files (#10673)
1 parent 9676185 commit e7b52af

File tree

7 files changed

+51
-24
lines changed

7 files changed

+51
-24
lines changed

poetry.lock

Lines changed: 13 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "2.2.1"
44
description = "Python dependency management and packaging made easy."
55
requires-python = ">=3.10,<4.0"
66
dependencies = [
7-
"poetry-core (==2.2.1)",
7+
"poetry-core @ git+https://github.com/python-poetry/poetry-core.git",
88
"build (>=1.2.1,<2.0.0)",
99
"cachecontrol[filecache] (>=0.14.0,<0.15.0)",
1010
"cleo (>=2.1.0,<3.0.0)",

src/poetry/inspection/info.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929

3030
if TYPE_CHECKING:
3131
from collections.abc import Iterator
32-
from collections.abc import Mapping
3332
from collections.abc import Sequence
3433

3534
from packaging.metadata import RawMetadata
3635
from packaging.utils import NormalizedName
36+
from poetry.core.packages.package import PackageFile
3737
from poetry.core.packages.project_package import ProjectPackage
3838

3939

@@ -57,7 +57,7 @@ def __init__(
5757
summary: str | None = None,
5858
requires_dist: list[str] | None = None,
5959
requires_python: str | None = None,
60-
files: Sequence[Mapping[str, str]] | None = None,
60+
files: Sequence[PackageFile] | None = None,
6161
yanked: str | bool = False,
6262
cache_version: str | None = None,
6363
) -> None:

src/poetry/packages/locker.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,10 @@ def _get_locked_package(
420420
package.files = package_files
421421
elif "hashes" in metadata:
422422
hashes = cast("dict[str, Any]", metadata["hashes"])
423-
package.files = [{"name": h, "hash": h} for h in hashes[name]]
423+
# Strictly speaking, this is not correct,
424+
# but we do not know the file names here,
425+
# so we just set both file and hash.
426+
package.files = [{"file": h, "hash": h} for h in hashes[name]]
424427
elif source_type in {"git", "directory", "url"}:
425428
package.files = []
426429
else:

src/poetry/repositories/http_repository.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from collections.abc import Iterator
3939

4040
from packaging.utils import NormalizedName
41+
from poetry.core.packages.package import PackageFile
4142
from poetry.core.packages.utils.link import Link
4243

4344
from poetry.repositories.link_sources.base import LinkSource
@@ -340,7 +341,7 @@ def _links_to_data(self, links: list[Link], data: PackageInfo) -> dict[str, Any]
340341
f' "{data.version}"'
341342
)
342343

343-
files: list[dict[str, Any]] = []
344+
files: list[PackageFile] = []
344345
for link in links:
345346
if link.yanked and not data.yanked:
346347
# drop yanked files unless the entire release is yanked
@@ -359,7 +360,23 @@ def _links_to_data(self, links: list[Link], data: PackageInfo) -> dict[str, Any]
359360
):
360361
file_hash = f"{hash_type}:{link.hashes[hash_type]}"
361362

362-
files.append({"file": link.filename, "hash": file_hash})
363+
if file_hash is None:
364+
# Is that even possible?
365+
# Before introducing this warning and ignoring the file,
366+
# null hashes would have been written to the lockfile,
367+
# which should have been failed in the Chooser at latest.
368+
self._log(
369+
f"Failed to determine hash of {link.url}. Skipping file.",
370+
level="warning",
371+
)
372+
else:
373+
files.append({"file": link.filename, "hash": file_hash})
374+
375+
if not files:
376+
raise PackageNotFoundError(
377+
f'Could not determine a hash for any distribution link of package: "{data.name}" version:'
378+
f' "{data.version}"'
379+
)
363380

364381
data.files = files
365382

tests/installation/test_chooser.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616

1717
if TYPE_CHECKING:
18+
from poetry.core.packages.package import PackageFile
19+
1820
from poetry.repositories.repository_pool import RepositoryPool
1921
from tests.conftest import Config
2022
from tests.types import DistributionHashGetter
@@ -211,7 +213,7 @@ def test_chooser_chooses_distributions_that_match_the_package_hashes(
211213
chooser = Chooser(pool, env)
212214

213215
package = Package("isort", "4.3.4")
214-
files = [
216+
files: list[PackageFile] = [
215217
{
216218
"file": filename,
217219
"hash": (f"sha256:{dist_hash_getter(filename).sha256}"),
@@ -246,9 +248,9 @@ def test_chooser_chooses_yanked_if_no_others(
246248
chooser = Chooser(pool, env)
247249

248250
package = Package("black", "21.11b0")
249-
files = [
251+
files: list[PackageFile] = [
250252
{
251-
"filename": filename,
253+
"file": filename,
252254
"hash": (f"sha256:{dist_hash_getter(filename).sha256}"),
253255
}
254256
for filename in [f"{package.name}-{package.version}-py3-none-any.whl"]
@@ -286,9 +288,9 @@ def test_chooser_does_not_choose_yanked_if_others(
286288
)
287289

288290
package = Package("futures", "3.2.0")
289-
files = [
291+
files: list[PackageFile] = [
290292
{
291-
"filename": filename,
293+
"file": filename,
292294
"hash": (f"sha256:{dist_hash_getter(filename).sha256}"),
293295
}
294296
for filename in [
@@ -330,12 +332,12 @@ def test_chooser_throws_an_error_if_package_hashes_do_not_match(
330332
chooser = Chooser(pool, env)
331333

332334
package = Package("isort", "4.3.4")
333-
files = [
335+
files: list[PackageFile] = [
334336
{
335337
"hash": (
336338
"sha256:0000000000000000000000000000000000000000000000000000000000000000"
337339
),
338-
"filename": "isort-4.3.4.tar.gz",
340+
"file": "isort-4.3.4.tar.gz",
339341
}
340342
]
341343
if source_type == "legacy":
@@ -373,7 +375,7 @@ def test_chooser_md5_remote_fallback_to_sha256_inline_calculation(
373375
)
374376
package.files = [
375377
{
376-
"filename": filename,
378+
"file": filename,
377379
"hash": (f"sha256:{dist_hash_getter(filename).sha256}"),
378380
}
379381
for filename in [f"{package.name}-{package.version}.tar.gz"]

tests/installation/test_executor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from collections.abc import Mapping
4343
from collections.abc import Sequence
4444

45+
from poetry.core.packages.package import PackageFile
4546
from pytest_mock import MockerFixture
4647

4748
from poetry.config.config import Config
@@ -1819,7 +1820,7 @@ def test_other_error(
18191820
],
18201821
)
18211822
def test_executor_known_hashes(
1822-
package_files: list[dict[str, str]],
1823+
package_files: list[PackageFile],
18231824
expected_url_reference: dict[str, Any],
18241825
tmp_venv: VirtualEnv,
18251826
pool: RepositoryPool,

0 commit comments

Comments
 (0)