Skip to content

Commit

Permalink
Show License-Expression if present in package metadata
Browse files Browse the repository at this point in the history
With Core Metadata 2.4 a new field, License-Expression, has been added.
If it's present, favor it over the deprecated (with PEP 639) legacy
unstructured License field.

Closes: #13112
  • Loading branch information
befeleme committed Jan 21, 2025
1 parent 3b0215f commit 41c807c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions news/13112.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prefer to display ``License-Expression`` in ``pip show`` if metadata version is at least 2.4.
9 changes: 8 additions & 1 deletion src/pip/_internal/commands/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class _PackageInfo(NamedTuple):
author: str
author_email: str
license: str
license_expression: str
entry_points: List[str]
files: Optional[List[str]]

Expand Down Expand Up @@ -161,6 +162,7 @@ def _get_requiring_packages(current_dist: BaseDistribution) -> Iterator[str]:
author=metadata.get("Author", ""),
author_email=metadata.get("Author-email", ""),
license=metadata.get("License", ""),
license_expression=metadata.get("License-Expression", ""),
entry_points=entry_points,
files=files,
)
Expand All @@ -180,13 +182,18 @@ def print_results(
if i > 0:
write_output("---")

metadata_version_tuple = tuple(map(int, dist.metadata_version.split(".")))

write_output("Name: %s", dist.name)
write_output("Version: %s", dist.version)
write_output("Summary: %s", dist.summary)
write_output("Home-page: %s", dist.homepage)
write_output("Author: %s", dist.author)
write_output("Author-email: %s", dist.author_email)
write_output("License: %s", dist.license)
if metadata_version_tuple >= (2, 4) and dist.license_expression:
write_output("License-Expression: %s", dist.license_expression)
else:
write_output("License: %s", dist.license)
write_output("Location: %s", dist.location)
if dist.editable_project_location is not None:
write_output(
Expand Down
Binary file not shown.
Binary file not shown.
43 changes: 42 additions & 1 deletion tests/functional/test_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,21 @@ def test_all_fields(script: PipTestEnvironment) -> None:
"""
Test that all the fields are present
"""
# future-compat: once pip adopts PEP 639 in pyproject.toml and
# its build backend produces metadata 2.4 or greater,
# it will display "License-Expression" rather than License
verbose = script.pip("show", "--verbose", "pip").stdout
match = re.search(r"Metadata-Version:\s(\d+\.\d+)", verbose)
if match is not None:
metadata_version = match.group(1)
metadata_version_tuple = tuple(map(int, metadata_version.split(".")))
if metadata_version_tuple >= (2, 4) and "License-Expression" in verbose:
license_str = "License-Expression"
else:
license_str = "License"
else:
license_str = "License"

result = script.pip("show", "pip")
lines = result.stdout.splitlines()
expected = {
Expand All @@ -226,7 +241,7 @@ def test_all_fields(script: PipTestEnvironment) -> None:
"Home-page",
"Author",
"Author-email",
"License",
f"{license_str}",
"Location",
"Editable project location",
"Requires",
Expand Down Expand Up @@ -410,3 +425,29 @@ def test_show_populate_homepage_from_project_urls(
result = script.pip("show", "simple", cwd=pkg_path)
lines = result.stdout.splitlines()
assert "Home-page: https://example.com" in lines


def test_show_license_expression(script: PipTestEnvironment, data: TestData) -> None:
"""
Show License-Expression if present in metadata >= 2.4.
"""
wheel_file = data.packages.joinpath("license.dist-0.1-py2.py3-none-any.whl")
script.pip("install", "--no-index", wheel_file)
result = script.pip("show", "license.dist")
lines = result.stdout.splitlines()
assert "License-Expression: MIT AND MIT-0" in lines
assert "License: The legacy license declaration" not in lines


def test_show_license_for_metadata_24(
script: PipTestEnvironment, data: TestData
) -> None:
"""
Show License if License-Expression is not there for metadata >= 2.4.
"""
wheel_file = data.packages.joinpath("license.dist-0.2-py2.py3-none-any.whl")
script.pip("install", "--no-index", wheel_file)
result = script.pip("show", "license.dist")
lines = result.stdout.splitlines()
assert "License-Expression: " not in lines
assert "License: The legacy license declaration" in lines

0 comments on commit 41c807c

Please sign in to comment.