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

show: populate missing home-page from project-urls if possible #12569

Merged
merged 3 commits into from
Apr 7, 2024
Merged
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
3 changes: 3 additions & 0 deletions news/11221.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Display the Project-URL value under key "Home-page" in ``pip show`` when the Home-Page metadata field is not set.

The Project-URL key detection is case-insensitive, and ignores any dashes and underscores.
20 changes: 18 additions & 2 deletions src/pip/_internal/commands/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ def _get_requiring_packages(current_dist: BaseDistribution) -> Iterator[str]:

metadata = dist.metadata

project_urls = metadata.get_all("Project-URL", [])
homepage = metadata.get("Home-page", "")
if not homepage:
# It's common that there is a "homepage" Project-URL, but Home-page
# remains unset (especially as PEP 621 doesn't surface the field).
#
# This logic was taken from PyPI's codebase.
for url in project_urls:
url_label, url = url.split(",", maxsplit=1)
normalized_label = (
url_label.casefold().replace("-", "").replace("_", "").strip()
)
if normalized_label == "homepage":
homepage = url.strip()
break

yield _PackageInfo(
name=dist.raw_name,
version=str(dist.version),
Expand All @@ -132,8 +148,8 @@ def _get_requiring_packages(current_dist: BaseDistribution) -> Iterator[str]:
metadata_version=dist.metadata_version or "",
classifiers=metadata.get_all("Classifier", []),
summary=metadata.get("Summary", ""),
homepage=metadata.get("Home-page", ""),
project_urls=metadata.get_all("Project-URL", []),
homepage=homepage,
project_urls=project_urls,
author=metadata.get("Author", ""),
author_email=metadata.get("Author-email", ""),
license=metadata.get("License", ""),
Expand Down
22 changes: 22 additions & 0 deletions tests/functional/test_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import re
import textwrap

import pytest

from pip import __version__
from pip._internal.commands.show import search_packages_info
from pip._internal.utils.unpacking import untar_file
Expand Down Expand Up @@ -387,3 +389,23 @@ def test_show_deduplicate_requirements(script: PipTestEnvironment) -> None:
result = script.pip("show", "simple", cwd=pkg_path)
lines = result.stdout.splitlines()
assert "Requires: pip" in lines


@pytest.mark.parametrize(
"project_url", ["Home-page", "home-page", "Homepage", "homepage"]
)
def test_show_populate_homepage_from_project_urls(
script: PipTestEnvironment, project_url: str
) -> None:
pkg_path = create_test_package_with_setup(
script,
name="simple",
version="1.0",
project_urls={project_url: "https://example.com"},
)
script.run("python", "setup.py", "egg_info", expect_stderr=True, cwd=pkg_path)
script.environ.update({"PYTHONPATH": pkg_path})

result = script.pip("show", "simple", cwd=pkg_path)
lines = result.stdout.splitlines()
assert "Home-page: https://example.com" in lines
Loading