From 6e6b304a4752b48545a949501b50372cf04cae52 Mon Sep 17 00:00:00 2001 From: Ee Durbin Date: Thu, 3 Oct 2024 19:07:27 -0400 Subject: [PATCH] Apply suggestions from code review Co-authored-by: Brett Cannon --- docs/licenses.rst | 5 ++--- src/packaging/licenses/__init__.py | 18 +++++++++--------- src/packaging/metadata.py | 4 ++-- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/docs/licenses.rst b/docs/licenses.rst index 91151d59..450f6f9f 100644 --- a/docs/licenses.rst +++ b/docs/licenses.rst @@ -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:: diff --git a/src/packaging/licenses/__init__.py b/src/packaging/licenses/__init__.py index dd7ccae2..babcc851 100644 --- a/src/packaging/licenses/__init__.py +++ b/src/packaging/licenses/__init__.py @@ -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): @@ -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 @@ -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 diff --git a/src/packaging/metadata.py b/src/packaging/metadata.py index 20cafe60..1de2baab 100644 --- a/src/packaging/metadata.py +++ b/src/packaging/metadata.py @@ -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: @@ -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`"""