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

Removing an element from an array can lead to toml syntax error #213

Closed
Jonxslays opened this issue Jul 22, 2022 · 0 comments · Fixed by #218
Closed

Removing an element from an array can lead to toml syntax error #213

Jonxslays opened this issue Jul 22, 2022 · 0 comments · Fixed by #218

Comments

@Jonxslays
Copy link

Details

When you remove an element from an array, tomlkit then places the array closing bracket on the previous line.
If the previous line had a comment, this is now invalid toml syntax.

The most elegant solution feels like it would be to make sure that closing bracket doesn't get placed on the end of the preceding line if it was not already there.

Reproduction script

import tomlkit

# Some initial toml content with an item to be removed
INITIAL_CONTENT = """[info]
status = [
    1,  # Open
    2,  # In-progress
    3,  # Closed
    4,  # Useless-status
]
"""

# This would be perfect
EXPECTED_RESULT = """[info]
status = [
    1,  # Open
    2,  # In-progress
    3,  # Closed
      # Useless-status
]
"""

# This causes a toml syntax error
# The closing bracket is behind a comment
ACTUAL_RESULT = """[info]
status = [
    1,  # Open
    2,  # In-progress
    3,  # Closed
      # Useless-status]
"""

data = tomlkit.loads(INITIAL_CONTENT)

# Remove the unnecessary item from the status list
data["info"]["status"].pop()  # type: ignore
result = tomlkit.dumps(data)

# Succeeds
assert ACTUAL_RESULT == result, "Actual content didn't match"

# Fails
assert EXPECTED_RESULT == result, "Expected content didn't match"

System information

Python version: 3.10.4
Tomlkit version: 0.11.1
OS: Arch Linux 5.15.45-1-lts

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