Broken `if-if-else` Branching in `_patch_value` Wipes Unmodified Keys in Nested List-Dict Structures
Bug : Broken if-if-else Branching in _patch_value Wipes Unmodified Keys in Nested List-Dict Structures
1. File & Lines
- File:
taipy/gui/utils/patch.py - Lines: 46–63
- Function:
_patch_value(value, change, remove)
2. Description & Real-World Impact
Taipy GUI provides a state.patch(name, change=...) API allowing developers and frontend components to perform granular, partial updates to complex variables without sending full object trees across WebSockets.
In taipy/gui/utils/patch.py, when updating an element inside a list where the element is a dictionary (for instance, updating the status of a specific row in state.table_items), the patching logic contains two consecutive if statements instead of an if ... elif ... else block.
Because the second condition is if isinstance(v, list):, when v is a dict, the isinstance(v, list) test evaluates to False. The code then drops immediately into the associated else: block:
else:
value[idx] = vThis completely overwrites the entire dictionary at value[idx] with v, obliterating all other keys and fields! A partial update intended to modify 1 field silently destroys the rest of the object.
3. Vulnerable Source Code
# taipy/gui/utils/patch.py lines 46-63
if idx < len(value):
if isinstance(v, dict):
_patch_value(value[idx], v)
if isinstance(v, list): # <--- BUG: Must be 'elif isinstance(v, list):'
if insert:
value[idx:idx] = v
else:
for jdx, nv in enumerate(v, start=idx):
if jdx < len(value):
if isinstance(nv, dict):
_patch_value(value[jdx], nv)
else:
value[jdx] = nv
else:
value.append(nv)
else: # <--- Executes when v is a dict!
value[idx] = v # <--- OVERWRITES ENTIRE DICT!4. Minimal Reproduction Script
from taipy.gui.utils.patch import _patch_value
# A list of records in a GUI table or dataset
items = [
{"id": 101, "title": "Quarterly Report", "author": "Alice", "status": "draft"}
]
# User triggers a patch to update ONLY the status field of record 0
_patch_value(items, change={0: {"status": "published"}})
print("Result:", items)
# EXPECTED: [{'id': 101, 'title': 'Quarterly Report', 'author': 'Alice', 'status': 'published'}]
# ACTUAL: [{'status': 'published'}] <-- 'id', 'title', and 'author' were deleted!5. Proposed Fix (Git Patch)
diff --git a/taipy/gui/utils/patch.py b/taipy/gui/utils/patch.py
index a1b2c3d..e4f5g6h 100644
--- a/taipy/gui/utils/patch.py
+++ b/taipy/gui/utils/patch.py
@@ -47,7 +47,7 @@ def _patch_value(value: t.Any, change: t.Optional[dict] = None, remove: t.Option
if idx < len(value):
if isinstance(v, dict):
_patch_value(value[idx], v)
- if isinstance(v, list):
+ elif isinstance(v, list):
if insert:
value[idx:idx] = v
else:Source: Avaiga/taipy