From 5ce57194563039186880d9cd5a062b3d86fb0bec Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Wed, 15 Feb 2023 22:32:29 +0000 Subject: [PATCH 1/4] Fix unnecessary newline addition on sub-tables --- src/pyproject_fmt/formatter/util.py | 10 ++++++++-- tests/formatter/test_tools.py | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/pyproject_fmt/formatter/util.py b/src/pyproject_fmt/formatter/util.py index dba1f59..5fbdd33 100644 --- a/src/pyproject_fmt/formatter/util.py +++ b/src/pyproject_fmt/formatter/util.py @@ -8,6 +8,7 @@ from tomlkit.container import OutOfOrderTableProxy from tomlkit.items import ( AbstractTable, + AoT, Array, Comment, Item, @@ -108,8 +109,13 @@ 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: + break whitespace = Whitespace("\n") insert_body = content.value.body if insert_body and isinstance(insert_body[-1][1], Whitespace): diff --git a/tests/formatter/test_tools.py b/tests/formatter/test_tools.py index 3de4cfe..5a52fe3 100644 --- a/tests/formatter/test_tools.py +++ b/tests/formatter/test_tools.py @@ -48,3 +48,17 @@ def test_tools_ordering(fmt: Fmt) -> None: a = 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) From 5b7c9af2f0d675fb2c7994c6c481c87c53902506 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Thu, 16 Feb 2023 13:39:43 +0000 Subject: [PATCH 2/4] Add test --- tests/formatter/test_tools.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/formatter/test_tools.py b/tests/formatter/test_tools.py index 5a52fe3..0497daf 100644 --- a/tests/formatter/test_tools.py +++ b/tests/formatter/test_tools.py @@ -50,6 +50,29 @@ def test_tools_ordering(fmt: Fmt) -> None: fmt(fmt_tools, content, expected) +def test_sub_table_newline(fmt: Fmt) -> None: + content = """ + [tool.mypy] + a = 0 + + [[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] From 1969a09b0381308f30c817e64eb2e951f3db0b6d Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Thu, 16 Feb 2023 13:41:38 +0000 Subject: [PATCH 3/4] Spellcheck --- whitelist.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/whitelist.txt b/whitelist.txt index d335dd8..e1097a0 100644 --- a/whitelist.txt +++ b/whitelist.txt @@ -1,3 +1,4 @@ +Ao autoclass autodoc canonicalize From 8ab3f8a893b1b254a0ae0afc2debee03871a5e06 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Sat, 25 Feb 2023 10:01:26 +0000 Subject: [PATCH 4/4] ignore coverage --- src/pyproject_fmt/formatter/util.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pyproject_fmt/formatter/util.py b/src/pyproject_fmt/formatter/util.py index 5fbdd33..32383f5 100644 --- a/src/pyproject_fmt/formatter/util.py +++ b/src/pyproject_fmt/formatter/util.py @@ -114,7 +114,9 @@ def ensure_newline_at_end(body: Table) -> None: 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: + 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