From 9ccf89110c618690ad11c179caa33256f416b377 Mon Sep 17 00:00:00 2001 From: Stefaan Lippens Date: Thu, 25 May 2023 17:35:37 +0200 Subject: [PATCH] Fix #431 fix UDP param conversion issue in `run_udf` `context` --- CHANGELOG.md | 3 + openeo/internal/graph_building.py | 2 + tests/rest/test_udp.py | 98 +++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b54deea9e..f60c65490 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Fixed UDP parameter conversion issue in `build_process_dict` when using parameter in `context` of `run_udf` + [#431](https://github.com/Open-EO/openeo-python-client/issues/431) + ## [0.17.0] and [0.17.1] - 2023-05-16 diff --git a/openeo/internal/graph_building.py b/openeo/internal/graph_building.py index aff300a8a..bafc5aa8e 100644 --- a/openeo/internal/graph_building.py +++ b/openeo/internal/graph_building.py @@ -358,6 +358,8 @@ def _flatten_argument(self, value): raise ValueError(pg) else: value = {k: self._flatten_argument(v) for k, v in value.items()} + elif isinstance(value, Parameter): + value = {"from_parameter": value.name} return value def leaveArgument(self, argument_id: str, value): diff --git a/tests/rest/test_udp.py b/tests/rest/test_udp.py index 1f7afda9e..89f89e98a 100644 --- a/tests/rest/test_udp.py +++ b/tests/rest/test_udp.py @@ -460,3 +460,101 @@ def test_build_process_dict_from_datacube(con100): "returns": {"schema": {"type": "number"}}, } assert actual == expected + + +def test_build_process_dict_udf_context_param_field(con100): + """https://github.com/Open-EO/openeo-python-client/issues/431""" + udf_param = Parameter.number("udf_param") + cube = con100.datacube_from_process("add", x=3, y=5) + udf = openeo.UDF(code="print(123)", runtime="Python", context={"foo": udf_param}) + cube = cube.apply(process=udf) + + actual = build_process_dict( + process_graph=cube, + process_id="do_udf", + summary="do_udf value", + description="do_udf", + parameters=[udf_param], + returns={"schema": {"type": "number"}}, + ) + expected = { + "id": "do_udf", + "description": "do_udf", + "parameters": [{"name": "udf_param", "description": "udf_param", "schema": {"type": "number"}}], + "process_graph": { + "add1": {"arguments": {"x": 3, "y": 5}, "process_id": "add"}, + "apply1": { + "arguments": { + "data": {"from_node": "add1"}, + "process": { + "process_graph": { + "runudf1": { + "process_id": "run_udf", + "arguments": { + "data": {"from_parameter": "x"}, + "udf": "print(123)", + "runtime": "Python", + "context": {"foo": {"from_parameter": "udf_param"}}, + }, + "result": True, + } + } + }, + }, + "process_id": "apply", + "result": True, + }, + }, + "returns": {"schema": {"type": "number"}}, + "summary": "do_udf value", + } + assert actual == expected + + +def test_build_process_dict_udf_context_param_direct(con100): + """https://github.com/Open-EO/openeo-python-client/issues/431""" + udf_param = Parameter.number("udf_param") + cube = con100.datacube_from_process("add", x=3, y=5) + udf = openeo.UDF(code="print(123)", runtime="Python", context=udf_param) + cube = cube.apply(process=udf) + + actual = build_process_dict( + process_graph=cube, + process_id="do_udf", + summary="do_udf value", + description="do_udf", + parameters=[udf_param], + returns={"schema": {"type": "number"}}, + ) + expected = { + "id": "do_udf", + "description": "do_udf", + "parameters": [{"name": "udf_param", "description": "udf_param", "schema": {"type": "number"}}], + "process_graph": { + "add1": {"arguments": {"x": 3, "y": 5}, "process_id": "add"}, + "apply1": { + "arguments": { + "data": {"from_node": "add1"}, + "process": { + "process_graph": { + "runudf1": { + "process_id": "run_udf", + "arguments": { + "data": {"from_parameter": "x"}, + "udf": "print(123)", + "runtime": "Python", + "context": {"from_parameter": "udf_param"}, + }, + "result": True, + } + } + }, + }, + "process_id": "apply", + "result": True, + }, + }, + "returns": {"schema": {"type": "number"}}, + "summary": "do_udf value", + } + assert actual == expected