Skip to content

Commit 45cd2ed

Browse files
committed
⚙️ fixed: remove default context from result dataclass model.
1 parent d21c196 commit 45cd2ed

3 files changed

Lines changed: 25 additions & 42 deletions

File tree

src/ddeutil/workflow/result.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,6 @@ def get_status_from_error(
166166
return FAILED
167167

168168

169-
def default_context() -> DictData:
170-
return {"status": WAIT}
171-
172-
173169
@dataclass(
174170
config=ConfigDict(arbitrary_types_allowed=True, use_enum_values=True),
175171
)
@@ -188,7 +184,7 @@ class Result:
188184

189185
extras: DictData = field(default_factory=dict, compare=False, repr=False)
190186
status: Status = field(default=WAIT)
191-
context: DictData = field(default_factory=default_context)
187+
context: Optional[DictData] = field(default=None)
192188
info: DictData = field(default_factory=dict)
193189
run_id: str = field(default_factory=default_gen_id)
194190
parent_run_id: Optional[str] = field(default=None)
@@ -207,11 +203,18 @@ def __prepare_trace(self) -> Self:
207203
extras=self.extras,
208204
)
209205

210-
if self.parent_run_id is None:
211-
self.parent_run_id = self.run_id
212-
213206
return self
214207

208+
@classmethod
209+
def from_trace(cls, trace: Trace):
210+
"""Construct the result model from trace for clean code objective."""
211+
return cls(
212+
run_id=trace.run_id,
213+
parent_run_id=trace.parent_run_id,
214+
extras=trace.extras,
215+
trace=trace,
216+
)
217+
215218
def catch(
216219
self,
217220
status: Union[int, Status],
@@ -227,7 +230,11 @@ def catch(
227230
228231
:rtype: Self
229232
"""
230-
self.__dict__["context"].update(context or {})
233+
if self.__dict__["context"] is None:
234+
self.__dict__["context"] = context
235+
else:
236+
self.__dict__["context"].update(context or {})
237+
231238
self.__dict__["status"] = (
232239
Status(status) if isinstance(status, int) else status
233240
)

src/ddeutil/workflow/workflow.py

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -939,12 +939,9 @@ def rerun(
939939
"[WORKFLOW]: Does not rerun because it already executed with "
940940
"success status."
941941
)
942-
return Result(
943-
run_id=run_id,
944-
parent_run_id=parent_run_id,
942+
return Result.from_trace(trace).catch(
945943
status=SUCCESS,
946944
context=catch(context=context, status=SUCCESS),
947-
extras=self.extras,
948945
)
949946

950947
err: dict[str, str] = context.get("errors", {})
@@ -960,12 +957,9 @@ def rerun(
960957
)
961958
if not self.jobs:
962959
trace.warning(f"[WORKFLOW]: {self.name!r} does not set jobs")
963-
return Result(
964-
run_id=run_id,
965-
parent_run_id=parent_run_id,
960+
return Result.from_trace(trace).catch(
966961
status=SUCCESS,
967962
context=catch(context=context, status=SUCCESS),
968-
extras=self.extras,
969963
)
970964

971965
# NOTE: Prepare the new context variable for rerun process.
@@ -990,12 +984,9 @@ def rerun(
990984
"[WORKFLOW]: It does not have job to rerun. it will change "
991985
"status to skip."
992986
)
993-
return Result(
994-
run_id=run_id,
995-
parent_run_id=parent_run_id,
987+
return Result.from_trace(trace).catch(
996988
status=SKIP,
997989
context=catch(context=context, status=SKIP),
998-
extras=self.extras,
999990
)
1000991

1001992
not_timeout_flag: bool = True
@@ -1008,9 +999,7 @@ def rerun(
1008999

10091000
catch(context, status=WAIT)
10101001
if event and event.is_set():
1011-
return Result(
1012-
run_id=run_id,
1013-
parent_run_id=parent_run_id,
1002+
return Result.from_trace(trace).catch(
10141003
status=CANCEL,
10151004
context=catch(
10161005
context,
@@ -1022,7 +1011,6 @@ def rerun(
10221011
).to_dict(),
10231012
},
10241013
),
1025-
extras=self.extras,
10261014
)
10271015

10281016
with ThreadPoolExecutor(max_job_parallel, "wf") as executor:
@@ -1050,9 +1038,7 @@ def rerun(
10501038
backoff_sleep = 0.01
10511039

10521040
if check == FAILED: # pragma: no cov
1053-
return Result(
1054-
run_id=run_id,
1055-
parent_run_id=parent_run_id,
1041+
return Result.from_trace(trace).catch(
10561042
status=FAILED,
10571043
context=catch(
10581044
context,
@@ -1065,7 +1051,6 @@ def rerun(
10651051
).to_dict(),
10661052
},
10671053
),
1068-
extras=self.extras,
10691054
)
10701055
elif check == SKIP: # pragma: no cov
10711056
trace.info(
@@ -1141,12 +1126,9 @@ def rerun(
11411126
statuses[total + 1 + skip_count + i] = s
11421127

11431128
st: Status = validate_statuses(statuses)
1144-
return Result(
1145-
run_id=run_id,
1146-
parent_run_id=parent_run_id,
1129+
return Result.from_trace(trace).catch(
11471130
status=st,
11481131
context=catch(context, status=st),
1149-
extras=self.extras,
11501132
)
11511133

11521134
event.set()
@@ -1160,9 +1142,7 @@ def rerun(
11601142

11611143
time.sleep(0.0025)
11621144

1163-
return Result(
1164-
run_id=run_id,
1165-
parent_run_id=parent_run_id,
1145+
return Result.from_trace(trace).catch(
11661146
status=FAILED,
11671147
context=catch(
11681148
context,
@@ -1174,5 +1154,4 @@ def rerun(
11741154
).to_dict(),
11751155
},
11761156
),
1177-
extras=self.extras,
11781157
)

tests/test_result.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ def test_result_default():
2929
time.sleep(0.025)
3030
rs2 = Result()
3131
assert rs.status == Status.WAIT
32-
assert rs.context == {"status": Status.WAIT}
32+
assert rs.context is None
3333
assert rs2.status == Status.WAIT
34-
assert rs2.context == {"status": Status.WAIT}
34+
assert rs2.context is None
3535

3636
# NOTE: Result objects should not equal because they do not have the same
3737
# running ID value.
@@ -51,9 +51,6 @@ def test_result_context():
5151

5252
def test_result_catch():
5353
rs: Result = Result()
54-
55-
assert rs.run_id == rs.parent_run_id
56-
5754
data = {"params": {"source": "src", "target": "tgt"}}
5855
rs.catch(status=SUCCESS, context=data)
5956
assert rs.status == SUCCESS

0 commit comments

Comments
 (0)