From 340ca214135d9e111c32e1aeaac12451b0723cf4 Mon Sep 17 00:00:00 2001 From: finswimmer Date: Mon, 2 Dec 2024 11:38:52 +0100 Subject: [PATCH] fix: workaround unsupported metadata version 2.4 in pkginfo --- src/poetry/inspection/info.py | 19 ++++++++++++++++--- tests/inspection/test_info.py | 3 +-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/poetry/inspection/info.py b/src/poetry/inspection/info.py index 08c7a7918b6..7fea8d2b4c8 100644 --- a/src/poetry/inspection/info.py +++ b/src/poetry/inspection/info.py @@ -5,6 +5,7 @@ import glob import logging import tempfile +import warnings from pathlib import Path from typing import TYPE_CHECKING @@ -12,6 +13,7 @@ import pkginfo +from pkginfo.distribution import NewMetadataVersion from poetry.core.constraints.version import Version from poetry.core.factory import Factory from poetry.core.packages.dependency import Dependency @@ -265,7 +267,10 @@ def _from_distribution( :param dist: The distribution instance to parse information from. """ - if dist.metadata_version not in pkginfo.distribution.HEADER_ATTRS: + # The current pkginfo version (1.11.2) does not support 2.4. + # The fields we are interested in can be parsed nevertheless. + supported_metadata_versions = {*pkginfo.distribution.HEADER_ATTRS.keys(), "2.4"} + if dist.metadata_version not in supported_metadata_versions: # This check can be replaced once upstream implements strict parsing # https://bugs.launchpad.net/pkginfo/+bug/2058697 raise ValueError(f"Unknown metadata version: {dist.metadata_version}") @@ -389,7 +394,11 @@ def from_metadata_directory(cls, path: Path) -> PackageInfo | None: if directory.suffix == ".egg-info": dist = pkginfo.UnpackedSDist(directory.as_posix()) elif directory.suffix == ".dist-info": - dist = pkginfo.Wheel(directory.as_posix()) + with warnings.catch_warnings(): + # Filtering the NewMetadataVersion can be remove once pkginfo + # supports MetadataVersion 2.4. + warnings.simplefilter("ignore", NewMetadataVersion) + dist = pkginfo.Wheel(directory.as_posix()) else: continue break @@ -489,7 +498,11 @@ def from_wheel(cls, path: Path) -> PackageInfo: :param path: Path to wheel. """ try: - wheel = pkginfo.Wheel(str(path)) + with warnings.catch_warnings(): + # Filtering the NewMetadataVersion can be remove once pkginfo + # supports MetadataVersion 2.4. + warnings.simplefilter("ignore", NewMetadataVersion) + wheel = pkginfo.Wheel(str(path)) return cls._from_distribution(wheel) except ValueError as e: raise PackageInfoError(path, e) diff --git a/tests/inspection/test_info.py b/tests/inspection/test_info.py index 776cafa60d1..633a501c155 100644 --- a/tests/inspection/test_info.py +++ b/tests/inspection/test_info.py @@ -12,7 +12,6 @@ from build import BuildBackendException from build import ProjectBuilder from packaging.metadata import parse_email -from pkginfo.distribution import NewMetadataVersion from poetry.inspection.info import PackageInfo from poetry.inspection.info import PackageInfoError @@ -221,7 +220,7 @@ def test_info_from_wheel_metadata_version_unknown( / "demo_metadata_version_unknown-0.1.0-py2.py3-none-any.whl" ) - with pytest.warns(NewMetadataVersion), pytest.raises(PackageInfoError) as e: + with pytest.raises(PackageInfoError) as e: PackageInfo.from_wheel(path) assert "Unknown metadata version: 999.3" in str(e.value)