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

Change in behaviour in 0.11.2 resulting in invalid syntax, could it be a regression? #223

Closed
abravalheri opened this issue Aug 9, 2022 · 1 comment

Comments

@abravalheri
Copy link
Contributor

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())
EOS

which 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_line

If 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?

@abravalheri
Copy link
Contributor Author

Thank you very much!

capuanob pushed a commit to capuanob/tomlkit that referenced this issue Mar 1, 2023
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

No branches or pull requests

1 participant