Skip to content

Commit 0a7e040

Browse files
committed
show: populate missing home-page from project-urls if possible
1 parent fb5f63f commit 0a7e040

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

news/11221.feature.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fallback to displaying "Home-page" Project-URL when the Home-Page metadata field is not set in ``pip show``.
2+
3+
The Project-URL fallback is case-insensitive and ignores any dashes or
4+
underscores that may be present.

src/pip/_internal/commands/show.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
from optparse import Values
3-
from typing import Generator, Iterable, Iterator, List, NamedTuple, Optional
3+
from typing import Dict, Generator, Iterable, Iterator, List, NamedTuple, Optional
44

55
from pip._vendor.packaging.utils import canonicalize_name
66

@@ -61,7 +61,7 @@ class _PackageInfo(NamedTuple):
6161
classifiers: List[str]
6262
summary: str
6363
homepage: str
64-
project_urls: List[str]
64+
project_urls: Dict[str, str]
6565
author: str
6666
author_email: str
6767
license: str
@@ -121,6 +121,25 @@ def _get_requiring_packages(current_dist: BaseDistribution) -> Iterator[str]:
121121

122122
metadata = dist.metadata
123123

124+
project_urls = {}
125+
for url in metadata.get_all("Project-URL", []):
126+
url_label, url = url.split(",", maxsplit=1)
127+
project_urls[url_label.strip()] = url.strip()
128+
129+
homepage = metadata.get("Home-page", "")
130+
if not homepage:
131+
# It's common that there is a "homepage" Project-URL, but Home-page
132+
# remains unset (especially as PEP 621 doesn't surface the field).
133+
#
134+
# This logic was taken from PyPI's codebase.
135+
for url_label, url in project_urls.items():
136+
normalized_label = (
137+
url_label.casefold().replace("-", "").replace("_", "")
138+
)
139+
if normalized_label == "homepage":
140+
homepage = url
141+
break
142+
124143
yield _PackageInfo(
125144
name=dist.raw_name,
126145
version=str(dist.version),
@@ -132,8 +151,8 @@ def _get_requiring_packages(current_dist: BaseDistribution) -> Iterator[str]:
132151
metadata_version=dist.metadata_version or "",
133152
classifiers=metadata.get_all("Classifier", []),
134153
summary=metadata.get("Summary", ""),
135-
homepage=metadata.get("Home-page", ""),
136-
project_urls=metadata.get_all("Project-URL", []),
154+
homepage=homepage,
155+
project_urls=project_urls,
137156
author=metadata.get("Author", ""),
138157
author_email=metadata.get("Author-email", ""),
139158
license=metadata.get("License", ""),
@@ -181,8 +200,8 @@ def print_results(
181200
for entry in dist.entry_points:
182201
write_output(" %s", entry.strip())
183202
write_output("Project-URLs:")
184-
for project_url in dist.project_urls:
185-
write_output(" %s", project_url)
203+
for label, url in dist.project_urls.items():
204+
write_output(f" {label}, {url}")
186205
if list_files:
187206
write_output("Files:")
188207
if dist.files is None:

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)