diff --git a/tests/test_toml_document.py b/tests/test_toml_document.py index 6f26e206..faadae41 100644 --- a/tests/test_toml_document.py +++ b/tests/test_toml_document.py @@ -1059,3 +1059,16 @@ def test_build_table_with_dotted_key(): assert json.loads(json.dumps(doc)) == { "a": {"b": {"c": 1, "d": 2}, "d": {"e": 3}, "c": {"foo": "bar"}} } + + +def test_parse_subtables_no_extra_indent(): + expected = """\ +[a] + [a.b.c] + foo = 1 + + [a.b.d] + bar = 2 +""" + doc = parse(expected) + assert doc.as_string() == expected diff --git a/tomlkit/items.py b/tomlkit/items.py index 77f49869..74c3e785 100644 --- a/tomlkit/items.py +++ b/tomlkit/items.py @@ -2,6 +2,7 @@ import abc import copy +import dataclasses import re import string @@ -303,32 +304,23 @@ def __len__(self): return len(self.value) +@dataclasses.dataclass class Trivia: """ Trivia information (aka metadata). """ - def __init__( - self, - indent: str = None, - comment_ws: str = None, - comment: str = None, - trail: str = None, - ) -> None: - # Whitespace before a value. - self.indent = indent or "" - # Whitespace after a value, but before a comment. - self.comment_ws = comment_ws or "" - # Comment, starting with # character, or empty string if no comment. - self.comment = comment or "" - # Trailing newline. - if trail is None: - trail = "\n" - - self.trail = trail + # Whitespace before a value. + indent: str = "" + # Whitespace after a value, but before a comment. + comment_ws: str = "" + # Comment, starting with # character, or empty string if no comment. + comment: str = "" + # Trailing newline. + trail: str = "\n" def copy(self) -> Trivia: - return type(self)(self.indent, self.comment_ws, self.comment, self.trail) + return dataclasses.replace(self) class KeyType(Enum): diff --git a/tomlkit/parser.py b/tomlkit/parser.py index 6a2ee515..db5aae1c 100644 --- a/tomlkit/parser.py +++ b/tomlkit/parser.py @@ -955,7 +955,7 @@ def _parse_table( # So we have to create the parent tables table = Table( Container(True), - Trivia(indent, cws, comment, trail), + Trivia("", cws, comment, trail), is_aot and name_parts[0] in self._aot_stack, is_super_table=True, name=name_parts[0].key,