Return a single key from key() for a single-element list (fixes #430)#520
Open
tienvantranspk-tech wants to merge 1 commit into
Open
Conversation
A one-element key list built a single-element DottedKey, which behaved differently from a plain key - for example, a membership test failed after appending it to a document. Return the SingleKey directly when the iterable yields exactly one key; multi-element lists are unchanged. Fixes python-poetry#430.
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Fixes creation of TOML keys so that a one-element key list behaves like a plain SingleKey (instead of a single-element DottedKey), aligning behavior with issue #430 and expected document interactions.
Changes:
- Update
tomlkit.key()to returnSingleKeywhen the iterable contains exactly one element. - Add regression coverage ensuring one-element key lists serialize and behave like plain keys, while multi-element lists remain dotted.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tomlkit/api.py | Adjusts key() behavior for single-element iterables to avoid creating single-element dotted keys. |
| tests/test_api.py | Adds a regression test covering the new single-element iterable behavior and preserves dotted behavior for multi-element lists. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
262
to
+268
| """ | ||
| if isinstance(k, str): | ||
| return SingleKey(k) | ||
| return DottedKey([SingleKey(_k) for _k in k]) | ||
| keys = [SingleKey(_k) for _k in k] | ||
| if len(keys) == 1: | ||
| return keys[0] | ||
| return DottedKey(keys) |
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.
Fixes #430.
Problem
key(["my_key"])builds a single-elementDottedKey, which behaves differently fromkey("my_key")(aSingleKey). For example:Fix
In
key(), when the iterable yields exactly one key, return thatSingleKeyinstead of wrapping it in aDottedKey. Multi-element lists are unchanged (still dotted keys).Tests
Added a regression test. The existing suite passes (333 passed, excluding the
toml-testgit submodule which is not initialised in my checkout), andruff check/ruff formatare clean.