From 42e93984c31a2c943641630eda544f4a9f539f9c Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Fri, 12 Sep 2025 14:01:09 +0000 Subject: [PATCH] fix: Refactor try-except loop to resolve PERF203 lint issue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: João --- tests/agents/test_crew_agent_parser.py | 29 ++++++++++++++------------ 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/tests/agents/test_crew_agent_parser.py b/tests/agents/test_crew_agent_parser.py index 3bba0d090..57d055456 100644 --- a/tests/agents/test_crew_agent_parser.py +++ b/tests/agents/test_crew_agent_parser.py @@ -1,11 +1,11 @@ import pytest +from crewai.agents import parser from crewai.agents.crew_agent_executor import ( AgentAction, AgentFinish, OutputParserException, ) -from crewai.agents import parser def test_valid_action_parsing_special_characters(): @@ -345,12 +345,16 @@ def test_integration_valid_and_invalid(): """ parts = text.strip().split("\n\n") results = [] - for part in parts: + + def parse_part(part_text): try: - result = parser.parse(part.strip()) - results.append(result) + return parser.parse(part_text.strip()) except OutputParserException as e: - results.append(e) + return e + + for part in parts: + result = parse_part(part) + results.append(result) assert isinstance(results[0], AgentAction) assert isinstance(results[1], AgentFinish) @@ -402,10 +406,10 @@ def test_harmony_format_detection(): """Test that Harmony format is properly detected.""" harmony_text = "<|start|>assistant<|channel|>analysis<|message|>result<|end|>" react_text = "Thought: test\nFinal Answer: result" - + harmony_result = parser.parse(harmony_text) react_result = parser.parse(react_text) - + assert isinstance(harmony_result, parser.AgentFinish) assert isinstance(react_result, parser.AgentFinish) assert harmony_result.output == "result" @@ -424,10 +428,10 @@ def test_automatic_format_detection(): """Test that the parser automatically detects different formats.""" react_action = "Thought: Let's search\nAction: search\nAction Input: query" react_finish = "Thought: Done\nFinal Answer: result" - + harmony_action = '<|start|>assistant<|channel|>commentary to=tool<|message|>{"input": "test"}<|call|>' harmony_finish = "<|start|>assistant<|channel|>analysis<|message|>final result<|end|>" - + assert isinstance(parser.parse(react_action), parser.AgentAction) assert isinstance(parser.parse(react_finish), parser.AgentFinish) assert isinstance(parser.parse(harmony_action), parser.AgentAction) @@ -437,13 +441,13 @@ def test_automatic_format_detection(): def test_format_registry(): """Test the format registry functionality.""" from crewai.agents.parser import _format_registry - + assert 'react' in _format_registry._parsers assert 'harmony' in _format_registry._parsers - + react_text = "Thought: test\nAction: search\nAction Input: query" harmony_text = "<|start|>assistant<|channel|>analysis<|message|>result<|end|>" - + assert _format_registry._parsers['react'].can_parse(react_text) assert _format_registry._parsers['harmony'].can_parse(harmony_text) assert not _format_registry._parsers['react'].can_parse(harmony_text) @@ -452,4 +456,3 @@ def test_format_registry(): def test_backward_compatibility(): """Test that all existing ReAct format tests still pass.""" - pass