Skip to content

Commit 4ca39e0

Browse files
authored
fix: contribute directly a value to the context (#991)
Signed-off-by: Louis Mandel <[email protected]>
1 parent 73845ad commit 4ca39e0

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

src/pdl/pdl_context.py

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ def __mul__(self, value: "PDLContext"):
4040
class SingletonContext(PDLContext):
4141
message: PdlLazy[dict[str, Any]]
4242

43-
def __init__(self, message: PdlLazy[dict[str, Any]]):
44-
self.message = message
43+
def __init__(self, message: PdlLazy[dict[str, Any]] | dict[str, Any]):
44+
if isinstance(message, PdlLazy):
45+
self.message = message
46+
else:
47+
self.message = PdlConst(message)
4548

4649
def serialize(self, mode: SerializeMode) -> list[dict[str, Any]]:
4750
result = self.message.result()
@@ -60,20 +63,23 @@ def __repr__(self): # pyright: ignore
6063
class IndependentContext(PDLContext):
6164
context: PdlLazy[list[PDLContext]]
6265

63-
def __init__(self, context: list[PDLContext]):
66+
def __init__(self, context: list[PDLContext | dict[str, Any]]):
6467
ret: list[PDLContext] = []
6568
for item in context:
66-
if isinstance(item, IndependentContext):
67-
ret += item.context.data
68-
elif isinstance(item, SingletonContext):
69-
ret += [item]
70-
elif isinstance(item, DependentContext) and len(item) == 0:
71-
pass
72-
else:
73-
# Not all elements of the list are Independent, so return
74-
self.context = PdlList(context)
75-
return
76-
# All elements of the list are Independent
69+
match item:
70+
case IndependentContext():
71+
ret += item.context.data
72+
case SingletonContext():
73+
ret += [item]
74+
case DependentContext():
75+
if len(item) == 0:
76+
pass
77+
else:
78+
ret += [item]
79+
case dict():
80+
ret += [SingletonContext(item)]
81+
case _:
82+
assert False
7783
self.context = PdlList(ret)
7884

7985
def serialize(self, mode: SerializeMode) -> list[dict[str, Any]]:
@@ -99,20 +105,23 @@ def __repr__(self): # pyright: ignore
99105
class DependentContext(PDLContext):
100106
context: PdlLazy[list[PDLContext]]
101107

102-
def __init__(self, context: list[PDLContext]):
108+
def __init__(self, context: list[PDLContext | dict[str, Any]]):
103109
ret: list[PDLContext] = []
104110
for item in context:
105-
if isinstance(item, DependentContext):
106-
ret += item.context.data
107-
elif isinstance(item, SingletonContext):
108-
ret += [item]
109-
elif isinstance(item, IndependentContext) and len(item) == 0:
110-
pass
111-
else:
112-
# Not all elements of the list are Dependent, so return
113-
self.context = PdlList(context)
114-
return
115-
# All elements of the list are Dependent
111+
match item:
112+
case DependentContext():
113+
ret += item.context.data
114+
case SingletonContext():
115+
ret += [item]
116+
case IndependentContext():
117+
if len(item) == 0:
118+
pass
119+
else:
120+
ret += [item]
121+
case dict():
122+
ret += [SingletonContext(item)]
123+
case _:
124+
assert False
116125
self.context = PdlList(ret)
117126

118127
def serialize(self, mode: SerializeMode) -> list[dict[str, Any]]:

src/pdl/pdl_interpreter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ def process_advanced_block(
485485
background = DependentContext([])
486486
contribute_value, trace = process_contribute(trace, new_scope, loc)
487487
if contribute_value is not None:
488-
background = DependentContext([contribute_value])
488+
background = DependentContext(contribute_value)
489489

490490
return result, background, new_scope, trace
491491

0 commit comments

Comments
 (0)