item() reorders keys (dict-valued before scalar) even with the default sort_keys=False
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
Research direction
Start at the items.item() entry point and run the reproduction from the issue with tomlkit 0.15.0 or the referenced master revision. Inspect the list-of-dicts sorting branch and verify that the default sort_keys=False preserves the input order, with the [a] table emitted before b = 2.
Written by the indexing model from the issue text.
Description
Summary
When tomlkit.items.item() builds a value from a Python list of dicts, its sort key has a misplaced parenthesis:
key=lambda i: (isinstance(i[1], dict), i[0] if _sort_keys else 1). The if _sort_keys else 1 guards only i[0], so the isinstance(i[1], dict) term stays active — dict-valued keys are forced after scalar-valued keys even when sort_keys=False. The sibling top-level branch guards the whole tuple correctly: (isinstance(i[1], dict), i[0]) if _sort_keys else 1.
Reproduction
from tomlkit.items import item
print(item([{'a': {'x': 1}, 'b': 2}]).as_string())
# b = 2
#
# [a]
# x = 1
Input key order is a, b, but b is emitted before the [a] table.
Expected
With the default sort_keys=False, key order is preserved: the [a] table, then b = 2.
Actual
b = 2 is emitted before [a] — keys silently reordered.
Fix sketch
Guard the whole sort tuple with _sort_keys, matching the top-level dict branch.
Environment
tomlkit 0.15.0 (master @ 43668dd), Python 3.12.
- Dominant language
- Python
- Stars
- 850
- Forks
- 163
- Avg merge
- 13m
- Merged PRs (30d)
- 2
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python-poetry/tomlkit
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
python-poetry/tomlkit#603 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 52/100
python-poetry/tomlkit#580 · 1 comment ·
-
Difficulty 3/5 1-2 days Newbie friendliness 72/100
python-poetry/tomlkit#577 ·
-
Difficulty 5/5 Over a week Newbie friendliness 20/100
python-poetry/tomlkit#574 · 5 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
python-poetry/tomlkit#571 ·
All issues in python-poetry/tomlkit
Similar issues
-
triage/confirmed
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
agentscope-ai/agentscope#2775 ·
-
comp/desktop P3 type/bug
Difficulty 1/5 Under an hour Newbie friendliness 92/100
NousResearch/hermes-agent#118866 ·
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 90/100
apache/cloudstack#14222 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100