From d0e4c356e1010eeefff7017e7328d1592ed323b0 Mon Sep 17 00:00:00 2001 From: lorenzejay Date: Thu, 8 Jan 2026 16:13:07 -0800 Subject: [PATCH] refactor: improve code readability in environment tools Refactored the file_search_tool.py, grep_tool.py, and list_dir_tool.py to enhance code readability. Changes include formatting list comprehensions for better clarity, adjusting error handling for consistency, and removing unnecessary imports. These improvements aim to streamline the codebase and maintain a clean structure for future development. --- .../environment_tools/file_search_tool.py | 4 +++- .../experimental/environment_tools/grep_tool.py | 16 +++++++++------- .../environment_tools/list_dir_tool.py | 2 -- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/crewai/src/crewai/experimental/environment_tools/file_search_tool.py b/lib/crewai/src/crewai/experimental/environment_tools/file_search_tool.py index e51326ced..79209a0e5 100644 --- a/lib/crewai/src/crewai/experimental/environment_tools/file_search_tool.py +++ b/lib/crewai/src/crewai/experimental/environment_tools/file_search_tool.py @@ -91,7 +91,9 @@ Examples: matches = [m for m in matches if m.is_dir()] # Filter out hidden files - matches = [m for m in matches if not any(part.startswith(".") for part in m.parts)] + matches = [ + m for m in matches if not any(part.startswith(".") for part in m.parts) + ] # Sort alphabetically matches.sort(key=lambda x: str(x).lower()) diff --git a/lib/crewai/src/crewai/experimental/environment_tools/grep_tool.py b/lib/crewai/src/crewai/experimental/environment_tools/grep_tool.py index 5a85ceae5..cfbc0de7f 100644 --- a/lib/crewai/src/crewai/experimental/environment_tools/grep_tool.py +++ b/lib/crewai/src/crewai/experimental/environment_tools/grep_tool.py @@ -120,7 +120,8 @@ Examples: output = result.stdout # Count actual match lines (not context lines) match_lines = [ - line for line in output.split("\n") + line + for line in output.split("\n") if line and not line.startswith("--") ] match_count = len(match_lines) @@ -129,18 +130,19 @@ Examples: header += "=" * 60 + "\n" return header + output - elif result.returncode == 1: + if result.returncode == 1: # No matches found (grep returns 1 for no matches) return f"No matches found for '{pattern}' in {path}" - else: - # Error occurred - error_msg = result.stderr.strip() if result.stderr else "Unknown error" - return f"Error: {error_msg}" + # Error occurred + error_msg = result.stderr.strip() if result.stderr else "Unknown error" + return f"Error: {error_msg}" except subprocess.TimeoutExpired: return "Error: Search timed out (>30s). Try narrowing the search path." except FileNotFoundError: - return "Error: grep command not found. Ensure grep is installed on the system." + return ( + "Error: grep command not found. Ensure grep is installed on the system." + ) except Exception as e: return f"Error during search: {e}" diff --git a/lib/crewai/src/crewai/experimental/environment_tools/list_dir_tool.py b/lib/crewai/src/crewai/experimental/environment_tools/list_dir_tool.py index 357da6252..a4513aa79 100644 --- a/lib/crewai/src/crewai/experimental/environment_tools/list_dir_tool.py +++ b/lib/crewai/src/crewai/experimental/environment_tools/list_dir_tool.py @@ -2,8 +2,6 @@ from __future__ import annotations -from pathlib import Path - from pydantic import BaseModel, Field from crewai.experimental.environment_tools.base_environment_tool import (