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

adding and removing items from multi-line arrays does not preserve "end-of-array" style #216

Closed
decathorpe opened this issue Jul 29, 2022 · 0 comments · Fixed by #218
Closed

Comments

@decathorpe
Copy link

decathorpe commented Jul 29, 2022

It looks like adding or removing items from multi-line arrays does not respect the existing formatting of the "end of the array", i.e. the placement of trailing commas (if any) and closing "]" is not preserved.

  1. Appending an element to an existing multi-line array with a trailing comma after the last item results in an array without a trailing comma after the last item:
import tomlkit

test = """\
hello = [
    "one",
    "two",
    "three",
]
"""

expected = """\
hello = [
    "one",
    "two",
    "three",
    "four",
]
"""

actual = """\
hello = [
    "one",
    "two",
    "three",
    "four"
]
"""

data = tomlkit.parse(test)
data["hello"].append("four")

tomlkit.dumps(data) == expected  # False
tomlkit.dumps(data) == actual  # True
  1. Removing any element (i.e. this is not specific to removing the last element) from a multi-line array moves the closing "]" to the end of the previous line, even if it was on its own line before:
import tomlkit

test = """\
hello = [
    "one",
    "two",
    "three",
]
"""

expected = """\
hello = [
    "one",
    "two",
]
"""

actual = """\
hello = [
    "one",
    "two"]
"""

data = tomlkit.parse(test)
data["hello"].remove("three")

tomlkit.dumps(data) == expected  # False
tomlkit.dumps(data) == actual  # True

Not only is the placement of the closing "]" not preserved, but it appears to be "blindly" moved to the end of the previous line.
This can actually produce invalid TOML if the previous line contains a comment (already filed as a separate issue: #213 ).


Tested with tomlkit 0.11.1.

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.

1 participant