python-poetry / tomlkit

Style-preserving TOML library for Python
MIT License
693 stars 98 forks source link

Empty table dumped when number of subtables > 1 #377

Closed pelson closed 2 months ago

pelson commented 2 months ago

With the following code and tomlkit 0.13:

import tomlkit

doc = tomlkit.document()

def add_table(parent, name):
    parent.add(name, tomlkit.table())
    return parent[name]

root = add_table(doc, 'root')

first = add_table(root, "first")

print(">>> WITH A SINGLE TABLE:")
print(tomlkit.dumps(doc))
print()

second = add_table(root, "second")

print(">>> WITH A DOUBLE TABLE:")
print(tomlkit.dumps(doc))

You get:

>>> WITH A SINGLE TABLE:
[root.first]

>>> WITH A DOUBLE TABLE:
[root]
[root.first]

[root.second]

Notice that in the second example, we get an empty [root] table, whereas in the first case we do not. In both cases, I would not expect the table to be shown (and perhaps, none of the tables should be shown...).

pelson commented 2 months ago

Thanks for the quick fix! :+1: