Licenses

Helper for canonicalizing SPDX License-Expression metadata as defined in PEP 639.

Reference

class packaging.licenses.NormalizedLicenseExpression

A typing.NewType of str, representing a normalized License-Expression.

exception packaging.licenses.InvalidLicenseExpression

Raised when a License-Expression is invalid.

packaging.licenses.canonicalize_license_expression(raw_license_expression)

This function takes a valid License-Expression, and returns the normalized form of it.

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

Parameters:

raw_license_expression (str) – The License-Expression to canonicalize.

Raises:

InvalidLicenseExpression – If the License-Expression is invalid due to an invalid/unknown license identifier or invalid syntax.

>>> from packaging.licenses import canonicalize_license_expression
>>> canonicalize_license_expression("mit")
'MIT'
>>> canonicalize_license_expression("mit and (apache-2.0 or bsd-2-clause)")
'MIT AND (Apache-2.0 OR BSD-2-Clause)'
>>> canonicalize_license_expression("(mit")
Traceback (most recent call last):
  ...
InvalidLicenseExpression: Invalid license expression: '(mit'
>>> canonicalize_license_expression("Use-it-after-midnight")
Traceback (most recent call last):
  ...
InvalidLicenseExpression: Unknown license: 'Use-it-after-midnight'