Skip to content

Commit 5fe9c32

Browse files
committed
Defer import error for packaging.licenses in environments with packaging<24.2 (#4898)
2 parents 44303b6 + 676362d commit 5fe9c32

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

newsfragments/4898.bugfix.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Better error messages for ``packaging.licenses`` import errors in environments with ``packaging<24.2``\.
2+
The import statement was also deferred to spare users that are not using
3+
license expressions.
4+

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ core = [
102102

103103
# for distutils
104104
"jaraco.functools >= 4",
105-
"packaging",
106105
"more_itertools",
107106
]
108107

setuptools/_normalization.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
import re
7+
from typing import TYPE_CHECKING
78

89
import packaging
910

@@ -148,3 +149,31 @@ def safer_best_effort_version(value: str) -> str:
148149
# See bdist_wheel.safer_verion
149150
# TODO: Replace with only safe_version in the future (no need for best effort)
150151
return filename_component(best_effort_version(value))
152+
153+
154+
def _missing_canonicalize_license_expression(expression: str) -> str:
155+
"""
156+
Defer import error to affect only users that actually use it
157+
https://github.com/pypa/setuptools/issues/4894
158+
>>> _missing_canonicalize_license_expression("a OR b")
159+
Traceback (most recent call last):
160+
...
161+
ImportError: ...Cannot import `packaging.licenses`...
162+
"""
163+
raise ImportError(
164+
"Cannot import `packaging.licenses`."
165+
"""
166+
Setuptools>=77.0.0 requires "packaging>=24.2" to work properly.
167+
Please make sure you have a suitable version installed.
168+
"""
169+
)
170+
171+
172+
try:
173+
from packaging.licenses import (
174+
canonicalize_license_expression as _canonicalize_license_expression,
175+
)
176+
except ImportError: # pragma: nocover
177+
if not TYPE_CHECKING:
178+
# XXX: pyright is still upset even with # pyright: ignore[reportAssignmentType]
179+
_canonicalize_license_expression = _missing_canonicalize_license_expression

setuptools/dist.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from typing import TYPE_CHECKING, Any, Union
1313

1414
from more_itertools import partition, unique_everseen
15-
from packaging.licenses import canonicalize_license_expression
1615
from packaging.markers import InvalidMarker, Marker
1716
from packaging.specifiers import InvalidSpecifier, SpecifierSet
1817
from packaging.version import Version
@@ -24,6 +23,7 @@
2423
command as _, # noqa: F401 # imported for side-effects
2524
)
2625
from ._importlib import metadata
26+
from ._normalization import _canonicalize_license_expression
2727
from ._path import StrPath
2828
from ._reqs import _StrOrIter
2929
from .config import pyprojecttoml, setupcfg
@@ -423,7 +423,7 @@ def _finalize_license_expression(self) -> None:
423423
license_expr = self.metadata.license_expression
424424
if license_expr:
425425
str_ = _static.Str if _static.is_static(license_expr) else str
426-
normalized = str_(canonicalize_license_expression(license_expr))
426+
normalized = str_(_canonicalize_license_expression(license_expr))
427427
if license_expr != normalized:
428428
InformationOnly.emit(f"Normalizing '{license_expr}' to '{normalized}'")
429429
self.metadata.license_expression = normalized

0 commit comments

Comments
 (0)