From e19041976a324085f5165591607aa832afb2b2e3 Mon Sep 17 00:00:00 2001 From: Norman Fomferra Date: Tue, 26 Nov 2024 17:16:58 +0100 Subject: [PATCH] Changed the yet unused descriptor type `CbFunction` for callback functions. --- chartlets.js/CHANGES.md | 4 +++ chartlets.js/package.json | 2 +- chartlets.js/src/lib/types/model/callback.ts | 8 ++++-- chartlets.py/CHANGES.md | 1 + chartlets.py/chartlets/callback.py | 11 ++++--- chartlets.py/chartlets/version.py | 2 +- chartlets.py/tests/callback_test.py | 30 ++++++++++---------- 7 files changed, 35 insertions(+), 23 deletions(-) diff --git a/chartlets.js/CHANGES.md b/chartlets.js/CHANGES.md index c576eb8d..d485eadb 100644 --- a/chartlets.js/CHANGES.md +++ b/chartlets.js/CHANGES.md @@ -1,3 +1,7 @@ +## Version 0.0.x (in development) + +* Changed the yet unused descriptor type `CbFunction` for callback functions. + ## Version 0.0.28 (from 2024/11/26) * Updated docs. diff --git a/chartlets.js/package.json b/chartlets.js/package.json index 2ede7f72..036f5e0b 100644 --- a/chartlets.js/package.json +++ b/chartlets.js/package.json @@ -1,6 +1,6 @@ { "name": "chartlets", - "version": "0.0.28", + "version": "0.0.29", "description": "An experimental library for integrating interactive charts into existing JavaScript applications.", "type": "module", "files": [ diff --git a/chartlets.js/src/lib/types/model/callback.ts b/chartlets.js/src/lib/types/model/callback.ts index f72263ac..12117833 100644 --- a/chartlets.js/src/lib/types/model/callback.ts +++ b/chartlets.js/src/lib/types/model/callback.ts @@ -23,15 +23,19 @@ export interface JsonSchema { export interface CbFunction { name: string; parameters: CbParameter[]; - returnType: JsonSchema; + return: CbReturn; } export interface CbParameter { name: string; - type?: JsonSchema; + schema?: JsonSchema; default?: unknown; } +export interface CbReturn { + schema?: JsonSchema; +} + /** * A reference to a specific contribution. */ diff --git a/chartlets.py/CHANGES.md b/chartlets.py/CHANGES.md index 6c24faf0..97ca6886 100644 --- a/chartlets.py/CHANGES.md +++ b/chartlets.py/CHANGES.md @@ -2,6 +2,7 @@ * Fixed a bug that prevent using annotations of type `dict` or `dict[str, T]`. in callback functions. +* Changed schema of the yet unused descriptor for callback functions. ## Version 0.0.28 (from 2024/11/26) diff --git a/chartlets.py/chartlets/callback.py b/chartlets.py/chartlets/callback.py index 1a0583e8..83e65aba 100644 --- a/chartlets.py/chartlets/callback.py +++ b/chartlets.py/chartlets/callback.py @@ -104,9 +104,7 @@ def to_dict(self) -> dict[str, Any]: "function": { "name": self.function.__qualname__, "parameters": [_parameter_to_dict(p) for p in parameters], - "returnType": _annotation_to_json_schema( - self.signature.return_annotation - ), + "return": _return_to_dict(self.signature.return_annotation), } } if self.inputs: @@ -152,11 +150,16 @@ def _parameter_to_dict(parameter: inspect.Parameter) -> dict[str, Any]: empty = inspect.Parameter.empty d = {"name": parameter.name} if parameter.annotation is not empty: - d |= {"type": _annotation_to_json_schema(parameter.annotation)} + d |= {"schema": _annotation_to_json_schema(parameter.annotation)} if parameter.default is not empty: d |= {"default": parameter.default} return d +def _return_to_dict(return_annotation: Any) -> dict[str, Any]: + return { + "schema": _annotation_to_json_schema(return_annotation) + } + _basic_types = { None: "null", diff --git a/chartlets.py/chartlets/version.py b/chartlets.py/chartlets/version.py index 7dc68830..be67c33b 100644 --- a/chartlets.py/chartlets/version.py +++ b/chartlets.py/chartlets/version.py @@ -1 +1 @@ -version = "0.0.28" +version = "0.0.29" diff --git a/chartlets.py/tests/callback_test.py b/chartlets.py/tests/callback_test.py index b7428b41..dd7abdf9 100644 --- a/chartlets.py/tests/callback_test.py +++ b/chartlets.py/tests/callback_test.py @@ -15,9 +15,9 @@ def my_callback( b: str | int = "", c: bool | None = False, d: list[str] = (), - e: dict[str, Any] = (), + e: dict[str, Any] | None = None, ) -> str: - return f"{a}-{b}-{c}-{d}" + return f"{a}-{b}-{c}-{d}-{e}" def my_callback_2(ctx, n: int) -> tuple[list[str], str | None]: @@ -45,29 +45,29 @@ def test_to_dict_with_no_outputs(self): "function": { "name": "my_callback", "parameters": [ - {"name": "a", "type": {"type": "integer"}}, + {"name": "a", "schema": {"type": "integer"}}, { - "default": "", "name": "b", - "type": {"type": ["string", "integer"]}, + "schema": {"type": ["string", "integer"]}, + "default": "", }, { - "default": False, "name": "c", - "type": {"type": ["boolean", "null"]}, + "schema": {"type": ["boolean", "null"]}, + "default": False, }, { - "default": (), "name": "d", - "type": {"items": {"type": "string"}, "type": "array"}, + "schema": {"items": {"type": "string"}, "type": "array"}, + "default": (), }, { - "default": (), "name": "e", - "type": {"additionalProperties": {}, "type": "object"}, + "schema": {'type': ['object', 'null']}, + "default": None, }, ], - "returnType": {"type": "string"}, + "return": {"schema": {"type": "string"}}, }, "inputs": [ {"id": "a", "property": "value"}, @@ -95,14 +95,14 @@ def test_to_dict_with_two_outputs(self): { "function": { "name": "my_callback_2", - "parameters": [{"name": "n", "type": {"type": "integer"}}], - "returnType": { + "parameters": [{"name": "n", "schema": {"type": "integer"}}], + "return": {"schema":{ "items": [ {"items": {"type": "string"}, "type": "array"}, {"type": ["string", "null"]}, ], "type": "array", - }, + }}, }, "inputs": [{"id": "n", "property": "value"}], "outputs": [