Skip to content
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

Fix unnecessary newline addition on sub-tables #70

Merged
merged 4 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/pyproject_fmt/formatter/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from tomlkit.container import OutOfOrderTableProxy
from tomlkit.items import (
AbstractTable,
AoT,
Array,
Comment,
Item,
Expand Down Expand Up @@ -108,8 +109,15 @@ def sorted_array(

def ensure_newline_at_end(body: Table) -> None:
content = body
while content.value.body and isinstance(content.value.body[-1][1], Table):
content = content.value.body[-1][1]
while True:
if isinstance(content, AoT) and content.value and isinstance(content[-1], (AoT, Table)):
content = content[-1]
elif isinstance(content, Table) and content.value.body and isinstance(content.value.body[-1][1], (AoT, Table)):
content = content.value.body[-1][1]
else: # pragma: no cover
# coverage has a bug on python < 3.10, seeing this line as uncovered
# https://github.com/nedbat/coveragepy/issues/1480
break
whitespace = Whitespace("\n")
insert_body = content.value.body
if insert_body and isinstance(insert_body[-1][1], Whitespace):
Expand Down
37 changes: 37 additions & 0 deletions tests/formatter/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,40 @@ def test_tools_ordering(fmt: Fmt) -> None:
a = 0
"""
fmt(fmt_tools, content, expected)


def test_sub_table_newline(fmt: Fmt) -> None:
content = """
[tool.mypy]
a = 0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this newline too here 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see why you did not do it. 🤔 If you remove this line this patch will not add the newline in.

[[tool.mypy.overrides]]
a = 1
[tool.something-else]
b = 0
"""
expected = """
[tool.mypy]
a = 0

[[tool.mypy.overrides]]
a = 1

[tool.something-else]
b = 0
"""
fmt(fmt_tools, content, expected)


def test_sub_table_no_op(fmt: Fmt) -> None:
content = """
[tool.mypy]
a = 0

[[tool.mypy.overrides]]
a = 1

[tool.something-else]
b = 0
"""
fmt(fmt_tools, content, content)
1 change: 1 addition & 0 deletions whitelist.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Ao
autoclass
autodoc
canonicalize
Expand Down