From 8da37e0e8d8caa9be88d9754269a9ff4b54c8e92 Mon Sep 17 00:00:00 2001 From: Hannes Neuschmidt Date: Mon, 24 Aug 2026 13:42:12 +0200 Subject: [PATCH] Add draft of annotation-reading code --- test/test_parameters.py | 18 ++++++++++++++++++ xcengine/parameters.py | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/test/test_parameters.py b/test/test_parameters.py index 77e6400..8b3a0b7 100644 --- a/test/test_parameters.py +++ b/test/test_parameters.py @@ -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 \ No newline at end of file diff --git a/xcengine/parameters.py b/xcengine/parameters.py index 4536f63..c621d25 100644 --- a/xcengine/parameters.py +++ b/xcengine/parameters.py @@ -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