Skip to content
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

Draft: Fix warning message for missing METADATA file #13119

Closed
wants to merge 7 commits into from
Closed
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
1 change: 1 addition & 0 deletions news/12446.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Updated warning message for invalid dist-info directory due to missing METADATA file.
11 changes: 10 additions & 1 deletion src/pip/_internal/metadata/importlib/_compat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import importlib.metadata
import os
from typing import Any, Optional, Protocol, Tuple, cast
from typing import Any, Optional, Protocol, Tuple, Union, cast

from pip._vendor.packaging.utils import NormalizedName, canonicalize_name

Expand Down Expand Up @@ -33,6 +33,12 @@ def name(self) -> str:
def parent(self) -> "BasePath":
raise NotImplementedError()

def joinpath(self, *args: Union[str, os.PathLike[str]]) -> "BasePath":
raise NotImplementedError()

def exists(self) -> bool:
raise NotImplementedError()


def get_info_location(d: importlib.metadata.Distribution) -> Optional[BasePath]:
"""Find the path to the distribution's metadata directory.
Expand Down Expand Up @@ -81,5 +87,8 @@ def get_dist_canonical_name(dist: importlib.metadata.Distribution) -> Normalized

name = cast(Any, dist).name
if not isinstance(name, str):
info_location = get_info_location(dist)
if info_location and not info_location.joinpath("METADATA"):
raise BadMetadata(dist, reason="missing `METADATA` file")
raise BadMetadata(dist, reason="invalid metadata entry 'name'")
return canonicalize_name(name)
5 changes: 3 additions & 2 deletions src/pip/_internal/metadata/importlib/_dists.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Mapping,
Optional,
Sequence,
Union,
cast,
)

Expand Down Expand Up @@ -100,8 +101,8 @@ class Distribution(BaseDistribution):
def __init__(
self,
dist: importlib.metadata.Distribution,
info_location: Optional[BasePath],
installed_location: Optional[BasePath],
info_location: Optional[Union[BasePath, pathlib.PurePosixPath]],
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PurePosixPath doesn't have exists and I didn't find more elegant way to solve it

installed_location: Optional[Union[BasePath, pathlib.PurePosixPath]],
) -> None:
self._dist = dist
self._info_location = info_location
Expand Down
19 changes: 19 additions & 0 deletions tests/functional/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,3 +751,22 @@ def test_list_pep610_editable(script: PipTestEnvironment) -> None:
break
else:
pytest.fail("package 'testpkg' not found in pip list result")


def test_list_missing_metadata_warning(script: PipTestEnvironment) -> None:
"""
Test that a warning is shown when a dist-info directory is missing the
METADATA file.
"""
# Create a test package and create .dist-info dir without METADATA file
pkg_path = create_test_package_with_setup(script, name="testpkg", version="1.0")
dist_info_path = pkg_path / "testpkg-1.0.dist-info"
dist_info_path.mkdir()
dist_info_path.joinpath("RECORD").write_text("")

# List should show a warning about the missing METADATA file
result = script.pip("list", expect_stderr=True)
assert "WARNING: Skipping" in result.stderr
assert (
"due to invalid dist-info directory: missing `METADATA` file" in result.stderr
)
Loading