fix: handle non-string dict keys holding lists in keylists/keypaths#583
Merged
fabiocaccamo merged 1 commit intoJul 7, 2026
Conversation
`keylists(d, indexes=True)` raised `TypeError: unsupported operand
type(s) for +=: 'int' and 'str'` whenever a non-string dict key
(int / None / bool / tuple) held a list value.
Root cause: in `_get_keylist_for_list` the index was appended to the
parent key with `keys[-1] += f"[{key}]"`, which assumes the last parent
key is a string. A string key `"a"` became `"a[0]"`, but an int key `0`
hit `0 += "[0]"` and crashed.
This was inconsistent: the same non-string key holding a *dict* worked
fine (`keylists({0: {"x": 1}})` -> `[[0], [0, "x"]]`), and non-string
keys are explicitly supported elsewhere (see
`test_keylists_with_non_string_keys`). Only the list branch assumed
stringness.
Because `keypaths`, `match` and `benedict.keypaths()/keylists()` all
build on `keylists`, the crash surfaced through the public API too, e.g.
`benedict({0: [1, 2]}).keypaths(indexes=True)`.
Fix: stringify the parent key before appending the index
(`keys[-1] = f"{keys[-1]}[{key}]"`). String keys are unaffected
(`f"{'a'}[0]" == "a[0]"`); non-string keys now produce a consistent
stringified keypath (`0` -> `"0[0]"`, `None` -> `"None[0]"`), matching
how `keypaths` already stringifies every key via `f"{key}"`.
Added `test_keylists_with_non_string_keys_and_lists_and_indexes_included`
which fails without the fix (TypeError) and passes with it.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #583 +/- ##
=======================================
Coverage 98.32% 98.32%
=======================================
Files 64 64
Lines 2392 2392
=======================================
Hits 2352 2352
Misses 40 40
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a TypeError in keylists(..., indexes=True) when traversing list values under non-string dict keys, which also affected the public benedict.keypaths() / benedict.keylists() APIs that build on keylists.
Changes:
- Update list-index key composition to stringify the parent key before appending
"[i]", avoidingint += str(and similar) failures. - Add a regression test covering non-string keys (e.g.,
0,None) that hold list values whenindexes=True.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
benedict/core/keylists.py |
Prevents TypeError by building indexed list keys via an f-string assignment instead of += on potentially non-string keys. |
tests/core/test_keylists.py |
Adds regression coverage for non-string dict keys that hold lists when indexes=True. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
fabiocaccamo
approved these changes
Jul 7, 2026
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
keylists(d, indexes=True)raisesTypeErrorwhenever a non-string dict key (int /None/ bool / tuple) holds a list value. Becausekeypaths,match, and the publicbenedict.keypaths()/benedict.keylists()all build onkeylists, the crash also surfaces through the public API.Reproduce
Root cause
In
benedict/core/keylists.py,_get_keylist_for_listappended the list index to the parent key with:This assumes the last parent key is a string. A string key
"a"becomes"a[0]", but an int key0evaluates0 += "[0]"and crashes.The behaviour was inconsistent: the same non-string key holding a dict worked fine, and non-string keys are explicitly supported elsewhere in the module (see the existing
test_keylists_with_non_string_keys). Only the list branch assumed stringness.keylists(..., indexes=True)before{"a": [1, 2]}[['a'], ['a[0]'], ['a[1]']][['a'], ['a[0]'], ['a[1]']](unchanged){0: {"x": 1}}[[0], [0, 'x']][[0], [0, 'x']](unchanged){0: [1, 2]}[[0], ['0[0]'], ['0[1]']]{None: [1, 2]}[[None], ['None[0]'], ['None[1]']]Fix
Stringify the parent key before appending the index:
String keys are unaffected (
f"{'a'}[0]" == "a[0]"); non-string keys now produce a consistent stringified keypath (0→"0[0]",None→"None[0]"), matching howkeypathsalready stringifies every key viaf"{key}".Tests
Added
test_keylists_with_non_string_keys_and_lists_and_indexes_included, which follows the existingtest_keylists_with_non_string_keysmembership style (mixed int/str/None keylists can't be sorted for comparison).Proven to guard the bug — stash the source fix and the new test fails with the original
TypeErroratkeylists.py:26; restore the fix and it passes:Full
tests/core+tests/utilssuite: 123 passed (was 122).ruff check,ruff format --check,black --checkandmypyare all clean on the changed files (the 3 pre-existing mypy errors inserializers/*andparse_util.pyare unrelated optional-dependency stub issues, present with or without this change).Diff: +27 / −1 across
benedict/core/keylists.pyandtests/core/test_keylists.py.