Skip to content

Normalize array indices before updating formatted storage - #657

Open
xiehuanyi wants to merge 1 commit into
python-poetry:masterfrom
xiehuanyi:fix/array-index-protocol-20261014
Open

xiehuanyi wants to merge 1 commit into
python-poetry:masterfrom
xiehuanyi:fix/array-index-protocol-20261014

Conversation

@xiehuanyi

Copy link
Copy Markdown

Summary

Array assignment, deletion, and insertion accept objects implementing __index__ through their underlying list operations, but subsequently compare or look up the original object when updating the formatted array. For example, assigning with an index object returning 0 changes [1, 2] into [3, 2] and then raises TypeError, leaving as_string() as [1, 2].

Normalize scalar indices before mutation and reuse the resulting integer for the formatted storage. Normalize slice components once, in the same step/start/stop order as a list, so deleting with stateful index objects does not convert them again after changing the list. Update the affected annotations to accept SupportsIndex.

Validation: 17 new regression cases fail on the unchanged source; all 25 focused cases pass after the fix. The full suite passes on Python 3.9 (1,226 passed, one skipped) and 3.11 (1,227 passed), and all configured pre-commit hooks pass. Mypy reports the same eight existing repository errors on the original and patched trees, excluding the external toml-test submodule.

Agent Drafting Metadata

  • Agent: Codex
  • Model: GPT-6.1 Sol (xhigh)
  • Notes: Agent-assisted reproduction, implementation, tests, and PR drafting. Regression results were verified before and after the source change.

Copilot AI balanced review requested due to automatic review settings October 14, 2026 10:36

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@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 349167ab83c5 on Python 3.12.14: I compared assignment, deletion, and insertion with single-use __index__ objects at six positions (-100, -3, -1, 0, 2, 100) against native lists. Error behavior and serialized data matched in all 18 cases, including unchanged data after out-of-range assignment/deletion. Four additional forward/reverse slices also converted each supplied bound exactly once. The 50 array-focused tests passed.

The far-out-of-range cases are a useful guard for the order of normalization and mutation: rejection must leave both the list data and formatted storage untouched. I checked rendered state independently with tomllib, rather than relying only on array equality.

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.

3 participants