Skip to content

Commit 6c8311b

Browse files
authored
Implement PEP 753 normalization for Home-Page fallback (#13242)
We treat "homepage" as a well-known project URL, using it to replace the Home-Page core metadata field if it's undefined. This patch updates the normalization logic to follow PEP 753. The logic emitting the raw project URLs was left unchanged as we don't really care for well-known vs. boring URLs here.
1 parent 459249e commit 6c8311b

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

news/13135.feature.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Use :pep:`753` "Well-known Project URLs in Metadata" normalization rules when
2+
identifying an equivalent project URL to replace a missing ``Home-Page`` field
3+
in ``pip show``.

src/pip/_internal/commands/show.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import string
23
from optparse import Values
34
from typing import Generator, Iterable, Iterator, List, NamedTuple, Optional
45

@@ -13,6 +14,13 @@
1314
logger = logging.getLogger(__name__)
1415

1516

17+
def normalize_project_url_label(label: str) -> str:
18+
# This logic is from PEP 753 (Well-known Project URLs in Metadata).
19+
chars_to_remove = string.punctuation + string.whitespace
20+
removal_map = str.maketrans("", "", chars_to_remove)
21+
return label.translate(removal_map).lower()
22+
23+
1624
class ShowCommand(Command):
1725
"""
1826
Show information about one or more installed packages.
@@ -135,13 +143,9 @@ def _get_requiring_packages(current_dist: BaseDistribution) -> Iterator[str]:
135143
if not homepage:
136144
# It's common that there is a "homepage" Project-URL, but Home-page
137145
# remains unset (especially as PEP 621 doesn't surface the field).
138-
#
139-
# This logic was taken from PyPI's codebase.
140146
for url in project_urls:
141147
url_label, url = url.split(",", maxsplit=1)
142-
normalized_label = (
143-
url_label.casefold().replace("-", "").replace("_", "").strip()
144-
)
148+
normalized_label = normalize_project_url_label(url_label)
145149
if normalized_label == "homepage":
146150
homepage = url.strip()
147151
break

tests/functional/test_show.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,8 @@ def test_show_deduplicate_requirements(script: PipTestEnvironment) -> None:
408408

409409

410410
@pytest.mark.parametrize(
411-
"project_url", ["Home-page", "home-page", "Homepage", "homepage"]
411+
"project_url",
412+
["Home-page", "home-page", "Homepage", "homepage", "home_PAGE", "home page"],
412413
)
413414
def test_show_populate_homepage_from_project_urls(
414415
script: PipTestEnvironment, project_url: str

0 commit comments

Comments
 (0)