From 06d8dccc16cd6b1694b97960708d1e73c130b7c7 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 18:50:55 +0800 Subject: [PATCH] refine code for isinstance --- metagpt/actions/write_prd.py | 2 +- metagpt/roles/role.py | 2 +- metagpt/roles/searcher.py | 2 +- metagpt/utils/common.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index f087d8650..0febb2656 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -185,7 +185,7 @@ class WritePRD(Action): return if not CONFIG.project_name: - if isinstance(prd, ActionOutput) or isinstance(prd, ActionNode): + if isinstance(prd, (ActionOutput, ActionNode)): ws_name = prd.instruct_content.dict()["Project Name"] else: ws_name = CodeParser.parse_str(block="Project Name", text=prd) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 3a8721004..fa09999e5 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -370,7 +370,7 @@ class Role(BaseModel): async def _act(self) -> Message: logger.info(f"{self._setting}: ready to {self._rc.todo}") response = await self._rc.todo.run(self._rc.important_memory) - if isinstance(response, ActionOutput) or isinstance(response, ActionNode): + if isinstance(response, (ActionOutput, ActionNode)): msg = Message( content=response.content, instruct_content=response.instruct_content, diff --git a/metagpt/roles/searcher.py b/metagpt/roles/searcher.py index 7d58ad922..a5c399f47 100644 --- a/metagpt/roles/searcher.py +++ b/metagpt/roles/searcher.py @@ -60,7 +60,7 @@ class Searcher(Role): logger.info(f"{self._setting}: ready to {self._rc.todo}") response = await self._rc.todo.run(self._rc.memory.get(k=0)) - if isinstance(response, ActionOutput) or isinstance(response, ActionNode): + if isinstance(response, (ActionOutput, ActionNode)): msg = Message( content=response.content, instruct_content=response.instruct_content, diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index 0060950dc..a445c9f31 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -203,7 +203,7 @@ class OutputParser: result = ast.literal_eval(structure_text) # Ensure the result matches the specified data type - if isinstance(result, list) or isinstance(result, dict): + if isinstance(result, (list, dict)): return result raise ValueError(f"The extracted structure is not a {data_type}.")