Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/pdl/pdl_dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
CallBlock,
CodeBlock,
ContributeTarget,
ContributeValue,
DataBlock,
EmptyBlock,
ErrorBlock,
Expand Down Expand Up @@ -82,6 +83,8 @@ def block_to_dict(block: pdl_ast.BlockType, json_compatible: bool) -> DumpedBloc
d["kind"] = str(block.kind)
if block.description is not None:
d["description"] = block.description
if block.role is not None:
d["role"] = block.role
if block.spec is not None:
d["spec"] = block.spec
if block.defs is not None:
Expand Down Expand Up @@ -205,7 +208,7 @@ def block_to_dict(block: pdl_ast.BlockType, json_compatible: bool) -> DumpedBloc
if block.assign is not None:
d["def"] = block.assign
if set(block.contribute) != {ContributeTarget.RESULT, ContributeTarget.CONTEXT}:
d["contribute"] = block.contribute
d["contribute"] = contribute_to_list(block.contribute)
if block.result is not None:
if isinstance(block.result, FunctionBlock):
d["result"] = ""
Expand Down Expand Up @@ -266,6 +269,18 @@ def location_to_dict(location: LocationType) -> dict[str, Any]:
return {"path": location.path, "file": location.file, "table": location.table}


def contribute_to_list(
contribute: Sequence[ContributeTarget | dict[str, ContributeValue]]
) -> list[str | dict[str, Any]]:
acc: list[str | dict[str, Any]] = []
for contrib in contribute:
if isinstance(contrib, str):
acc.append(str(contrib))
elif isinstance(contrib, dict):
acc.append({str(k): v.model_dump() for k, v in contrib.items()})
return acc


# def scope_to_dict(scope: ScopeType) -> dict[str, Any]:
# d = {}
# for x, v in scope.items():
Expand Down
36 changes: 36 additions & 0 deletions tests/test_dump.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import pathlib

from pdl.pdl_ast import BlockType, IncludeBlock
from pdl.pdl_ast_utils import iter_block_children
from pdl.pdl_dumper import dump_yaml, program_to_dict
from pdl.pdl_parser import PDLParseError, parse_file, parse_str


def has_include(block: BlockType) -> bool:
if isinstance(block, IncludeBlock):
return True
b = False

def f(x):
nonlocal b
if has_include(x):
b = True

iter_block_children(f, block)
return b


def test_ast_iterators() -> None:
for yaml_file_name in pathlib.Path(".").glob("**/*.pdl"):
try:
ast1, _ = parse_file(yaml_file_name)
if has_include(ast1.root):
continue
d = program_to_dict(ast1)
s = dump_yaml(d)
ast2, _ = parse_str(s)
json1 = ast1.model_dump_json()
json2 = ast2.model_dump_json()
assert json1 == json2, yaml_file_name
except PDLParseError:
pass
5 changes: 4 additions & 1 deletion tests/test_examples_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from pdl import pdl
from pdl.pdl_ast import ScopeType
from pdl.pdl_dumper import block_to_dict
from pdl.pdl_interpreter import PDLRuntimeError
from pdl.pdl_parser import PDLParseError

Expand Down Expand Up @@ -169,7 +170,9 @@ def test_valid_programs(capsys: CaptureFixture[str], monkeypatch: MonkeyPatch) -
scope = inputs.scope
try:
random.seed(11)
result = pdl.exec_file(pdl_file_name, scope=scope)
output = pdl.exec_file(pdl_file_name, scope=scope, output="all")
result = output["result"]
block_to_dict(output["trace"], json_compatible=True)
result_dir_name = (
pathlib.Path(".") / "tests" / "results" / pdl_file_name.parent
)
Expand Down