Skip to content

Commit 2c122d1

Browse files
authored
Merge pull request #88 from xcube-dev/hn-parse-annotations
Add annotation-reading code
2 parents 502c440 + 0c20079 commit 2c122d1

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
`s:softwareVersion` (#90)
77
* Allow creation of EOAP-only and xcube-server-only images, omitting
88
unnecessary dependencies (#56)
9+
* Add a utility function to read annotations from notebook code (#91)
910

1011
## Changes in 0.1.2
1112

test/test_parameters.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,16 @@ def test_read_datasets_from_product_missing_items(
377377
params.read_datasets_from_product(tmp_path, {})
378378
for substring in "missing", "foo", "bar":
379379
assert substring in str(error)
380+
381+
382+
def test_read_annotations_from_code():
383+
import textwrap
384+
385+
annotated_code = textwrap.dedent("""
386+
my_int: int = 42
387+
eoproduct: "EOInput" = None
388+
""")
389+
assert NotebookParameters.read_annotations(annotated_code) == {
390+
"my_int": "int",
391+
"eoproduct": "'EOInput'",
392+
}

xcengine/parameters.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,13 @@ def cwl_type(type_: type) -> str:
234234
}[type_]
235235
except KeyError:
236236
raise ValueError(f"Unhandled type {type_}")
237+
238+
@staticmethod
239+
def read_annotations(code: str) -> dict[str, str]:
240+
import ast
241+
242+
return {
243+
ast.unparse(node.target): ast.unparse(node.annotation)
244+
for node in ast.walk(ast.parse(code))
245+
if isinstance(node, ast.AnnAssign)
246+
}

0 commit comments

Comments
 (0)