Skip to content

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

@Jonxslays

Description

@Jonxslays

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

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