Skip to content

Return a single key from key() for a single-element list (fixes #430)#520

Open
tienvantranspk-tech wants to merge 1 commit into
python-poetry:masterfrom
tienvantranspk-tech:fix/key-single-element-dotted
Open

Return a single key from key() for a single-element list (fixes #430)#520
tienvantranspk-tech wants to merge 1 commit into
python-poetry:masterfrom
tienvantranspk-tech:fix/key-single-element-dotted

Conversation

@tienvantranspk-tech

Copy link
Copy Markdown

Fixes #430.

Problem

key(["my_key"]) builds a single-element DottedKey, which behaves differently from key("my_key") (a SingleKey). For example:

import tomlkit
doc = tomlkit.document()
doc.append(tomlkit.key(["my_key"]), "value")
"my_key" in doc        # -> False  (key("my_key") gives True)

Fix

In key(), when the iterable yields exactly one key, return that SingleKey instead of wrapping it in a DottedKey. Multi-element lists are unchanged (still dotted keys).

Tests

Added a regression test. The existing suite passes (333 passed, excluding the toml-test git submodule which is not initialised in my checkout), and ruff check / ruff format are clean.

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.
Copilot AI review requested due to automatic review settings June 15, 2026 16:00

Copilot AI 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.

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 return SingleKey when 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 thread tomlkit/api.py
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)
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.

Different behavior for SingleKey and DottedKey with single value

2 participants