toml icon indicating copy to clipboard operation
toml copied to clipboard

[BUG] empty dict in array list will not be parsed

Open yihengli opened this issue 1 year ago • 2 comments

a1 = {"a": [{"name": "bob", "kwargs": {}}]}
print(toml.dumps(a1))

What I got

[[a]]
name = "bob"

I am expecting to have

[[a]]
name = "bob"
[[a.kwargs]]

Latest toml and python=3.12

Not sure if this is the intended behavior

yihengli avatar Apr 26 '24 20:04 yihengli

I don't think this one can be put down to toml's strict TOML 0.5.0 support. The 0.5.0 ABNF toml-0.5.0.zip specially caters for empty inline tables:

inline-table-keyvals = [ inline-table-keyvals-non-empty ]

This isn't the only issue with toml's code for array tables - it needs work. See #439 In the mean time, other toml writers are available.

Reproduced:

>>> import tomli_w, tomlkit, toml_tools, toml
>>> a1 = {"a": [{"name": "bob", "kwargs": {}}]}
>>> for module in (tomli_w, tomlkit, toml_tools, toml):
...   print(module.__name__); print(module.dumps(a1))
...
tomli_w
a = [
    { name = "bob", kwargs = {} },
]

tomlkit
[[a]]
name = "bob"

[a.kwargs]

toml_tools
a = [
    { name = "bob", kwargs = {} },
]

toml
[[a]]
name = "bob"

JamesParrott avatar May 02 '24 14:05 JamesParrott

Having said that, toml will correctly parse a sub-array-table, and give your intended output:

>>> a2 = {"a": [{"name": "bob", "kwargs": [{}]}]}
>>> for module in (tomli_w, tomlkit, toml_tools, toml):
...   print(module.__name__); print(module.dumps(a2))
...
tomli_w
[[a]]
name = "bob"
kwargs = [
    {},
]

tomlkit
[[a]]
name = "bob"

[[a.kwargs]]

toml_tools
[[a]]
name = "bob"
kwargs = [
    {},
]

toml
[[a]]
name = "bob"
[[a.kwargs]]

JamesParrott avatar May 02 '24 14:05 JamesParrott