Skip to content

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

@abravalheri

Description

@abravalheri

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions