Skip to content
Open
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
18 changes: 18 additions & 0 deletions test/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,21 @@ def test_read_datasets_from_product_missing_items(
params.read_datasets_from_product(tmp_path, {})
for substring in "missing", "foo", "bar":
assert substring in str(error)


def test_read_annotations_from_code():
import textwrap
annotated_code = textwrap.dedent(
"""
my_int: int = 42
eoproduct: "EOInput" = None
"""
)
expected = [
{"annotation": "int", "value": "42", "target": "my_int", "line": 2},
{"annotation": "'EOInput'", "value": "None", "target": "eoproduct", "line": 3},
]

annotations = NotebookParameters.read_annotations(annotated_code)

assert annotations == expected
18 changes: 18 additions & 0 deletions xcengine/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,21 @@ def cwl_type(type_: type) -> str:
}[type_]
except KeyError:
raise ValueError(f"Unhandled type {type_}")

@staticmethod
def read_annotations(code: str) -> list[dict[str, Any]]:
import ast
tree = ast.parse(code)
annotations: list[dict[str, Any]] = []
for node in ast.walk(tree):
if not isinstance(node, ast.AnnAssign):
continue

annotations.append({
"annotation": ast.unparse(node.annotation),
"value": ast.unparse(node.value) if node.value else None,
"target": ast.unparse(node.target),
"line": node.lineno,
})

return annotations
Loading