Skip to content

Commit a766d3a

Browse files
committed
fix(#256): remove the extra indentation added when parsing
Close #2576 Signed-off-by: Frost Ming <[email protected]>
1 parent 9cf42c6 commit a766d3a

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

tests/test_toml_document.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,3 +1059,16 @@ def test_build_table_with_dotted_key():
10591059
assert json.loads(json.dumps(doc)) == {
10601060
"a": {"b": {"c": 1, "d": 2}, "d": {"e": 3}, "c": {"foo": "bar"}}
10611061
}
1062+
1063+
1064+
def test_parse_subtables_no_extra_indent():
1065+
expected = """\
1066+
[a]
1067+
[a.b.c]
1068+
foo = 1
1069+
1070+
[a.b.d]
1071+
bar = 2
1072+
"""
1073+
doc = parse(expected)
1074+
assert doc.as_string() == expected

tomlkit/items.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import abc
44
import copy
5+
import dataclasses
56
import re
67
import string
78

@@ -303,32 +304,23 @@ def __len__(self):
303304
return len(self.value)
304305

305306

307+
@dataclasses.dataclass
306308
class Trivia:
307309
"""
308310
Trivia information (aka metadata).
309311
"""
310312

311-
def __init__(
312-
self,
313-
indent: str = None,
314-
comment_ws: str = None,
315-
comment: str = None,
316-
trail: str = None,
317-
) -> None:
318-
# Whitespace before a value.
319-
self.indent = indent or ""
320-
# Whitespace after a value, but before a comment.
321-
self.comment_ws = comment_ws or ""
322-
# Comment, starting with # character, or empty string if no comment.
323-
self.comment = comment or ""
324-
# Trailing newline.
325-
if trail is None:
326-
trail = "\n"
327-
328-
self.trail = trail
313+
# Whitespace before a value.
314+
indent: str = ""
315+
# Whitespace after a value, but before a comment.
316+
comment_ws: str = ""
317+
# Comment, starting with # character, or empty string if no comment.
318+
comment: str = ""
319+
# Trailing newline.
320+
trail: str = "\n"
329321

330322
def copy(self) -> Trivia:
331-
return type(self)(self.indent, self.comment_ws, self.comment, self.trail)
323+
return dataclasses.replace(self)
332324

333325

334326
class KeyType(Enum):

tomlkit/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ def _parse_table(
955955
# So we have to create the parent tables
956956
table = Table(
957957
Container(True),
958-
Trivia(indent, cws, comment, trail),
958+
Trivia("", cws, comment, trail),
959959
is_aot and name_parts[0] in self._aot_stack,
960960
is_super_table=True,
961961
name=name_parts[0].key,

0 commit comments

Comments
 (0)