From d8875d549acd1498b42af76975b7e394d1f19f66 Mon Sep 17 00:00:00 2001 From: Benedikt Burger <67148916+BenediktBurger@users.noreply.github.com> Date: Fri, 23 Feb 2024 13:16:20 +0100 Subject: [PATCH] Fix errors. --- environment.yml | 4 ++-- pyleco/json_utils/rpc_generator.py | 2 ++ pyleco/json_utils/rpc_server_definition.py | 15 ++++++--------- pyproject.toml | 4 +++- tests/json_utils/test_json_objects_definitions.py | 2 +- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/environment.yml b/environment.yml index 1c42d673..1405ed3a 100644 --- a/environment.yml +++ b/environment.yml @@ -2,10 +2,10 @@ name: pyleco channels: - conda-forge dependencies: - - pyzmq=25.1.2 + - pyzmq #=25.1.2 - pip # don't pin, to gain newest conda compatibility fixes - pip: - - openrpc==8.1.0 + # - openrpc==8.1.0 - uuid7==0.1.0 # Development dependencies below - pytest=7.2.0 diff --git a/pyleco/json_utils/rpc_generator.py b/pyleco/json_utils/rpc_generator.py index c38ade34..1bc92d49 100644 --- a/pyleco/json_utils/rpc_generator.py +++ b/pyleco/json_utils/rpc_generator.py @@ -46,6 +46,7 @@ def build_request_str(self, method: str, *args, **kwargs) -> str: "and give additional keyword arguments at the same time.") id = self._id_counter self._id_counter += 1 + r: Union[Request, ParamsRequest] if args or kwargs: r = ParamsRequest(id=id, method=method, params=kwargs or list(args)) else: @@ -64,6 +65,7 @@ def get_result_from_response(self, data: Union[bytes, str, Dict]) -> Any: # Raise error if JSON RPC error response. if error_content := json_data.get("error"): + error: Union[Error, DataError] if error_content.get("data"): error = DataError(**error_content) else: diff --git a/pyleco/json_utils/rpc_server_definition.py b/pyleco/json_utils/rpc_server_definition.py index 104dfbf9..5cc68c1f 100644 --- a/pyleco/json_utils/rpc_server_definition.py +++ b/pyleco/json_utils/rpc_server_definition.py @@ -65,18 +65,15 @@ def process_request(self, data: Union[bytes, str]) -> Optional[str]: result = self.process_single_request(element) if result is not None: results.append(result.model_dump()) + if results: + return json.dumps(results, separators=(',', ':')) elif isinstance(json_data, dict): - results = self.process_single_request(json_data).model_dump() - if results: - return json.dumps(results, separators=(',', ':')) + results = self.process_single_request(json_data) + if results: + return results.model_dump_json() except Exception as exc: log.exception(f"{type(exc).__name__}:", exc_info=exc) - return ErrorResponse(id=id, error=INTERNAL_ERROR).model_dump_json() - else: - if id is not None: - return ResultResponse(id=id, result=result).model_dump_json() - else: - return None + return ErrorResponse(id=None, error=INTERNAL_ERROR).model_dump_json() def process_single_request(self, request: Dict[str, Any] ) -> Union[ResultResponse, ErrorResponse, None]: diff --git a/pyproject.toml b/pyproject.toml index 007e7782..ff5a5d64 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,7 +22,7 @@ requires-python = ">=3.7" dependencies = [ "pyzmq >= 22.3.0", "openrpc >= 8.1.0;python_version>=3.9", - "jsonrpc2-objects >= 3.0.0;python_version>=3.9", + "jsonrpc2-objects >= 3.0.0; python_version >= '3.9'", "uuid7 >= 0.1.0", ] @@ -80,4 +80,6 @@ omit = [ # Omit LECO definitions "pyleco/errors.py", "pyleco/core/leco_protocols.py", + "pyleco/json_utils/json_objects", + "pyleco/json_utils/rpc_server", ] diff --git a/tests/json_utils/test_json_objects_definitions.py b/tests/json_utils/test_json_objects_definitions.py index a8900b67..a020a17a 100644 --- a/tests/json_utils/test_json_objects_definitions.py +++ b/tests/json_utils/test_json_objects_definitions.py @@ -28,5 +28,5 @@ def test_error_with_data(): data_error = jod.DataError(code=5, message="whatever", data="abc") error_response = jod.ErrorResponse(id=7, error=data_error) - assert error_response.model_dump_json() == '{"id":7,"error":{"code":5,"message":"whatever","data":"abc"},"jsonrpc":"2.0"}' + assert error_response.model_dump_json() == '{"id":7,"error":{"code":5,"message":"whatever","data":"abc"},"jsonrpc":"2.0"}' # noqa