Skip to content

Self-hosted AI workspace.

Python

test(tool_execution): stop two tests leaking src.tool_execution into the suite (#2686)

RosenTomov0a2980e9c13d
diff --git a/tests/test_function_call_non_object_args.py b/tests/test_function_call_non_object_args.py
index 5e8cf4675..f96e0cb61 100644
--- a/tests/test_function_call_non_object_args.py
+++ b/tests/test_function_call_non_object_args.py
@@ -1,22 +1,38 @@
import sys
from unittest.mock import MagicMock
-# Clean up any mocks from previous tests to ensure we load real modules
-for mod in ['src.agent_tools', 'src.tool_parsing', 'src.tool_schemas', 'src.tool_execution']:
- sys.modules.pop(mod, None)
-
-# Mock heavy database/model dependencies before importing
-for mod in [
- 'sqlalchemy', 'sqlalchemy.orm', 'sqlalchemy.ext', 'sqlalchemy.ext.declarative',
- 'sqlalchemy.ext.hybrid', 'sqlalchemy.sql', 'sqlalchemy.sql.expression',
- 'src.database', 'core.models', 'core.database', 'core.auth'
-]:
- if mod not in sys.modules:
- sys.modules[mod] = MagicMock()
-
-import pytest
-import src.agent_tools # noqa: F401
-from src.tool_schemas import function_call_to_tool_block
+# This module needs the real agent-tool stack; importing it pulls in heavy
+# DB/auth deps, so we stub those just long enough to import, then restore them.
+# We deliberately do NOT pop src.tool_execution: popping and re-importing it
+# rebinds the `src` package's `tool_execution` attribute, so a later
+# `import src.tool_execution as te` resolves to a different module object than
+# the one its functions live in - which silently breaks tests that monkeypatch
+# it (e.g. test_edit_file's admin gate).
+_ABSENT = object()
+_AGENT_MODULES = ["src.agent_tools", "src.tool_parsing", "src.tool_schemas"]
+_STUBBED = [
+ "sqlalchemy", "sqlalchemy.orm", "sqlalchemy.ext", "sqlalchemy.ext.declarative",
+ "sqlalchemy.ext.hybrid", "sqlalchemy.sql", "sqlalchemy.sql.expression",
+ "src.database", "core.models", "core.database", "core.auth",
+]
+_saved_stubs = {name: sys.modules.get(name, _ABSENT) for name in _STUBBED}
+
+for _mod in _AGENT_MODULES:
+ sys.modules.pop(_mod, None)
+for _mod in _STUBBED:
+ if _mod not in sys.modules:
+ sys.modules[_mod] = MagicMock()
+
+import pytest # noqa: E402
+import src.agent_tools # noqa: E402,F401
+from src.tool_schemas import function_call_to_tool_block # noqa: E402
+
+# Drop the stubs we installed so they do not leak into later tests.
+for _name, _original in _saved_stubs.items():
+ if _original is _ABSENT:
+ sys.modules.pop(_name, None)
+ else:
+ sys.modules[_name] = _original
@pytest.mark.parametrize("arguments", [
diff --git a/tests/test_unknown_tool_calls.py b/tests/test_unknown_tool_calls.py
index bf6e4b64c..9911d61fb 100644
--- a/tests/test_unknown_tool_calls.py
+++ b/tests/test_unknown_tool_calls.py
@@ -1,25 +1,39 @@
import sys
from unittest.mock import MagicMock
-# Clean up any mocks from previous tests to ensure we load real modules
-for mod in ['src.agent_tools', 'src.tool_parsing', 'src.tool_schemas', 'src.tool_execution']:
- sys.modules.pop(mod, None)
-
-# Mock heavy database/model dependencies before importing
-for mod in [
- 'sqlalchemy', 'sqlalchemy.orm', 'sqlalchemy.ext', 'sqlalchemy.ext.declarative',
- 'sqlalchemy.ext.hybrid', 'sqlalchemy.sql', 'sqlalchemy.sql.expression',
- 'src.database', 'core.models', 'core.database', 'core.auth'
-]:
- if mod not in sys.modules:
- sys.modules[mod] = MagicMock()
-
-import pytest
-import src.agent_tools
-from src.tool_parsing import parse_tool_blocks
-from src.tool_schemas import function_call_to_tool_block
-from src.tool_execution import execute_tool_block
-from types import SimpleNamespace
+# This module needs the real agent-tool stack; importing it pulls in heavy
+# DB/auth deps, so we stub those just long enough to import, then restore them.
+# We deliberately do NOT pop src.tool_execution: popping and re-importing it
+# rebinds the `src` package's `tool_execution` attribute, so a later
+# `import src.tool_execution as te` resolves to a different module object than
+# the one its functions live in - which silently breaks tests that monkeypatch
+# it (e.g. test_edit_file's admin gate).
+_ABSENT = object()
+_AGENT_MODULES = ["src.agent_tools", "src.tool_parsing", "src.tool_schemas"]
+_STUBBED = [
+ "sqlalchemy", "sqlalchemy.orm", "sqlalchemy.ext", "sqlalchemy.ext.declarative",
+ "sqlalchemy.ext.hybrid", "sqlalchemy.sql", "sqlalchemy.sql.expression",
+ "src.database", "core.models", "core.database", "core.auth",
+]
+_saved_stubs = {name: sys.modules.get(name, _ABSENT) for name in _STUBBED}
+
+for _mod in _AGENT_MODULES:
+ sys.modules.pop(_mod, None)
+for _mod in _STUBBED:
+ if _mod not in sys.modules:
+ sys.modules[_mod] = MagicMock()
+
+import pytest # noqa: E402
+import src.agent_tools # noqa: E402,F401
+from src.tool_parsing import parse_tool_blocks # noqa: E402
+from src.tool_schemas import function_call_to_tool_block # noqa: E402
+
+# Drop the stubs we installed so they do not leak into later tests.
+for _name, _original in _saved_stubs.items():
+ if _original is _ABSENT:
+ sys.modules.pop(_name, None)
+ else:
+ sys.modules[_name] = _original
def test_parse_xml_unknown_tool_returns_none():