Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ jobs:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: [3.9, "3.10", 3.11, 3.12, 3.13, 3.14]
exclude:
- os: ubuntu-latest
python-version: 3.9
include:
# setup-python has no Python 3.9 build for Ubuntu 26.04.
- os: ubuntu-24.04
python-version: 3.9

steps:
- uses: actions/checkout@v6
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Fixed

- Add a missing separator when appending to an array whose last value is followed by standalone comments.

## [0.15.2] - 2026-10-16

### Changed
Expand Down
22 changes: 22 additions & 0 deletions tests/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,28 @@ def test_modify_array_with_comment() -> None:
assert doc.as_string() == "x = [\n 2\n]"


@pytest.mark.parametrize("trailing_comma", [False, True])
@pytest.mark.parametrize("operation", ["append", "extend", "insert"])
@pytest.mark.parametrize("comments", ["# trailing\n", "# first\n# trailing\n"])
def test_append_after_standalone_array_comments(
trailing_comma: bool, operation: str, comments: str
) -> None:
doc = parse("x = [\n1" + ("," if trailing_comma else "") + "\n" + comments + "]\n")
array = doc["x"]
if operation == "append":
array.append(2)
elif operation == "extend":
array.extend([2, 3])
else:
array.insert(len(array) + 5, 2)

expected = [1, 2, 3] if operation == "extend" else [1, 2]
rendered = doc.as_string()
assert array == expected
assert parse(rendered)["x"] == expected
assert comments in rendered


def test_append_to_multiline_array_with_comment() -> None:
doc = parse(
"""\
Expand Down
10 changes: 7 additions & 3 deletions tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -1675,6 +1675,10 @@ def insert(self, pos: int, value: Any) -> None: # type: ignore[override]
indent = self._value[idx].indent
if idx > 0:
last_item = self._value[idx - 1]
last_value_item = last_item
if pos > 0 and length:
# Standalone comments may follow the preceding array value.
last_value_item = self._value[self._index_map[min(pos, length) - 1]]
if indent is None:
indent = last_item.indent
if not isinstance(last_item.value, Null) and "\n" in default_indent:
Expand All @@ -1683,14 +1687,14 @@ def insert(self, pos: int, value: Any) -> None: # type: ignore[override]
comma = last_item.comma
comma_in_indent = indent is not None and "," in indent.s
if (
last_item.comma is None
and not isinstance(last_item.value, Null)
last_value_item.comma is None
and not isinstance(last_value_item.value, Null)
and not comma_in_indent
):
# Add comma to the last item to separate it from the following
# items, unless the new item's indent already starts with a
# comma (comma-first style).
last_item.comma = Whitespace(",")
last_value_item.comma = Whitespace(",")
if indent is not None and "," in indent.s:
# Comma-first style: the item that will follow the new one brings
# its own leading comma, so the new item must not add a trailing
Expand Down
Loading