Skip to content

Commit d0085c1

Browse files
authored
Merge pull request #68 from Materials-Data-Science-and-Informatics/fix/setuptools_license
fix setuptools license model
2 parents 0facd95 + defa696 commit d0085c1

4 files changed

Lines changed: 23 additions & 9 deletions

File tree

src/somesy/pyproject/models.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ class License(BaseModel):
122122

123123
model_config = dict(validate_assignment=True)
124124

125-
file: Optional[Path]
126-
text: Optional[LicenseEnum]
125+
file: Optional[Path] = None
126+
text: Optional[LicenseEnum] = None
127127

128128
@model_validator(mode="before")
129129
@classmethod
@@ -161,9 +161,7 @@ class SetuptoolsConfig(BaseModel):
161161
]
162162
description: str
163163
readme: Optional[Union[Path, List[Path], File]] = None
164-
license: Optional[Union[LicenseEnum, List[LicenseEnum]]] = Field(
165-
None, description="An SPDX license identifier."
166-
)
164+
license: Optional[License] = Field(None, description="An SPDX license identifier.")
167165
authors: Optional[List[STPerson]] = None
168166
maintainers: Optional[List[STPerson]] = None
169167
keywords: Optional[Set[str]] = None

src/somesy/pyproject/writer.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from rich.pretty import pretty_repr
1010
from tomlkit import load
1111

12-
from somesy.core.models import Person
12+
from somesy.core.models import Person, ProjectMetadata
1313
from somesy.core.writer import ProjectMetadataWriter
1414

1515
from .models import PoetryConfig, SetuptoolsConfig
@@ -122,6 +122,7 @@ def __init__(self, path: Path):
122122
mappings = {
123123
"homepage": ["urls", "homepage"],
124124
"repository": ["urls", "repository"],
125+
"license": ["license", "text"],
125126
}
126127
super().__init__(
127128
path, section=section, direct_mappings=mappings, model_cls=SetuptoolsConfig
@@ -146,6 +147,19 @@ def _to_person(person_obj) -> Person:
146147
}
147148
)
148149

150+
def sync(self, metadata: ProjectMetadata) -> None:
151+
"""Sync metadata with pyproject.toml file and fix license field."""
152+
super().sync(metadata)
153+
154+
# if license field has both text and file, remove file
155+
if (
156+
self._get_property(["license", "file"]) is not None
157+
and self._get_property(["license", "text"]) is not None
158+
):
159+
# delete license file property
160+
self._data["project"]["license"].pop("file")
161+
logger.debug("Removed license file from pyproject.toml")
162+
149163

150164
# ----
151165

tests/data/pyproject.setuptools.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
name = "test-package"
33
version = "0.1.0"
44
description = "This is a test package for demonstration purposes."
5-
authors = [{"name"="John Doe", "email"="john.doe@example.com"}]
6-
license = "MIT"
5+
authors = [{ "name" = "John Doe", "email" = "john.doe@example.com" }]
6+
license = { text = "MIT" }
77
keywords = ["test", "demo", "example"]
88
classifiers = [
99
"Operating System :: POSIX :: Linux",

tests/output/test_pyproject_writer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ def assert_content_match(pyproject_file):
3838
pyproject_file.description
3939
== "This is a test package for demonstration purposes."
4040
)
41-
assert pyproject_file.license == "MIT"
41+
assert (
42+
pyproject_file.license == "MIT" or pyproject_file.license["text"] == "MIT"
43+
)
4244
assert len(pyproject_file.authors) == 1
4345

4446
# assert for both formats

0 commit comments

Comments
 (0)