-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add initial support for license expression (PEP 639) #4706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
285d681
420766c
346bf17
31d8340
3744994
0d8f1f2
28baa9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Added initial support for license expression (`PEP 639 <https://peps.python.org/pep-0639/#add-license-expression-field>`_). -- by :user:`cdce8p` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,6 +58,7 @@ def apply(dist: Distribution, config: dict, filename: StrPath) -> Distribution: | |
| os.chdir(root_dir) | ||
| try: | ||
| dist._finalize_requires() | ||
| dist._finalize_license_expression() | ||
| dist._finalize_license_files() | ||
| finally: | ||
| os.chdir(current_directory) | ||
|
|
@@ -181,16 +182,19 @@ def _long_description( | |
| dist._referenced_files.add(file) | ||
|
|
||
|
|
||
| def _license(dist: Distribution, val: dict, root_dir: StrPath | None): | ||
| def _license(dist: Distribution, val: str | dict, root_dir: StrPath | None): | ||
| from setuptools.config import expand | ||
|
|
||
| if "file" in val: | ||
| # XXX: Is it completely safe to assume static? | ||
| value = expand.read_files([val["file"]], root_dir) | ||
| _set_config(dist, "license", _static.Str(value)) | ||
| dist._referenced_files.add(val["file"]) | ||
| if isinstance(val, str): | ||
| _set_config(dist, "license_expression", _static.Str(val)) | ||
| else: | ||
| _set_config(dist, "license", _static.Str(val["text"])) | ||
| if "file" in val: | ||
| # XXX: Is it completely safe to assume static? | ||
| value = expand.read_files([val["file"]], root_dir) | ||
| _set_config(dist, "license", _static.Str(value)) | ||
| dist._referenced_files.add(val["file"]) | ||
| else: | ||
| _set_config(dist, "license", _static.Str(val["text"])) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer to flatten this nested
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about this as well. However I decided to use to nesting to clearly separate the before / after PEP 639 cases. In a flattened list it just looks like they are equal which isn't the case. At some point, it might also make sense to add a DeprecationWarning for the legacy case which will be easier with the nested style. |
||
|
|
||
|
|
||
| def _people(dist: Distribution, val: list[dict], _root_dir: StrPath | None, kind: str): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| from typing import TYPE_CHECKING, Any, Union | ||
|
|
||
| from more_itertools import partition, unique_everseen | ||
| from packaging.licenses import canonicalize_license_expression | ||
| from packaging.markers import InvalidMarker, Marker | ||
| from packaging.specifiers import InvalidSpecifier, SpecifierSet | ||
| from packaging.version import Version | ||
|
|
@@ -288,6 +289,7 @@ class Distribution(_Distribution): | |
| 'long_description_content_type': lambda: None, | ||
| 'project_urls': dict, | ||
| 'provides_extras': dict, # behaves like an ordered set | ||
| 'license_expression': lambda: None, | ||
| 'license_file': lambda: None, | ||
| 'license_files': lambda: None, | ||
| 'install_requires': list, | ||
|
|
@@ -402,6 +404,31 @@ def _normalize_requires(self): | |
| (k, list(map(str, _reqs.parse(v or [])))) for k, v in extras_require.items() | ||
| ) | ||
|
|
||
| def _finalize_license_expression(self) -> None: | ||
| """Normalize license and license_expression.""" | ||
| license_expr = self.metadata.license_expression | ||
| if license_expr: | ||
| normalized = canonicalize_license_expression(license_expr) | ||
| if license_expr != normalized: | ||
| InformationOnly.emit(f"Normalizing '{license_expr}' to '{normalized}'") | ||
| self.metadata.license_expression = normalized | ||
|
|
||
| classifiers = [] | ||
| license_classifiers_found = False | ||
| for cl in self.metadata.get_classifiers(): | ||
| if not cl.startswith("License :: "): | ||
| classifiers.append(cl) | ||
| continue | ||
| license_classifiers_found = True | ||
| SetuptoolsDeprecationWarning.emit( | ||
| "License classifier are deprecated in favor of the license expression.", | ||
| f"Please remove the '{cl}' classifier.", | ||
| see_url="https://peps.python.org/pep-0639/", | ||
| due_date=(2027, 2, 17), # Introduced 2025-02-17 | ||
| ) | ||
| if license_classifiers_found: | ||
| self.metadata.set_classifiers(classifiers) | ||
|
||
|
|
||
| def _finalize_license_files(self) -> None: | ||
| """Compute names of all license files which should be included.""" | ||
| license_files: list[str] | None = self.metadata.license_files | ||
|
|
@@ -655,6 +682,7 @@ def parse_config_files( | |
| pyprojecttoml.apply_configuration(self, filename, ignore_option_errors) | ||
|
|
||
| self._finalize_requires() | ||
| self._finalize_license_expression() | ||
| self._finalize_license_files() | ||
|
|
||
| def fetch_build_eggs( | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this mean that we need a follow-up PR for the
License-Expression, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. This PR was designed so that it itself didn't require a metadata_version bump. That could / should happen at a later point once all pieces are in place. I.e. setuptools has adopted version
2.3, the license files are stored in the correct folder and license expression parsing is handled correctly.