Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Brett Cannon <brett@python.org>
  • Loading branch information
ewdurbin and brettcannon authored Oct 3, 2024
1 parent 64d3647 commit 6e6b304
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
5 changes: 2 additions & 3 deletions docs/licenses.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ Reference

.. function:: canonicalize_license_expression(raw_license_expression)

This function takes a valid Python package or extra name, and returns the
normalized form of it.
This function takes a valid License-Expression, and returns the normalized form of it.

The return type is typed as :class:`NormalizedLicenseExpression`. This allows type
checkers to help require that a string has passed through this function
before use.

:param str raw_license_expression: The License-Expression to canonicalize.
:raises InvalidLicenseExpression: If the License-Expression is invalid due to and
:raises InvalidLicenseExpression: If the License-Expression is invalid due to an
invalid/unknown license identifier or invalid syntax.

.. doctest::
Expand Down
18 changes: 9 additions & 9 deletions src/packaging/licenses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@


class InvalidLicenseExpression(ValueError):
"""Raised when a license-expression string
"""Raised when a license-expression string is invalid
>>> canonicalize_license_expression("invalid")
Traceback (most recent call last):
Expand All @@ -59,19 +59,19 @@ class InvalidLicenseExpression(ValueError):

def canonicalize_license_expression(
raw_license_expression: str,
) -> str | NormalizedLicenseExpression:
if raw_license_expression == "":
) -> NormalizedLicenseExpression:
if not raw_license_expression:
message = f"Invalid license expression: {raw_license_expression!r}"
raise InvalidLicenseExpression(message)

# Pad any parentheses so tokenization can be achieved by merely splitting on
# white space.
license_expression = raw_license_expression.replace("(", " ( ").replace(")", " ) ")

licenseref_prefix = "LicenseRef-"
license_refs = {
ref.lower(): "LicenseRef-" + ref[11:]
ref.lower(): "LicenseRef-" + ref[len(licenseref_prefix):]
for ref in license_expression.split()
if ref.lower().startswith("licenseref-")
if ref.lower().startswith(licenseref_prefix.lower())
}

# Normalize to lower case so we can look up licenses/exceptions
Expand All @@ -97,11 +97,11 @@ def canonicalize_license_expression(

python_expression = " ".join(python_tokens)
try:
result = eval(python_expression)
invalid = eval(python_expression)
except Exception:
result = True
invalid = True

if result is not False:
if invalid is not False:
message = f"Invalid license expression: {raw_license_expression!r}"
raise InvalidLicenseExpression(message) from None

Expand Down
4 changes: 2 additions & 2 deletions src/packaging/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ def _process_requires_dist(
else:
return reqs

def _process_license_expression(self, value: str) -> str | None:
def _process_license_expression(self, value: str) -> NormalizedLicenseExpression | None:
try:
return licenses.canonicalize_license_expression(value)
except ValueError as exc:
Expand Down Expand Up @@ -816,7 +816,7 @@ def from_email(cls, data: bytes | str, *, validate: bool = True) -> Metadata:
""":external:ref:`core-metadata-maintainer-email`"""
license: _Validator[str | None] = _Validator()
""":external:ref:`core-metadata-license`"""
license_expression: _Validator[str | None] = _Validator(added="2.4")
license_expression: _Validator[NormalizedLicenseExpression | None] = _Validator(added="2.4")
""":external:ref:`core-metadata-license-expression`"""
license_files: _Validator[list[str] | None] = _Validator(added="2.4")
""":external:ref:`core-metadata-license-file`"""
Expand Down

0 comments on commit 6e6b304

Please # to comment.