Skip to content

Commit

Permalink
Fix invalid newline inside inline-table (#12)
Browse files Browse the repository at this point in the history
Co-authored-by: Bernát Gábor <[email protected]>
  • Loading branch information
abravalheri and gaborbernat authored Mar 1, 2022
1 parent f23690f commit 7e56eb2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
changelog
=========

v0.3.2 (2022-03-01)
-------------------
* Fix invalid newline inside inline-table - by :user:`abravalheri`.

v0.3.1 (2022-02-27)
-------------------
* Better handling of PEP-508 dependencies by using the ``packaging`` module instead of the our own parsing logic - by
Expand Down
2 changes: 1 addition & 1 deletion src/pyproject_fmt/formatter/build_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def fmt_build_system(parsed: TOMLDocument, conf: Config) -> None:
if system is not None:
normalize_pep508_array(cast(Optional[Array], system.get("requires")), conf.indent)
sorted_array(cast(Optional[Array], system.get("backend-path")), indent=conf.indent)
order_keys(system.value.body, ("build-backend", "requires", "backend-path"))
order_keys(system, ("build-backend", "requires", "backend-path"))


__all__ = [
Expand Down
10 changes: 5 additions & 5 deletions src/pyproject_fmt/formatter/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ def fmt_project(parsed: TOMLDocument, conf: Config) -> None:
opt_deps = cast(Table, project["optional-dependencies"])
for value in opt_deps.values():
normalize_pep508_array(cast(Array, value), conf.indent)
order_keys(opt_deps.value.body, (), sort_key=lambda k: k[0])
order_keys(opt_deps, (), sort_key=lambda k: k[0])

for of_type in ("scripts", "gui-scripts", "entry-points", "urls"):
if of_type in project:
table = cast(Table, project[of_type])
order_keys(table.value.body, (), sort_key=lambda k: k[0])
order_keys(table, (), sort_key=lambda k: k[0])

if "entry-points" in project: # order entry points sub-table
entry_points = cast(Table, project["entry-points"])
order_keys(entry_points.value.body, (), sort_key=lambda k: k[0])
order_keys(entry_points, (), sort_key=lambda k: k[0])
for entry_point in entry_points.values():
order_keys(entry_point.value.body, (), sort_key=lambda k: k[0])
order_keys(entry_point, (), sort_key=lambda k: k[0])

# license: Optional[Union[str, LicenseTableLegacy]]
# license_files: Optional[LicenseFilesTable] = Field(alias="license-files")
Expand All @@ -51,7 +51,7 @@ def fmt_project(parsed: TOMLDocument, conf: Config) -> None:
# handle readme table

order_keys(
project.value.body,
project,
(
"name",
"version",
Expand Down
19 changes: 16 additions & 3 deletions src/pyproject_fmt/formatter/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@
from dataclasses import dataclass, field
from typing import Any, Callable, Sequence

from tomlkit.items import Array, Comment, Item, Key, String, Trivia, Whitespace
from tomlkit.items import (
AbstractTable,
Array,
Comment,
Item,
Key,
String,
Table,
Trivia,
Whitespace,
)

if sys.version_info >= (3, 8): # pragma: no cover (py38+)
from typing import Protocol
Expand All @@ -23,10 +33,11 @@ def __gt__(self, __other: Any) -> bool: # noqa: U101


def order_keys(
body: list[tuple[Key | None, Item]],
table: AbstractTable,
to_pin: Sequence[str] | None = None,
sort_key: None | Callable[[tuple[str, tuple[Key, Item]]], SupportsDunderLT | SupportsDunderGT] = None,
) -> None:
body = table.value.body
entries = {i.key: (i, v) for (i, v) in body if isinstance(i, Key)}
body.clear()

Expand All @@ -39,7 +50,9 @@ def order_keys(
body.extend(entries.values())
else:
body.extend(v for k, v in sorted(entries.items(), key=sort_key))
body.append((None, Whitespace("\n"))) # add trailing newline to separate

if isinstance(table, Table):
body.append((None, Whitespace("\n"))) # add trailing newline to separate


@dataclass
Expand Down
6 changes: 2 additions & 4 deletions tests/formatter/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ def test_entry_points(fmt: Fmt) -> None:
"""
expected = """
[project.entry-points]
alpha = {"A.A" = "a",B = "b"
}
beta = {C = "c",D = "d"
}
alpha = {"A.A" = "a",B = "b"}
beta = {C = "c",D = "d"}
"""
fmt(fmt_project, start, expected)

0 comments on commit 7e56eb2

Please sign in to comment.