Skip to content

Keep out-of-order table indices valid after a table is removed - #635

Open
youdie006 wants to merge 1 commit into
python-poetry:masterfrom
youdie006:fix-out-of-order-proxy-indices
Open

youdie006 wants to merge 1 commit into
python-poetry:masterfrom
youdie006:fix-out-of-order-proxy-indices

Conversation

@youdie006

@youdie006 youdie006 commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Deleting two keys through the same out-of-order table proxy raises or silently does nothing:

src = '[tool.black]\nline-length = 88\n\n[project]\nname = "x"\n\n[tool.ruff]\nselect = ["E"]\n'
doc = tomlkit.parse(src); tool = doc["tool"]
del tool["black"]; del tool["ruff"]      # IndexError: list index out of range
tomlkit.parse(src)["tool"].clear()        # IndexError

With [tool.a], [tool.b] and [tool.c] split by other tables, clear() returns but leaves [tool.b] and [tool.c] in the output. _tables_map holds positions in self._tables, and _remove_table removed the table with list.remove without shifting the later positions (list.remove also compares by equality, so it could take an equal empty table). It now removes the table by identity and shifts the later positions down by one.

test_delete_keys_through_the_same_out_of_order_table fails on master; dropping the shift or shifting the wrong side fails it too. pytest tests (1,069 passed) and pre-commit run on the changed files pass.

Agent Drafting Metadata

  • Agent: Claude Code
  • Model: Claude
  • Notes: Found by comparing edits against a tomllib model. I reviewed the change and ran the tests above.

OutOfOrderTableProxy maps each key to positions in self._tables, but
_remove_table took a table out of that list without shifting the later
positions, so a second delete through the same proxy indexed the wrong
table or ran past the end:

    tool = doc["tool"]; del tool["black"]; del tool["ruff"]  # IndexError

Remove the table by identity and shift the later positions down.
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.

1 participant