Skip to content

Add DottedKey to a super table gives wrong output #431

@deltamarnix

Description

@deltamarnix

I am trying to set dotted keys on an already existing super table. This seems to go wrong during the render method, because the code thinks I want to render everything as a super table.

My code:

import tomlkit

doc = tomlkit.loads("""
[output.netcdf_scalar]
value = 5
""")

output_table = doc["output"]
output_table[tomlkit.key(["csv", "path"])] = "output.csv"

print(tomlkit.dumps(doc))

Output:

csv.path = "output.csv"

[output.netcdf_scalar]
value = 5

Expected:

[output.csv]
path = "output.csv"

[output.netcdf_scalar]
value = 5

In the case of rendering the toml, it will say that the item "csv" is dotted, while "output" is not dotted, and "csv" is a super table, so it will not render "output" as prefix. But just adding the prefix will be wrong, because you actually need to at the [output.csv] table as a head, instead of writing output.csv.path.

for k, v in table.value.body:
if isinstance(v, Table):
if (
cur.strip(" ")
and not cur.strip(" ").endswith("\n")
and "\n" not in v.trivia.indent
):
cur += "\n"
if v.is_super_table():
if k.is_dotted() and not key.is_dotted():
# Dotted key inside table
cur += self._render_table(k, v)
else:
cur += self._render_table(k, v, prefix=_key)
else:
cur += self._render_table(k, v, prefix=_key)

I think that the _handle_dotted_key function is in this case not able to indicate that "csv" should not be super:

table = current = Table(Container(True), Trivia(), False, is_super_table=True)
for _name in mid:
_name._dotted = True
new_table = Table(Container(True), Trivia(), False, is_super_table=True)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions