Skip to content

fix: replace sub-dict keys on assignment in NDict.__setitem__ (Fixes #397)#418

Open
rtmalikian wants to merge 1 commit into
BiomedSciAI:masterfrom
rtmalikian:fix/issue-397-ndict-setitem-replace-sub-dict
Open

fix: replace sub-dict keys on assignment in NDict.__setitem__ (Fixes #397)#418
rtmalikian wants to merge 1 commit into
BiomedSciAI:masterfrom
rtmalikian:fix/issue-397-ndict-setitem-replace-sub-dict

Conversation

@rtmalikian

Copy link
Copy Markdown

Fixes #397

Problem

When assigning a new dict to an existing key in NDict, old sub-keys that were not present in the new dict were retained, causing unexpected merge-like behavior instead of full replacement.

Reproduction:

my_dict = NDict({"f": {"a": 1, "b": 2}})
my_dict["f"] = {"b": 1000}
print(my_dict)
# Actual:   {'f.a': 1, 'f.b': 1000}  ← f.a leaked through
# Expected: {'f.b': 1000}

Root cause: NDict.__setitem__ (line 212) iterates over the new dict's keys and sets them, but never removes the old sub-keys stored under the same prefix.

Solution

Before setting the new dict's sub-keys, delete all existing _stored keys that start with key + ".". This ensures the assignment fully replaces the previous sub-dict content.

Verification

# Exact reproduction from issue
my_dict = NDict({"f": {"a": 1, "b": 2}})
my_dict["f"] = {"b": 1000}
assert dict(my_dict._stored) == {"f.b": 1000}  # PASS

# Edge cases
my_dict2 = NDict({"x": {"a": 1, "b": 2, "c": 3}})
my_dict2["x"] = {}
assert dict(my_dict2._stored) == {}  # PASS — empty dict clears all

my_dict3 = NDict()
my_dict3["z"] = {"a": 1}
assert dict(my_dict3._stored) == {"z.a": 1}  # PASS — new assignment works

my_dict4 = NDict({"f": {"a": {"x": 1}, "b": 2}})
my_dict4["f"] = {"a": {"y": 2}}
assert "f.a.x" not in my_dict4._stored  # PASS — nested replacement
assert my_dict4._stored.get("f.a.y") == 2

About the Author: Raphael Malikian — Clinical AI Solutions Architect. I specialise in building and fixing AI/ML systems for healthcare, including vector databases, RAG pipelines, and clinical NLP. If you need help with your project or think I can add value to your organisation, feel free to reach out — I'd love to connect.

📧 rtmalikian@gmail.com
🔗 GitHub: https://github.com/rtmalikian
🔗 LinkedIn: http://www.linkedin.com/in/raphael-t-malikian-mbbs-bsc-hons-71075436a


Disclosure: This code was developed with assistance from mimo-2.5-pro (Xiaomi) via Hermes Agent (Nous Research). All changes were reviewed, tested against the actual codebase, and verified for correctness.

When assigning a new dict value to an existing key in NDict, old sub-keys
that were not present in the new dict were retained, causing unexpected
merge-like behavior instead of replacement.

Fix: delete all existing sub-keys with the matching prefix before setting
the new dict's keys. This ensures full replacement semantics.

Fixes BiomedSciAI#397
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.

Unexpected behavior when assigning a new sub dictionary to a field in NDict

1 participant