-
-
Notifications
You must be signed in to change notification settings - Fork 109
Description
Hello, thank you very much for the latest version, fixes and improvements.
I noticed that it changed the way tomlkit used to work for creating format-aware TOML files.
Previously, I was able to create a list of commented strings with the following code:
cat <<EOS | pip-run 'tomlkit==0.11.1'
import tomlkit
doc = tomlkit.document()
doc.add("project", tomlkit.table())
deps = tomlkit.array()
deps.add_line(tomlkit.string("first"), tomlkit.string("line"), comment="first comment")
deps.add_line(tomlkit.string("second_line"), comment="second comment")
deps.add_line(comment="comment-only line")
deps.add_line(indent="") # New line before closing brackets
doc["project"].add("dependencies", deps)
print(doc.as_string())
EOSwhich would generate a valid TOML string:
[project]
dependencies = [
"first", "line", # first comment
"second_line", # second comment
# comment-only line
]But now the same script results in an invalid TOML string:
cat <<EOS | pip-run 'tomlkit==0.11.2'
import tomlkit
doc = tomlkit.document()
doc.add("project", tomlkit.table())
deps = tomlkit.array()
deps.add_line(tomlkit.string("first"), tomlkit.string("line"), comment="first comment")
deps.add_line(tomlkit.string("second_line"), comment="second comment")
deps.add_line(comment="comment-only line")
deps.add_line(indent="") # New line before closing brackets
doc["project"].add("dependencies", deps)
print(doc.as_string())
EOS[project]
dependencies = [
"first", "line", # first comment
"second_line", # second comment
,# comment-only line
]I also tried without success to add a comment element directly to the array to circumvent the problem:
>>> deps.add_line(tomlkit.comment("comment-only line"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/tmp/pip-run-vav95r9r/tomlkit/items.py", line 1276, in add_line
raise ValueError(f"item type {type(it)} is not allowed in add_line")
ValueError: item type <class 'tomlkit.items.Comment'> is not allowed in add_lineIf I skip the last line (only containing a comment), the syntax error don't happen.
The syntax error seems to happen for every line that contains only a comment (it does not have to be the last line in the array).
Am I doing something wrong here? Is there any other way of achieving the same result? Or is it the case of a regression in 0.11.2?