Skip to content

Commit 12a061f

Browse files
[App] Increased DeepDiff's verbose level to properly handle dict changes (#13960)
1 parent fb15ab7 commit 12a061f

File tree

8 files changed

+22
-14
lines changed

8 files changed

+22
-14
lines changed

src/lightning_app/core/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,11 @@ async def post_state(
258258
last_state = global_app_state_store.get_served_state(x_lightning_session_uuid)
259259
state = deepcopy(last_state)
260260
state["app_state"]["stage"] = body["stage"]
261-
deep_diff = DeepDiff(last_state, state)
261+
deep_diff = DeepDiff(last_state, state, verbose_level=2)
262262
else:
263263
state = body["state"]
264264
last_state = global_app_state_store.get_served_state(x_lightning_session_uuid)
265-
deep_diff = DeepDiff(last_state, state)
265+
deep_diff = DeepDiff(last_state, state, verbose_level=2)
266266
update_delta = Delta(deep_diff)
267267
api_app_delta_queue.put(update_delta)
268268

src/lightning_app/core/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def set_last_state(self, state):
202202

203203
@staticmethod
204204
def populate_changes(last_state, new_state):
205-
diff = DeepDiff(last_state, new_state, view="tree")
205+
diff = DeepDiff(last_state, new_state, view="tree", verbose_level=2)
206206

207207
changes_categories = [diff[key] for key in diff.to_dict()]
208208

@@ -307,7 +307,7 @@ def maybe_apply_changes(self) -> bool:
307307
if not deltas:
308308
# When no deltas are received from the Rest API or work queues,
309309
# we need to check if the flow modified the state and populate changes.
310-
if Delta(DeepDiff(self.last_state, self.state)).to_dict():
310+
if Delta(DeepDiff(self.last_state, self.state, verbose_level=2)).to_dict():
311311
# new_state = self.populate_changes(self.last_state, self.state)
312312
self.set_state(self.state)
313313
self._has_updated = True

src/lightning_app/utilities/proxies.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def run_once(self) -> None:
221221
self._delta_memory.clear()
222222

223223
# The remaining delta is the result of state updates triggered outside the setattr, e.g, by a list append
224-
delta = Delta(DeepDiff(self._last_state, self._work.state))
224+
delta = Delta(DeepDiff(self._last_state, self._work.state, verbose_level=2))
225225
if not delta.to_dict():
226226
return
227227
self._last_state = deepcopy(self._work.state)
@@ -256,7 +256,7 @@ def __call__(self, name: str, value: Any) -> None:
256256
with _state_observer_lock:
257257
state = deepcopy(self.work.state)
258258
self.work._default_setattr(name, value)
259-
delta = Delta(DeepDiff(state, self.work.state))
259+
delta = Delta(DeepDiff(state, self.work.state, verbose_level=2))
260260
if not delta.to_dict():
261261
return
262262

@@ -408,7 +408,9 @@ def run_once(self):
408408
make_status(WorkStageStatus.FAILED, message=str(e), reason=WorkFailureReasons.USER_EXCEPTION)
409409
)
410410
self.delta_queue.put(
411-
ComponentDelta(id=self.work_name, delta=Delta(DeepDiff(reference_state, self.work.state)))
411+
ComponentDelta(
412+
id=self.work_name, delta=Delta(DeepDiff(reference_state, self.work.state, verbose_level=2))
413+
)
412414
)
413415
self.work.on_exception(e)
414416
print("########## CAPTURED EXCEPTION ###########")
@@ -437,7 +439,9 @@ def run_once(self):
437439
reference_state = deepcopy(self.work.state)
438440
self.work._calls[call_hash]["statuses"].append(make_status(WorkStageStatus.SUCCEEDED))
439441
self.work._calls[call_hash]["ret"] = ret
440-
self.delta_queue.put(ComponentDelta(id=self.work_name, delta=Delta(DeepDiff(reference_state, self.work.state))))
442+
self.delta_queue.put(
443+
ComponentDelta(id=self.work_name, delta=Delta(DeepDiff(reference_state, self.work.state, verbose_level=2)))
444+
)
441445

442446
# 18. Update the work for the next delta if any.
443447
self._proxy_setattr(cleanup=True)
@@ -452,7 +456,7 @@ def _sigterm_signal_handler(self, signum, frame, call_hash: str) -> None:
452456
self.work._calls[call_hash]["statuses"].append(
453457
make_status(WorkStageStatus.STOPPED, reason=WorkStopReasons.SIGTERM_SIGNAL_HANDLER)
454458
)
455-
delta = Delta(DeepDiff(state, self.work.state))
459+
delta = Delta(DeepDiff(state, self.work.state, verbose_level=2))
456460
self.delta_queue.put(ComponentDelta(id=self.work_name, delta=delta))
457461

458462
# kill the thread as the job is going to be terminated.

src/lightning_app/utilities/scheduler.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ def run_once(self):
3737
flow = self._app.get_component_by_name(metadata["name"])
3838
previous_state = deepcopy(flow.state)
3939
flow._enable_schedule(call_hash)
40-
component_delta = ComponentDelta(id=flow.name, delta=Delta(DeepDiff(previous_state, flow.state)))
40+
component_delta = ComponentDelta(
41+
id=flow.name, delta=Delta(DeepDiff(previous_state, flow.state, verbose_level=2))
42+
)
4143
self._app.delta_queue.put(component_delta)
4244
metadata["start_time"] = next_event.isoformat()
4345

src/lightning_app/utilities/state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def _store_state(self, state: Dict[str, Any]) -> None:
130130

131131
def send_delta(self) -> None:
132132
app_url = f"{self._url}/api/v1/delta"
133-
deep_diff = DeepDiff(_LAST_STATE, _STATE)
133+
deep_diff = DeepDiff(_LAST_STATE, _STATE, verbose_level=2)
134134
assert self._plugin is not None
135135
# TODO: Find how to prevent the infinite loop on refresh without storing the DeepDiff
136136
if self._plugin.should_update_app(deep_diff):

tests/tests_app/core/test_lightning_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def _change_stage(self, enum):
118118
previous_state = deepcopy(self.state)
119119
current_state = self.state
120120
current_state["app_state"]["stage"] = enum.value
121-
deep_diff = DeepDiff(previous_state, current_state)
121+
deep_diff = DeepDiff(previous_state, current_state, verbose_level=2)
122122
self.api_delta_queue.put(Delta(deep_diff))
123123

124124
def maybe_apply_changes(self):

tests/tests_app/core/test_lightning_flow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ def run(self):
415415
work_state = flow_a.work.state
416416
flow_a.work.counter = 1
417417
work_state_2 = flow_a.work.state
418-
delta = Delta(DeepDiff(work_state, work_state_2))
418+
delta = Delta(DeepDiff(work_state, work_state_2, verbose_level=2))
419419
delta = _delta_to_appstate_delta(flow_a, flow_a.work, delta)
420420
new_flow_state = LightningApp.populate_changes(flow_state, flow_state + delta)
421421
flow_a.set_state(new_flow_state)

tests/tests_app/utilities/test_proxies.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ def __call__(self):
254254
"message": None,
255255
}
256256
)
257-
self.delta_queue.put(ComponentDelta(id=self.work_name, delta=Delta(DeepDiff(state, self.work.state))))
257+
self.delta_queue.put(
258+
ComponentDelta(id=self.work_name, delta=Delta(DeepDiff(state, self.work.state, verbose_level=2)))
259+
)
258260
self.counter += 1
259261
except Exception as e:
260262
logger.error(traceback.format_exc())

0 commit comments

Comments
 (0)