Skip to content

Commit

Permalink
fix(#256): remove the extra indentation added when parsing
Browse files Browse the repository at this point in the history
Close #2576

Signed-off-by: Frost Ming <me@frostming.com>
  • Loading branch information
frostming committed Apr 27, 2023
1 parent 9cf42c6 commit a766d3a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
13 changes: 13 additions & 0 deletions tests/test_toml_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 11 additions & 19 deletions tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import abc
import copy
import dataclasses
import re
import string

Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tomlkit/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a766d3a

Please sign in to comment.