fix: re-raise AgentRepositoryError directly, address review comments

- Re-raise AgentRepositoryError before the generic except handler so
  security error messages are not swallowed (Cursor Bugbot feedback)
- Move module import to top-level to fix mixed import style warning
- Replace unnecessary lambda with MagicMock class reference
- Add noqa for PERF203 on the new except clause

Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
Devin AI
2026-04-14 07:42:52 +00:00
parent 5e80a9dde6
commit cf153bc413
2 changed files with 8 additions and 6 deletions

View File

@@ -1173,7 +1173,9 @@ def load_agent_from_repository(from_repository: str) -> dict[str, Any]:
else:
attributes[key].append(tool_value)
except Exception as e: # noqa: PERF203
except AgentRepositoryError: # noqa: PERF203
raise
except Exception as e:
raise AgentRepositoryError(
f"Tool {tool['name']} could not be loaded: {e}"
) from e

View File

@@ -9,6 +9,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from pydantic import BaseModel, Field
import crewai.utilities.agent_utils as _agent_utils_mod
from crewai.tools.base_tool import BaseTool
from crewai.utilities.agent_utils import (
ALLOWED_TOOL_MODULE_PREFIXES,
@@ -1223,7 +1224,6 @@ class TestLoadAgentFromRepositoryModuleValidation:
def test_accepts_trusted_crewai_tools_module(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""A tool from 'crewai_tools.*' that is a BaseTool subclass should load fine."""
import crewai.utilities.agent_utils as _mod
class FakeTool(BaseTool):
name: str = "fake"
@@ -1235,12 +1235,12 @@ class TestLoadAgentFromRepositoryModuleValidation:
fake_module = MagicMock()
fake_module.FakeTool = FakeTool
monkeypatch.setattr(_mod, "_print_current_organization", lambda: None)
monkeypatch.setattr(_agent_utils_mod, "_print_current_organization", lambda: None)
monkeypatch.setattr(
_mod, "_create_plus_client_hook", lambda: MagicMock()
_agent_utils_mod, "_create_plus_client_hook", MagicMock
)
monkeypatch.setattr(
_mod.asyncio, "run",
_agent_utils_mod.asyncio, "run",
lambda _coro: self._make_mock_response(
{
"role": "Helper",
@@ -1257,7 +1257,7 @@ class TestLoadAgentFromRepositoryModuleValidation:
),
)
monkeypatch.setattr(
_mod.importlib, "import_module", lambda _name: fake_module
_agent_utils_mod.importlib, "import_module", lambda _name: fake_module
)
result = load_agent_from_repository("my-agent")