Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent dumps when replacing existing item with nested table. #145

Closed
abravalheri opened this issue Sep 21, 2021 · 0 comments · Fixed by #151
Closed

Inconsistent dumps when replacing existing item with nested table. #145

abravalheri opened this issue Sep 21, 2021 · 0 comments · Fixed by #151

Comments

@abravalheri
Copy link
Contributor

abravalheri commented Sep 21, 2021

I am using tomlkit to try editing an existing document while preserving the original comments and formatting as much as possible.
I noticed that when I try to replace an existing item with a more complex entry (e.g. table), the produced TOML document (via dumps) does not correspond to the underlying data structure.

The follow is a minimal example to try to reproduce this error:

>>> import tomlkit
>>> example = """\
... [a]
... x = 1 # comment
... y = 2
... z = 3
... """
>>> doc = tomlkit.loads(example)
>>> doc
{'a': {'x': 1, 'y': 2, 'z': 3}}
>>> doc['a']['y'] = {'nested': 1}
>>> text = tomlkit.dumps(doc)
>>> doc  # this is the data-structure what we expect to obtain if parsing the dumped text
{'a': {'x': 1, 'y': {'nested': 1}, 'z': 3}} 
>>> tomlkit.loads(text)   # here 'z' is wrongly under 'nested'. The correct would be 'z' under 'a'
{'a': {'x': 1, 'y': {'nested': 1, 'z': 3}}}
>>> print(text)
[a]
x = 1 # comment
[a.y]
nested = 1

z = 3

Please notice in this example we expect the key z to still be under the a table, but the dump-ed document ends up putting z inside the nested sub-table, which is inconsistent with the underlying data-structure.

I believe the expected behaviour of dumps, after the modification would be producing the following:

[a]
x = 1 # comment
z = 3
[a.y]
nested = 1

or at least

[a]
x = 1 # comment
y = { nested = 1 }
z = 3

I am using Python 3.8.0 and tomlkit 0.7.2 on Ubuntu 18.04.5 LTS

@abravalheri abravalheri changed the title Inconsistent dump when replacing existing item with nested table. Inconsistent dumps when replacing existing item with nested table. Sep 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant