tomlkit
tomlkit copied to clipboard
Unwanted newlines being added to output.
I want to group my tables:
[first]
foo = 5
bar = 20
[first.a.b.c]
alice = 10
bob = 30
[second]
foo = 3
bar = 21
[second.a.b.c]
alice = 15
bob = 3543
When I try to manually construct this, I instead get
[first]
foo = 5
bar = 20
[first.a.b.c]
alice = 10
bob = 30
[second]
foo = 3
bar = 21
[second.a.b.c]
alice = 15
bob = 3543
I expected no newlines to be added for me and explicitly called out a newline between [first.a.b.c]
table and [second]
. Newlines being added surprised me and made made me wonder if tomlkit was properly preserving the lack of newlines but it seems to.
So I created the following test case to experiment with how newlines are dealt with
import tomlkit
t = tomlkit.loads("""[first]
foo = 5
bar = 20
[first.a.b.c]
alice = 10
bob = 30
[second]
foo = 3
bar = 21
[second.a.b.c]
alice = 15
bob = 3543""")
print("Round-trip")
print("```toml")
print(tomlkit.dumps(t))
print("```")
print()
t["second"].add("extra", tomlkit.table())
t["second"].add("extra1", tomlkit.table())
three = tomlkit.table()
three["foo"] = 2
three["bar"] = 1
child = tomlkit.table()
child["alice"] = 3
child["bob"] = 10
three["child"] = child
t["three"] = three
print("Changed")
print("```toml")
print(tomlkit.dumps(t))
print("```")
print()
The output is: Round-trip
[first]
foo = 5
bar = 20
[first.a.b.c]
alice = 10
bob = 30
[second]
foo = 3
bar = 21
[second.a.b.c]
alice = 15
bob = 3543
Changed
[first]
foo = 5
bar = 20
[first.a.b.c]
alice = 10
bob = 30
[second]
foo = 3
bar = 21
[second.a.b.c]
alice = 15
bob = 3543
[second.extra]
[second.extra1]
[three]
foo = 2
bar = 1
[three.child]
alice = 3
bob = 10
I was surprised that second.extra
didn't have a newline before it but second.extra1
did. I imagine this just shows how the newlines are being auto-added but it is still surprising.