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

Newline lost when updating a table #318

Closed
JulienPalard opened this issue Oct 12, 2023 · 2 comments · Fixed by #323
Closed

Newline lost when updating a table #318

JulienPalard opened this issue Oct 12, 2023 · 2 comments · Fixed by #323
Assignees

Comments

@JulienPalard
Copy link

May be related to #48.

Spotted a whitespace inconsistency between adding vs updating a table:

>>> import tomlkit
>>> d = tomlkit.document()
>>> d.setdefault("a", {})["b"] = {"foo": "bar"}
>>> d.setdefault("b", {})["b"] = {"foo": "bar"}
>>> print(tomlkit.dumps(d))  # I'm happy with the newlines ♥
[a.b]
foo = "bar"

[b.b]
foo = "bar"

>>> d = tomlkit.loads(tomlkit.dumps(d))
>>> d.setdefault("a", {})["b"] = {"foo": "baz"}
>>> print(tomlkit.dumps(d))  # Where are the newlines???
[a.b]
foo = "baz"
[b.b]
foo = "bar"
>>> tomlkit.__version__
'0.12.1'
@frostming
Copy link
Contributor

frostming commented Nov 6, 2023

What happened is, when you parse the TOML document, the newline is attached to the sub table foo = "bar".
But later you replaced the whole table with a new one, then tomlkit doesn't know it should add a newline to the end, think of the subtable doesn't know its position in the parsed tree unless it looks up for siblings of parents, which would be complex.

image

To compare, make the second table a sibling of the first one, namely:

[a.b]
foo = "bar"

[a.c]
foo = "bar"

Then the tree would look like:
image

I hope there would be more people reading the codebase and help improve this(kind of mess), instead of just opening an issue and complain why it doesn't work.

@frostming frostming self-assigned this Nov 6, 2023
frostming added a commit that referenced this issue Nov 6, 2023
Fixes #318

Signed-off-by: Frost Ming <me@frostming.com>
@JulienPalard
Copy link
Author

I hope there would be more people reading the codebase and help improve this(kind of mess), instead of just opening an issue and complain why it doesn't work.

I feel you.

I would love to have the free time to yak-shave on FOSS projects while I'm yak-shaving on FOSS projects, but this day I just ran out of time.

So instead of just shutting down my laptop I though that opening an issue to document what I've found would be better than nothing.

I was really not complaining, just documenting the thing. Sorry.

Oh while I'm here, thanks for your time on this project !

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.

2 participants