Repository navigation
Add DottedKey to a super table gives wrong output #431
Copy link
Copy link
Closed
Description
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 = 5Expected:
[output.csv]
path = "output.csv"
[output.netcdf_scalar]
value = 5In 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.
Lines 551 to 566 in 8c963db
| 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:
Lines 123 to 126 in 8c963db
| 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) |
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
frostming commentedon Jun 27, 2025
No, the expected output should be
Furthermore, I likely won't have spare time on this, feel free to dig and contribute a fix.
deltamarnix commentedon Jun 27, 2025
Do you think it would be best to make a more automatic function for
is_super_table()and remove the whole hardcoded setting of_is_super_table?The problem is that
outputitself is now set as_is_super_table=Truein the parser, but when adding extra keys tooutput,_is_super_tableshould be reevaluated.Or would it be better update the renderer?
is_super_table()foroutputindeed returns True, but there are already some extra checks in the renderer:tomlkit/tomlkit/container.py
Lines 513 to 533 in 8c963db
It would mean that we have to revise the check:
In this case
table.is_super_table()returns True, so it continues to theorstatement. All instances are tables, and theoutputkey itself is not dotted. So it will skip rendering the header.Although
outputis a super table, we still want it to render, because... yeah, why actually? Because one of its keys is dotted? We see a similar if-statement inappend():tomlkit/tomlkit/container.py
Lines 278 to 288 in 8c963db
deltamarnix commentedon Jul 6, 2025
As a very simple test I added a test that already contains an
[output]table with a direct value and that works as expected:This is due to the fact that container:524 sees that
outputis indeed not a super table. But leaving out[output] stuff = 3makes the test fail, because it thinks output is a super table. Then it would check the types inside of the table withBut I guess that's not enough. What is the specific case I am looking for? Add an extra check for tables that contain dotted keys?
fix: Add DottedKey to a super table gives wrong output (#431) (#435)