Skip to content

Commit c9982f9

Browse files
committed
Fix reformat/dumps output for PEP 639
1 parent 4b9c6d3 commit c9982f9

File tree

7 files changed

+66
-3
lines changed

7 files changed

+66
-3
lines changed

pyproject_parser/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ def dumps(
405405
if "license" in toml_dict["project"] and toml_dict["project"]["license"] is not None:
406406
toml_dict["project"] = { # type: ignore[typeddict-item]
407407
**toml_dict["project"], # type: ignore[misc,arg-type]
408-
"license": toml_dict["project"]["license"].to_pep621_dict()
408+
"license": toml_dict["project"]["license"].to_pep639()
409409
}
410410

411411
if "readme" in toml_dict["project"] and toml_dict["project"]["readme"] is not None:

pyproject_parser/classes.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
# stdlib
3838
import pathlib
3939
from contextlib import suppress
40-
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Type, TypeVar
40+
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Type, TypeVar, Union
4141

4242
# 3rd party
4343
import attr
@@ -346,6 +346,23 @@ def to_pep621_dict(self) -> Dict[str, str]:
346346

347347
return as_dict
348348

349+
def to_pep639(self) -> Union[Dict[str, str], str]:
350+
"""
351+
Construct a dictionary or string representing the :class:`~.License` object,
352+
suitable for use in :pep:`639`-compatible ``pyproject.toml`` configuration.
353+
354+
:rtype:
355+
356+
.. versionadded:: 0.14.0
357+
.. seealso:: :meth:`~.Readme.from_dict`, :meth:`~.License.to_pep621_dict`
358+
.. latex:clearpage::
359+
""" # noqa: D400
360+
361+
if self.expression is not None:
362+
return self.expression
363+
364+
return self.to_pep621_dict()
365+
349366

350367
class _NormalisedName(str):
351368
"""

tests/test_cli.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
pytest.param(UNORDERED, id="UNORDERED"),
4848
pytest.param(COMPLETE_UNDERSCORE_NAME, id="COMPLETE_UNDERSCORE_NAME"),
4949
pytest.param(COMPLETE_DEPENDENCY_GROUPS, id="COMPLETE_DEPENDENCY_GROUPS"),
50+
pytest.param(
51+
f"[project]\nname = 'spam'\nlicense = 'MIT AND (Apache-2.0 OR BSD-2-Clause)'\nversion = '2020.0.0'\n",
52+
id="PEP639"
53+
),
5054
]
5155
)
5256
@pytest.mark.parametrize("show_diff", [True, False])
@@ -84,7 +88,19 @@ def test_reformat(
8488
assert result.stdout == "Reformatting 'pyproject.toml'\n"
8589

8690

87-
@pytest.mark.parametrize("toml_string", [*valid_pep621_config, *valid_buildsystem_config])
91+
@pytest.mark.parametrize(
92+
"toml_string",
93+
[
94+
*valid_pep621_config,
95+
*valid_buildsystem_config,
96+
pytest.param(f"{MINIMAL_CONFIG}\nlicense = 'GPL-3.0-or-later'\n", id="PEP639-GPL"),
97+
pytest.param(
98+
f"{MINIMAL_CONFIG}\nlicense = 'MIT AND (Apache-2.0 OR BSD-2-Clause)'\n",
99+
id="PEP639-AND-OR",
100+
),
101+
pytest.param(f"{MINIMAL_CONFIG}\nlicense = 'LicenseRef-My-Custom-License'\n", id="PEP639-CUSTOM"),
102+
]
103+
)
88104
def test_check(
89105
toml_string: str,
90106
tmp_pathplus: PathPlus,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Reformatting 'pyproject.toml'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[project]
2+
name = "spam"
3+
version = "2020.0.0"
4+
license = "MIT AND (Apache-2.0 OR BSD-2-Clause)"
5+
dynamic = []
6+
7+
[tool]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Reformatting 'pyproject.toml'
2+
--- pyproject.toml (original)
3+
+++ pyproject.toml (reformatted)
4+
@@ -1,5 +1,8 @@
5+
[project]
6+
-name = 'spam'
7+
-license = 'MIT AND (Apache-2.0 OR BSD-2-Clause)'
8+
-version = '2020.0.0'
9+
+name = "spam"
10+
+version = "2020.0.0"
11+
+license = "MIT AND (Apache-2.0 OR BSD-2-Clause)"
12+
+dynamic = []
13+
14+
+[tool]
15+
+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[project]
2+
name = "spam"
3+
version = "2020.0.0"
4+
license = "MIT AND (Apache-2.0 OR BSD-2-Clause)"
5+
dynamic = []
6+
7+
[tool]

0 commit comments

Comments
 (0)