Skip to content

Commit 31437d6

Browse files
authored
Merge pull request #12569 from ichard26/populate_homepage_from_project_urls
show: populate missing home-page from project-urls if possible
2 parents 2f49e8f + e0cddaa commit 31437d6

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

news/11221.feature.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Display the Project-URL value under key "Home-page" in ``pip show`` when the Home-Page metadata field is not set.
2+
3+
The Project-URL key detection is case-insensitive, and ignores any dashes and underscores.

src/pip/_internal/commands/show.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,22 @@ def _get_requiring_packages(current_dist: BaseDistribution) -> Iterator[str]:
121121

122122
metadata = dist.metadata
123123

124+
project_urls = metadata.get_all("Project-URL", [])
125+
homepage = metadata.get("Home-page", "")
126+
if not homepage:
127+
# It's common that there is a "homepage" Project-URL, but Home-page
128+
# remains unset (especially as PEP 621 doesn't surface the field).
129+
#
130+
# This logic was taken from PyPI's codebase.
131+
for url in project_urls:
132+
url_label, url = url.split(",", maxsplit=1)
133+
normalized_label = (
134+
url_label.casefold().replace("-", "").replace("_", "").strip()
135+
)
136+
if normalized_label == "homepage":
137+
homepage = url.strip()
138+
break
139+
124140
yield _PackageInfo(
125141
name=dist.raw_name,
126142
version=str(dist.version),
@@ -132,8 +148,8 @@ def _get_requiring_packages(current_dist: BaseDistribution) -> Iterator[str]:
132148
metadata_version=dist.metadata_version or "",
133149
classifiers=metadata.get_all("Classifier", []),
134150
summary=metadata.get("Summary", ""),
135-
homepage=metadata.get("Home-page", ""),
136-
project_urls=metadata.get_all("Project-URL", []),
151+
homepage=homepage,
152+
project_urls=project_urls,
137153
author=metadata.get("Author", ""),
138154
author_email=metadata.get("Author-email", ""),
139155
license=metadata.get("License", ""),

tests/functional/test_show.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import re
44
import textwrap
55

6+
import pytest
7+
68
from pip import __version__
79
from pip._internal.commands.show import search_packages_info
810
from pip._internal.utils.unpacking import untar_file
@@ -387,3 +389,23 @@ def test_show_deduplicate_requirements(script: PipTestEnvironment) -> None:
387389
result = script.pip("show", "simple", cwd=pkg_path)
388390
lines = result.stdout.splitlines()
389391
assert "Requires: pip" in lines
392+
393+
394+
@pytest.mark.parametrize(
395+
"project_url", ["Home-page", "home-page", "Homepage", "homepage"]
396+
)
397+
def test_show_populate_homepage_from_project_urls(
398+
script: PipTestEnvironment, project_url: str
399+
) -> None:
400+
pkg_path = create_test_package_with_setup(
401+
script,
402+
name="simple",
403+
version="1.0",
404+
project_urls={project_url: "https://example.com"},
405+
)
406+
script.run("python", "setup.py", "egg_info", expect_stderr=True, cwd=pkg_path)
407+
script.environ.update({"PYTHONPATH": pkg_path})
408+
409+
result = script.pip("show", "simple", cwd=pkg_path)
410+
lines = result.stdout.splitlines()
411+
assert "Home-page: https://example.com" in lines

0 commit comments

Comments
 (0)