Skip to content

Validate keypath separators in update() with an iterable of pairs - #672

Open
imsurya26 wants to merge 2 commits into
fabiocaccamo:mainfrom
imsurya26:fix/update-pairs-keypath-check
Open

imsurya26 wants to merge 2 commits into
fabiocaccamo:mainfrom
imsurya26:fix/update-pairs-keypath-check

Conversation

@imsurya26

Copy link
Copy Markdown

Describe your changes

update() only validates keys that contain the keypath separator when its argument is a dict, so a list of pairs (or any other iterable/mapping that isn't a dict) skips the check:

>>> b = benedict()
>>> b.update({"a.b": 1})
ValueError: Key should not contain keypath separator '.', found: 'a.b'.
>>> b.update([("a.b", 1)])   # accepted
>>> b["a.b"]
KeyError: "Invalid keys: ['a', 'b']"

The second call stores the raw key "a.b", which can't be read back with keypaths. The cause is that keypath_util.check_keys() returns early for anything that isn't a dict. KeypathDict.update() now converts a non-dict argument with dict(other) before the check, so pairs, generators and other mappings raise the same ValueError as a dict. Dict arguments are passed through unchanged (no extra copy).

Unchanged: update() with no arguments, **kwargs, keypath_separator=None, benedict arguments (also in pointer mode), and the TypeError for invalid arguments such as update(5).

Related issue

None, found while testing update() with different argument types.

Checklist before requesting a review

  • I have performed a self-review of my code.
  • I have added tests for the proposed changes.
  • I have run the tests and there are not errors.

New test_update_with_pairs_with_separator_in_keys fails without the change and passes with it. tests/dicts/keypath/test_keypath_dict.py and tests/dicts/test_benedict.py pass, except three yaml/xml tests that fail the same way without the change because I don't have those extras installed. ruff check / ruff format --check clean.

KeypathDict.update() checks that keys don't contain the keypath separator, but check_keys() only inspects dicts. update([('a.b', 1)]) therefore stored the raw key 'a.b', which can't be read back afterwards (b['a.b'] raises KeyError), while update({'a.b': 1}) raises ValueError. Convert non-dict arguments to a dict before the check.
@manav4u

manav4u commented Nov 22, 2026

Copy link
Copy Markdown

The bad-key check works, but could dict(other) be skipped when keypath_separator=None? Validation is disabled there, so eager conversion changes existing generator updates:

from benedict import benedict

d = benedict(keypath_separator=None)

def pairs():
    yield ("a", 1)
    yield ("b", d["a"] + 1)

d.update(pairs())
print(d)

This prints {'a': 1, 'b': 2} on main, but raises KeyError: 'a' on 14ce275 because the second pair is read before the first reaches d.

Locally, if self._keypath_separator and not isinstance(other, dict): preserves this case and still rejects dotted keys in pairs when the separator is enabled. The guarded tree passes the existing 991-test unittest suite, Ruff lint/format and mypy on Linux CPython 3.13.18. No hosted-CI claim.

AI-assisted investigation and review.

With keypath_separator=None there are no keys to check, so keep
passing the iterable straight to dict.update(). Converting it first
broke generators that read values added earlier in the same update.
@imsurya26

Copy link
Copy Markdown
Author

Thanks, you are right. Fixed in f317cc4 with the guard you suggested: the pairs are only converted to a dict when keypath_separator is set, so with keypath_separator=None the iterable goes straight to dict.update() again. I added a test for your generator example.

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.

2 participants