Skip to content

Commit

Permalink
Fix warning message for missing METADATA file
Browse files Browse the repository at this point in the history
Fixes #12446

Update the warning message for missing `METADATA` file in dist-info directories.

* Update the warning message in `src/pip/_internal/metadata/importlib/_compat.py` to differentiate between a missing `METADATA` file and other metadata issues.
* Add a check for the existence of the `METADATA` file in the `get_dist_canonical_name` function.
* Add a test case in `tests/functional/test_list.py` to verify the new warning message for a missing `METADATA` file.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/pypa/pip/issues/12446?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
anuk909 committed Dec 18, 2024
1 parent 3b91f42 commit 22c094e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/pip/_internal/metadata/importlib/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,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").exists():
raise BadMetadata(dist, reason="missing `METADATA` file")
raise BadMetadata(dist, reason="invalid metadata entry 'name'")
return canonicalize_name(name)
16 changes: 16 additions & 0 deletions tests/functional/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,3 +751,19 @@ 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

0 comments on commit 22c094e

Please sign in to comment.