Skip to content

fix(items): keep inline entries before AoT and table sections when sorting keys - #730

Open
ghanashyam03 wants to merge 1 commit into
python-poetry:masterfrom
ghanashyam03:fix/sort-keys-aot-order-729
Open

ghanashyam03 wants to merge 1 commit into
python-poetry:masterfrom
ghanashyam03:fix/sort-keys-aot-order-729

Conversation

@ghanashyam03

Copy link
Copy Markdown

Summary

  • Added _opens_container_section in tomlkit/items.py and updated _sort_container_body to classify AoT and section-opening Table items separately from inline values (InlineTable, scalars, and inline dotted-key super tables).
  • Added regression tests in tests/test_api.py covering AoT inside tables and nested AoTs, mixed AoT/sub-table ordering, inline tables, and dotted keys under dumps(..., sort_keys=True).

Motivation

Previously, _sort_container_body used isinstance(value, dict) to push section headers after inline entries. Because AoT subclasses _CustomList rather than dict, and InlineTable / inline dotted-key super tables subclass dict, sorting could place [[aot]] or [table] headers before inline keys and move parent entries into the following section.

Fixes #729.

Testing

  • ruff check tomlkit tests/test_api.py (passed)
  • ruff format --check tomlkit/items.py tests/test_api.py (passed)
  • python -m pytest -p no:remotedata -q tests (1485 passed)

@human-attestation

Copy link
Copy Markdown

🛡️ Human Attestation required. A human needs to confirm this PR once before it can merge.

Attest this PR — sign in with GitHub, pass the challenge, and confirm one of the statements.

@Giacomo117 Giacomo117 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.

Nice catch, this one actually changes data silently, so it's a good one to fix.

I tried it on top of current master: merges cleanly, full suite passes, and the new test fails without the change. I also threw a few more shapes at sort_keys=True (sub-tables, two AoTs, an AoT with its own sub-table, top-level AoT). On master most of them come back different, e.g.

[t]
z = 1
[[t.a]]
x = 2

turns into z living inside [[t.a]]. With your branch they all round-trip fine.

Only thing I'd add is a line in CHANGELOG.md.

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.

sort_keys moves a parent value into an array-of-tables element

2 participants