From c6df35c8ca3c3ee16bbcddb180188a681f63371f Mon Sep 17 00:00:00 2001 From: HR Wu <5631010+heiruwu@users.noreply.github.com> Date: Thu, 9 May 2024 17:03:22 +0800 Subject: [PATCH] fix(resources): fix nil response handling (#151) Because - In the case of error response, will cause resource methods to fail This commit - return none if request failed --- instill/resources/model.py | 28 +++++++++++++++++++--------- instill/resources/pipeline.py | 22 +++++++++++++++------- poetry.lock | 13 ++++++------- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/instill/resources/model.py b/instill/resources/model.py index a640bfb..42b1555 100644 --- a/instill/resources/model.py +++ b/instill/resources/model.py @@ -1,5 +1,6 @@ # pylint: disable=no-member,wrong-import-position,no-name-in-module import time +from typing import Optional import instill.protogen.model.model.v1alpha.model_definition_pb2 as model_definition_interface import instill.protogen.model.model.v1alpha.model_pb2 as model_interface @@ -47,12 +48,15 @@ def __init__( self.resource = model - def __call__(self, task_inputs: list, silent: bool = False) -> list: - return self.client.model_service.trigger_model( + def __call__(self, task_inputs: list, silent: bool = False) -> Optional[list]: + response = self.client.model_service.trigger_model( self.resource.id, task_inputs, silent=silent, - ).task_outputs + ) + if response is not None: + return response.task_outputs + return response @property def client(self): @@ -78,17 +82,23 @@ def _update(self): def get_definition(self) -> model_definition_interface.ModelDefinition: return self.resource.model_definition - def get_readme(self, silent: bool = False) -> str: - return self.client.model_service.get_model_card( + def get_readme(self, silent: bool = False) -> Optional[str]: + response = self.client.model_service.get_model_card( self.resource.id, silent=silent, - ).readme + ) + if response is not None: + return response.readme + return response - def get_state(self, silent: bool = False) -> model_interface.Model.State: - return self.client.model_service.watch_model( + def get_state(self, silent: bool = False): + response = self.client.model_service.watch_model( self.resource.id, silent=silent, - ).state + ) + if response is not None: + return response.state + return response def deploy(self, silent: bool = False) -> model_interface.Model.State: self.client.model_service.deploy_model( diff --git a/instill/resources/pipeline.py b/instill/resources/pipeline.py index ccd215a..163a5e3 100644 --- a/instill/resources/pipeline.py +++ b/instill/resources/pipeline.py @@ -1,5 +1,5 @@ # pylint: disable=no-member,wrong-import-position,no-name-in-module -from typing import Tuple, Union +from typing import Optional, Tuple, Union import grpc from google.longrunning import operations_pb2 @@ -36,13 +36,15 @@ def __call__( self, task_inputs: list, silent: bool, - ) -> Tuple[list, pipeline_interface.TriggerMetadata]: + ) -> Optional[Tuple[list, pipeline_interface.TriggerMetadata]]: resp = self.client.pipeline_service.trigger_pipeline( self.resource.id, task_inputs, silent=silent, ) - return resp.outputs, resp.metadata + if resp is not None: + return resp.outputs, resp.metadata + return resp @property def client(self): @@ -64,21 +66,27 @@ def _update(self): self.resource = self.client.pipeline_service.get_pipeline(name=self.resource.id) def get_operation(self, operation: operations_pb2.Operation, silent: bool = False): - return self.client.pipeline_service.get_operation( + response = self.client.pipeline_service.get_operation( operation.name, silent=silent, - ).operation + ) + if response is not None: + return response.operation + return response def trigger_async( self, task_inputs: list, silent: bool = False, ) -> operations_pb2.Operation: - return self.client.pipeline_service.trigger_async_pipeline( + response = self.client.pipeline_service.trigger_async_pipeline( self.resource.id, task_inputs, silent=silent, - ).operation + ) + if response is not None: + return response.operation + return response def get_recipe(self) -> pipeline_interface.Recipe: return self.resource.recipe diff --git a/poetry.lock b/poetry.lock index 717aca4..523c8b9 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand. [[package]] name = "aiohttp" @@ -233,8 +233,8 @@ files = [ lazy-object-proxy = ">=1.4.0" typing-extensions = {version = ">=4.0.0", markers = "python_version < \"3.11\""} wrapt = [ - {version = ">=1.11,<2", markers = "python_version < \"3.11\""}, {version = ">=1.14,<2", markers = "python_version >= \"3.11\""}, + {version = ">=1.11,<2", markers = "python_version < \"3.11\""}, ] [[package]] @@ -758,10 +758,10 @@ isort = ">=4.3.21,<6.0" jinja2 = ">=2.10.1,<4.0" packaging = "*" pydantic = [ - {version = ">=1.5.1,<2.4.0 || >2.4.0,<3.0", extras = ["email"], markers = "python_version < \"3.10\""}, - {version = ">=1.9.0,<2.4.0 || >2.4.0,<3.0", extras = ["email"], markers = "python_version >= \"3.10\" and python_version < \"3.11\""}, {version = ">=1.10.0,<2.0.0 || >2.0.0,<2.0.1 || >2.0.1,<2.4.0 || >2.4.0,<3.0", extras = ["email"], markers = "python_version >= \"3.12\" and python_version < \"4.0\""}, {version = ">=1.10.0,<2.4.0 || >2.4.0,<3.0", extras = ["email"], markers = "python_version >= \"3.11\" and python_version < \"3.12\""}, + {version = ">=1.5.1,<2.4.0 || >2.4.0,<3.0", extras = ["email"], markers = "python_version < \"3.10\""}, + {version = ">=1.9.0,<2.4.0 || >2.4.0,<3.0", extras = ["email"], markers = "python_version >= \"3.10\" and python_version < \"3.11\""}, ] pyyaml = ">=6.0.1" toml = {version = ">=0.10.0,<1.0.0", markers = "python_version < \"3.11\""} @@ -2945,8 +2945,8 @@ files = [ astroid = ">=2.12.13,<=2.14.0-dev0" colorama = {version = ">=0.4.5", markers = "sys_platform == \"win32\""} dill = [ - {version = ">=0.2", markers = "python_version < \"3.11\""}, {version = ">=0.3.6", markers = "python_version >= \"3.11\""}, + {version = ">=0.2", markers = "python_version < \"3.11\""}, ] isort = ">=4.2.5,<6" mccabe = ">=0.6,<0.8" @@ -3151,7 +3151,6 @@ files = [ {file = "PyYAML-6.0.1-cp311-cp311-win_amd64.whl", hash = "sha256:bf07ee2fef7014951eeb99f56f39c9bb4af143d8aa3c21b1677805985307da34"}, {file = "PyYAML-6.0.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:855fb52b0dc35af121542a76b9a84f8d1cd886ea97c84703eaa6d88e37a2ad28"}, {file = "PyYAML-6.0.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:40df9b996c2b73138957fe23a16a4f0ba614f4c0efce1e9406a184b6d07fa3a9"}, - {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a08c6f0fe150303c1c6b71ebcd7213c2858041a7e01975da3a99aed1e7a378ef"}, {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6c22bec3fbe2524cde73d7ada88f6566758a8f7227bfbf93a408a9d86bcc12a0"}, {file = "PyYAML-6.0.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8d4e9c88387b0f5c7d5f281e55304de64cf7f9c0021a3525bd3b1c542da3b0e4"}, {file = "PyYAML-6.0.1-cp312-cp312-win32.whl", hash = "sha256:d483d2cdf104e7c9fa60c544d92981f12ad66a457afae824d146093b8c294c54"}, @@ -3346,8 +3345,8 @@ filelock = "*" frozenlist = "*" gpustat = {version = ">=1.0.0", optional = true, markers = "extra == \"serve\""} grpcio = [ - {version = ">=1.32.0", optional = true, markers = "python_version < \"3.10\" and extra == \"serve\""}, {version = ">=1.42.0", optional = true, markers = "python_version >= \"3.10\" and extra == \"serve\""}, + {version = ">=1.32.0", optional = true, markers = "python_version < \"3.10\" and extra == \"serve\""}, ] jsonschema = "*" msgpack = ">=1.0.0,<2.0.0"