镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Keep a space after the comma when inserting at the front of an array - #659

Open
INo-xious wants to merge 1 commit into
python-poetry:masterfrom
INo-xious:fix/array-insert-front-spacing
Open

INo-xious wants to merge 1 commit into
python-poetry:masterfrom
INo-xious:fix/array-insert-front-spacing

Conversation

@INo-xious

Copy link
Copy Markdown
Contributor

Summary

Inserting at the front of a single-line array leaves the element that used to be first glued to the new comma. This happens for parsed arrays and for arrays built with item():

import tomlkit

doc = tomlkit.parse("x = [1, 2]\n")
doc["x"].insert(0, 0)
doc.as_string()  # 'x = [0,1, 2]\n'

a = tomlkit.item([1, 2])
a.insert(0, 0)
a.as_string()  # '[0,1, 2]'

The new group takes the first element's indent, which is empty in this layout, and the displaced element keeps its empty indent. Array.insert now gives the displaced element the same default " " indent that any non-first inserted element already gets, so the result is [0, 1, 2]. Padded ([ 1, 2 ]), multiline and comma-first arrays already set an indent on their first element and render exactly as before. Insertions in the middle or at the end are unaffected.

Testing: test_insert_at_front_of_array_keeps_space_after_comma fails on the current master and passes with the change. The full suite passes on Python 3.11 (1203 passed) and 3.9 (1202 passed, 1 skipped), and pre-commit run passes on the changed files. A before/after comparison of 22 array layouts with inserts at several positions changed only the front-insert cases described above.

Agent Drafting Metadata

  • Agent: autonomous coding agent
  • Model: Claude
  • Notes: The agent found the bug, wrote the fix, the regression test and this description, and ran the checks above (the test was confirmed failing before the fix and passing after). No human reviewed it before submission.

@Wkhanex Wkhanex left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-assisted validation at 29948bb5c60d on Python 3.12.14: I checked front insertion with both 0 and a far-negative index against compact, padded, single-element, commented, and multiline arrays. All ten parsed cases preserved their expected data under tomllib; constructed arrays produced exactly [0, 1, 2]. The 26 array-focused tests passed.

The far-negative insertion is useful to retain alongside insert(0, ...): native list semantics clamp it to the front, so it should take the same displaced-first-element spacing path. I also checked a trailing comment on the former first value, which remained valid after insertion.

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 this pull request may close these issues.

2 participants