Skip to content

Commit 676362d

Browse files
committed
Refactor fallback for packaging.licenses
1 parent 53fc322 commit 676362d

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

setuptools/_normalization.py

+20-12
Original file line numberDiff line numberDiff line change
@@ -151,21 +151,29 @@ def safer_best_effort_version(value: str) -> str:
151151
return filename_component(best_effort_version(value))
152152

153153

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+
154172
try:
155173
from packaging.licenses import (
156174
canonicalize_license_expression as _canonicalize_license_expression,
157175
)
158-
except ImportError:
176+
except ImportError: # pragma: nocover
159177
if not TYPE_CHECKING:
160178
# XXX: pyright is still upset even with # pyright: ignore[reportAssignmentType]
161-
162-
def _canonicalize_license_expression(expression: str) -> str:
163-
# Defer import error to affect only users that actually use it
164-
# https://github.com/pypa/setuptools/issues/4894
165-
raise ImportError(
166-
"Cannot import `packaging.licenses`."
167-
"""
168-
Setuptools>=77.0.0 requires "packaging>=24.2" to work properly.
169-
Please make sure you have a suitable version installed.
170-
"""
171-
)
179+
_canonicalize_license_expression = _missing_canonicalize_license_expression

0 commit comments

Comments
 (0)