diff --git a/news/11221.feature.rst b/news/11221.feature.rst new file mode 100644 index 00000000000..319cc02c4a5 --- /dev/null +++ b/news/11221.feature.rst @@ -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. diff --git a/src/pip/_internal/commands/show.py b/src/pip/_internal/commands/show.py index cfd67420d0a..b7894ce1f9c 100644 --- a/src/pip/_internal/commands/show.py +++ b/src/pip/_internal/commands/show.py @@ -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), @@ -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", ""), diff --git a/tests/functional/test_show.py b/tests/functional/test_show.py index ad139240b77..7797de9e992 100644 --- a/tests/functional/test_show.py +++ b/tests/functional/test_show.py @@ -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 @@ -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