Skip to content

Commit

Permalink
fixups from code-review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ewdurbin committed Oct 3, 2024
1 parent a65ca89 commit 1cac177
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/packaging/licenses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from __future__ import annotations

import re
from typing import NewType
from typing import NewType, cast

from packaging.licenses._spdx import EXCEPTIONS, LICENSES

Expand Down Expand Up @@ -69,7 +69,7 @@ def canonicalize_license_expression(
license_expression = raw_license_expression.replace("(", " ( ").replace(")", " ) ")
licenseref_prefix = "LicenseRef-"
license_refs = {
ref.lower(): "LicenseRef-" + ref[len(licenseref_prefix):]
ref.lower(): "LicenseRef-" + ref[len(licenseref_prefix) :]
for ref in license_expression.split()
if ref.lower().startswith(licenseref_prefix.lower())
}
Expand Down Expand Up @@ -139,4 +139,7 @@ def canonicalize_license_expression(

normalized_expression = " ".join(normalized_tokens)

return normalized_expression.replace("( ", "(").replace(" )", ")")
return cast(
NormalizedLicenseExpression,
normalized_expression.replace("( ", "(").replace(" )", ")"),
)
9 changes: 7 additions & 2 deletions src/packaging/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from . import licenses, requirements, specifiers, utils
from . import version as version_module
from .licenses import NormalizedLicenseExpression

T = typing.TypeVar("T")

Expand Down Expand Up @@ -644,7 +645,9 @@ def _process_requires_dist(
else:
return reqs

def _process_license_expression(self, value: str) -> NormalizedLicenseExpression | 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 +819,9 @@ 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[NormalizedLicenseExpression | 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 1cac177

Please sign in to comment.