Skip to content

Fix TypeError when a keypath sets a string subkey through a list value#591

Open
santhreal wants to merge 1 commit into
fabiocaccamo:mainfrom
santhreal:fix/setitem-string-subkey-through-list
Open

Fix TypeError when a keypath sets a string subkey through a list value#591
santhreal wants to merge 1 commit into
fabiocaccamo:mainfrom
santhreal:fix/setitem-string-subkey-through-list

Conversation

@santhreal

Copy link
Copy Markdown

Setting a keypath whose parent value is a list but whose next key is a non-integer string raises a raw TypeError: list indices must be integers or slices, not str, even though the same assignment over a scalar/None value overrides it with a new dict, and reading the same keypath raises the library's own KeyError.

A list cannot hold a string key, so it is now overridden the same way a scalar is, keeping keypath assignment consistent. Valid integer indexes still address the list unchanged. The fix is at the single navigation choke point, so subset/move/unflatten are covered too.

from benedict import benedict
b = benedict({'a': [1, 2]})
b['a.x'] = 1        # was: TypeError; now: {'a': {'x': 1}}

Setting a keypath whose parent value is a list but whose next key is a
non-integer string (e.g. b['a.x'] = 1 with a=[1, 2]) raised a raw
TypeError: list indices must be integers or slices, not str, while the
same assignment over a scalar/None value overrides it with a new dict.
A list cannot hold a string key, so treat it like a scalar and override
it, keeping the behavior consistent (b['a.x'] -> {'a': {'x': 1}}). Valid
integer indexes still address the list unchanged.
@codecov

codecov Bot commented Jul 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.38%. Comparing base (a922d1a) to head (b5dd5fa).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #591   +/-   ##
=======================================
  Coverage   98.38%   98.38%           
=======================================
  Files          64       64           
  Lines        2418     2419    +1     
=======================================
+ Hits         2379     2380    +1     
  Misses         39       39           
Flag Coverage Δ
unittests 98.38% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

1 participant