Skip to content

Commit 6c8f509

Browse files
authored
Fixes in pdl_dumper (#199)
1 parent 2b34a9a commit 6c8f509

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/pdl/pdl_dumper.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
CallBlock,
1313
CodeBlock,
1414
ContributeTarget,
15+
ContributeValue,
1516
DataBlock,
1617
EmptyBlock,
1718
ErrorBlock,
@@ -82,6 +83,8 @@ def block_to_dict(block: pdl_ast.BlockType, json_compatible: bool) -> DumpedBloc
8283
d["kind"] = str(block.kind)
8384
if block.description is not None:
8485
d["description"] = block.description
86+
if block.role is not None:
87+
d["role"] = block.role
8588
if block.spec is not None:
8689
d["spec"] = block.spec
8790
if block.defs is not None:
@@ -205,7 +208,7 @@ def block_to_dict(block: pdl_ast.BlockType, json_compatible: bool) -> DumpedBloc
205208
if block.assign is not None:
206209
d["def"] = block.assign
207210
if set(block.contribute) != {ContributeTarget.RESULT, ContributeTarget.CONTEXT}:
208-
d["contribute"] = block.contribute
211+
d["contribute"] = contribute_to_list(block.contribute)
209212
if block.result is not None:
210213
if isinstance(block.result, FunctionBlock):
211214
d["result"] = ""
@@ -266,6 +269,18 @@ def location_to_dict(location: LocationType) -> dict[str, Any]:
266269
return {"path": location.path, "file": location.file, "table": location.table}
267270

268271

272+
def contribute_to_list(
273+
contribute: Sequence[ContributeTarget | dict[str, ContributeValue]]
274+
) -> list[str | dict[str, Any]]:
275+
acc: list[str | dict[str, Any]] = []
276+
for contrib in contribute:
277+
if isinstance(contrib, str):
278+
acc.append(str(contrib))
279+
elif isinstance(contrib, dict):
280+
acc.append({str(k): v.model_dump() for k, v in contrib.items()})
281+
return acc
282+
283+
269284
# def scope_to_dict(scope: ScopeType) -> dict[str, Any]:
270285
# d = {}
271286
# for x, v in scope.items():

tests/test_dump.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import pathlib
2+
3+
from pdl.pdl_ast import BlockType, IncludeBlock
4+
from pdl.pdl_ast_utils import iter_block_children
5+
from pdl.pdl_dumper import dump_yaml, program_to_dict
6+
from pdl.pdl_parser import PDLParseError, parse_file, parse_str
7+
8+
9+
def has_include(block: BlockType) -> bool:
10+
if isinstance(block, IncludeBlock):
11+
return True
12+
b = False
13+
14+
def f(x):
15+
nonlocal b
16+
if has_include(x):
17+
b = True
18+
19+
iter_block_children(f, block)
20+
return b
21+
22+
23+
def test_ast_iterators() -> None:
24+
for yaml_file_name in pathlib.Path(".").glob("**/*.pdl"):
25+
try:
26+
ast1, _ = parse_file(yaml_file_name)
27+
if has_include(ast1.root):
28+
continue
29+
d = program_to_dict(ast1)
30+
s = dump_yaml(d)
31+
ast2, _ = parse_str(s)
32+
json1 = ast1.model_dump_json()
33+
json2 = ast2.model_dump_json()
34+
assert json1 == json2, yaml_file_name
35+
except PDLParseError:
36+
pass

tests/test_examples_run.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from pdl import pdl
1010
from pdl.pdl_ast import ScopeType
11+
from pdl.pdl_dumper import block_to_dict
1112
from pdl.pdl_interpreter import PDLRuntimeError
1213
from pdl.pdl_parser import PDLParseError
1314

@@ -169,7 +170,9 @@ def test_valid_programs(capsys: CaptureFixture[str], monkeypatch: MonkeyPatch) -
169170
scope = inputs.scope
170171
try:
171172
random.seed(11)
172-
result = pdl.exec_file(pdl_file_name, scope=scope)
173+
output = pdl.exec_file(pdl_file_name, scope=scope, output="all")
174+
result = output["result"]
175+
block_to_dict(output["trace"], json_compatible=True)
173176
result_dir_name = (
174177
pathlib.Path(".") / "tests" / "results" / pdl_file_name.parent
175178
)

0 commit comments

Comments
 (0)