toml
toml copied to clipboard
[BUG] empty dict in array list will not be parsed
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
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"
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]]