Preserve inline table key order in item() (#546)#552
Open
uttam12331 wants to merge 1 commit into
Open
Conversation
`item()` sorted dict-valued keys last in the list/tuple branch even under the
default ``sort_keys=False``, because the sort key kept ``isinstance(i[1],
dict)`` in the tuple unconditionally (unlike the dict branch). For an inline
table this silently reordered keys, e.g.
``item([[{"a": {"x": 1}, "b": 2}]])`` produced ``[[{b = 2, a = {x = 1}}]]``.
Only apply the dicts-last ordering to arrays of tables (which render as
``[[table]]`` headers, where a dict-valued key would otherwise capture the
following keys). Inline tables cannot capture, so their insertion order is now
preserved unless sorting is explicitly requested.
Closes python-poetry#546
dimbleby
reviewed
Jul 5, 2026
| # Inline tables cannot capture, so preserve insertion order unless | ||
| # explicitly sorting (matching the dict branch above). See #546. | ||
| def _sort_key(i: tuple[Any, Any]) -> Any: | ||
| return (isinstance(i[1], dict), i[0]) if _sort_keys else 1 |
Contributor
There was a problem hiding this comment.
why are you keeping dictionaries last in this branch?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #546.
item()reordered the keys of an inline table under the defaultsort_keys=False:Root cause
In the
list/tuplebranch ofitem(), the sort key wasso
isinstance(i[1], dict)stayed in the tuple unconditionally, forcing dict-valued keys last even when not sorting. The siblingdictbranch has the correct form ((isinstance(i[1], dict), i[0]) if _sort_keys else 1).Fix
Keep the dicts-last ordering only for arrays of tables (
Tableconstructor), which render as[[table]]headers where a dict-valued key would otherwise capture the following keys — this is why simply removing the dicts-last ordering (PR #547) was not acceptable. Inline tables cannot capture, so their insertion order is preserved unlesssort_keys=Trueis passed (matching thedictbranch).Verified:
[[{a = {x = 1}, b = 2}]](order preserved)_sort_keys=True:[[{b = 2, a = {x = 1}}]](still sorted, dicts-last)b = 2still emitted before the[a]sub-table headerTests
Added
test_item_inline_table_preserves_key_ordercovering all three cases. It fails onmasterand passes with this change. Full suite passes (356 passed);ruff check/ruff formatclean; added a CHANGELOG entry.