Skip to content

Commit f07f980

Browse files
authored
refactor: make the use of dependent and independent context explicit (#972)
Signed-off-by: Louis Mandel <[email protected]>
1 parent be61a56 commit f07f980

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

src/pdl/pdl_interpreter.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
)
9797
from .pdl_context import ( # noqa: E402
9898
DependentContext,
99+
IndependentContext,
99100
PDLContext,
100101
SerializeMode,
101102
SingletonContext,
@@ -359,7 +360,11 @@ def set_error_to_scope_for_retry(
359360
"content": error,
360361
"defsite": block_id,
361362
}
362-
scope = scope | {"pdl_context": pdl_context * SingletonContext(PdlDict(err_msg))}
363+
scope = scope | {
364+
"pdl_context": DependentContext(
365+
[pdl_context, SingletonContext(PdlDict(err_msg))]
366+
)
367+
}
363368
return scope
364369

365370

@@ -605,9 +610,13 @@ def process_block_body(
605610
append(obj_loc, k),
606611
)
607612
if block.context == IndependentEnum.DEPENDENT:
608-
background = background * value_background
613+
background = DependentContext(
614+
[background, value_background]
615+
)
609616
else:
610-
background = background + value_background
617+
background = IndependentContext(
618+
[background, value_background]
619+
)
611620
if (
612621
block.context is IndependentEnum.INDEPENDENT
613622
): # reset pdl_context
@@ -829,7 +838,9 @@ def process_block_body(
829838
}
830839
]
831840
)
832-
scope = scope | {"pdl_context": pdl_context_init * background}
841+
scope = scope | {
842+
"pdl_context": DependentContext([pdl_context_init, background])
843+
}
833844
if items is not None:
834845
for k in items.keys():
835846
scope = scope | {k: items[k][iidx]}
@@ -845,9 +856,13 @@ def process_block_body(
845856
repeat_loc,
846857
)
847858
if block.context is IndependentEnum.DEPENDENT:
848-
saved_background = saved_background * iteration_background
859+
saved_background = DependentContext(
860+
[saved_background, iteration_background]
861+
)
849862
else:
850-
saved_background = saved_background + iteration_background
863+
saved_background = IndependentContext(
864+
[saved_background, iteration_background]
865+
)
851866

852867
if block.context is IndependentEnum.DEPENDENT:
853868
background = saved_background
@@ -1088,7 +1103,9 @@ def process_blocks( # pylint: disable=too-many-arguments,too-many-positional-ar
10881103
try:
10891104
for i, block in enumerate(blocks):
10901105
iteration_state = iteration_state.with_iter(i)
1091-
scope = scope | {"pdl_context": pdl_context_init * background}
1106+
scope = scope | {
1107+
"pdl_context": DependentContext([pdl_context_init, background])
1108+
}
10921109
new_loc = append(loc, "[" + str(i) + "]")
10931110
if iteration_type == IterationType.LASTOF and state.yield_result:
10941111
iteration_state = state.with_yield_result(i + 1 == len(blocks))
@@ -1100,9 +1117,13 @@ def process_blocks( # pylint: disable=too-many-arguments,too-many-positional-ar
11001117
) = process_block(iteration_state, scope, block, new_loc)
11011118
results.append(iteration_result)
11021119
if context == IndependentEnum.DEPENDENT:
1103-
saved_background = saved_background * iteration_background
1120+
saved_background = DependentContext(
1121+
[saved_background, iteration_background]
1122+
)
11041123
else:
1105-
saved_background = saved_background + iteration_background
1124+
saved_background = IndependentContext(
1125+
[saved_background, iteration_background]
1126+
)
11061127

11071128
if context == IndependentEnum.DEPENDENT:
11081129
background = saved_background

0 commit comments

Comments
 (0)