Fix item() reordering dict keys inside inline tables - #611
Closed
afonsojanu wants to merge 1 commit into
Closed
afonsojanu wants to merge 1 commit into
afonsojanu wants to merge 1 commit into
Conversation
The list-of-dicts branch of item() builds each dict's sort key as
(isinstance(i[1], dict), i[0] if _sort_keys else 1). The closing
parenthesis only wraps i[0], so the dict-valued check stays active no
matter what _sort_keys is, quietly moving dict-valued keys to the end
even when the caller asked to keep their original order.
For a concrete [table] or array of tables this went unnoticed because
Container.append already repositions scalar keys ahead of any table
header on insertion, independent of the order item() hands it keys in.
Inline tables get no such correction, so the reordering is directly
visible there, e.g. item([1, {"a": {"x": 1}, "b": 2}]) rendered
{b = 2, a = {x = 1}} instead of preserving the original a, b order.
Move the parenthesis to match the sibling top-level dict branch a few
lines above, which already guards the whole tuple correctly. Verified
against the full existing test suite (all 378 tests still pass) plus a
new regression test covering both the default order-preserving case and
sort_keys=True still forcing dict-valued keys last.
Resolves python-poetry#546.
Contributor
|
if correct - then why check |
Contributor
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
Fixes #546.
item()'s list-of-dicts branch (the one that turns a Python list containing dicts into a TOML inline table) builds a sort key as:The closing parenthesis only wraps
i[0], soisinstance(i[1], dict)gets evaluated unconditionally regardless of_sort_keys. That quietly moves dict-valued keys to the end of the output even when the caller passed the defaultsort_keys=Falseand expected the original key order to be preserved.A prior attempt at this same one-line fix (#547) was closed over a concern that it might break how a scalar key avoids getting swallowed into a following
[table]header. I traced the actual rendering path before assuming that concern still applies here:Container.appendalready repositions scalar keys ahead of any table header at insertion time, independent of the orderitem()hands it keys in, so a concrete[table]/array-of-tables is unaffected either way:Inline tables get no such correction from
Container.append, since they render in strict insertion order, which is exactly why the bug is visible there and only there.Fix
Moves the closing parenthesis so the whole tuple is guarded by
_sort_keys, matching the sibling branch a few lines above that already does this correctly:Testing
Added
test_item_list_of_dicts_as_inline_table_keeps_key_orderintests/test_items.py, covering both the default order-preserving case andsort_keys=Truestill forcing dict-valued keys last. Confirmed it fails against the original code and passes with the fix. Full suite passes (pytest tests/ --ignore=tests/test_toml_tests.py, 379/379 — the ignored file needs a git submodule unrelated to this change).Agent Drafting Metadata