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

Comment before table causes next entry to be in table #98

Closed
jpyams opened this issue May 7, 2020 · 2 comments · Fixed by #122
Closed

Comment before table causes next entry to be in table #98

jpyams opened this issue May 7, 2020 · 2 comments · Fixed by #122

Comments

@jpyams
Copy link

jpyams commented May 7, 2020

Steps to Reproduce

In a tomlkit document, add a comment, a table, then a separate entry. Dump the document to TOML.

import tomlkit

doc = tomlkit.document()
doc.add(tomlkit.comment('Comment'))
doc['foo'] = {'name': 'test'}
doc['bar'] = 1
print(tomlkit.dumps(doc))

Expected result

bar is outside the table.

bar = 1
# Comment

[foo]
name = "test"

Actual result

bar is dumped as an entry in foo.

# Comment

[foo]
name = "test"
bar = 1

Additional notes

This behavior only happens with the first key after the table. Subsequent entries are correctly outside the table, though after the comment.

import tomlkit

doc = tomlkit.document()
doc.add(tomlkit.comment('Comment'))
doc['foo'] = {'name': 'test'}
doc['bar'] = 1
doc['baz'] = 1
print(tomlkit.dumps(doc))

gives

# Comment
baz = 1

[foo]
name = "test"
bar = 1
@jpyams
Copy link
Author

jpyams commented Sep 3, 2020

Funny how rereading the README can make life easier. It looks like when adding a comment tomlkit is expecting me to do

import tomlkit

doc = tomlkit.document()
doc['foo'] = {'name': 'test'}
doc['foo'].comment('Comment')
doc['bar'] = 1
print(tomlkit.dumps(doc))

That gives me

bar = 1
[foo] # Comment
name = "test"

Adding the comment directly to the item also feels a lot more intuitive, so I like this more than what I was doing before.

I'm leaving this issue open because I don't know if what I was doing before is expected to work or not. If what I was doing before was user error, then please go ahead and close this.

@frostming
Copy link
Contributor

I've created a fork of tomlkit v0.7.0 with the issue fixed, try it:

GitHub: https://github.com/frostming/atoml
PyPI: https://pypi.org/project/atoml

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