From 4069e3d0d0a0fd0560ab88e58312b66ac9cdb08c Mon Sep 17 00:00:00 2001 From: breedx Date: Mon, 27 Apr 2026 17:18:51 +0000 Subject: [PATCH 1/2] fix(tests): don't stomp real mcp with MagicMocks when mcp is installed tests/plugins/conftest.py unconditionally installed MagicMocks into sys.modules['mcp'], 'mcp.client', and 'mcp.client.session' whenever 'mcp' wasn't already imported. Real mcp is in the dev env, but pytest starts with it not yet imported, so the mock path always won. Later, when tests like tests/agents/test_compaction.py transitively imported pydantic_ai.mcp, its 'from mcp.client.sse import sse_client' blew up with: ModuleNotFoundError: No module named 'mcp.client.sse'; 'mcp.client' is not a package because the installed MagicMock had no __path__. Two-file pytest invocations spanning tests/plugins/* plus any module that touches pydantic_ai.mcp would fail collection. Single-file runs of either worked because tests/plugins/conftest.py wasn't loaded. Fix: try importing real mcp first, only install the mocks when the import fails. Real dev envs skip the mock entirely; bare CI images without mcp still get the compatibility shim. --- tests/plugins/conftest.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/plugins/conftest.py b/tests/plugins/conftest.py index 8cb5e6c08..34c48d5e1 100644 --- a/tests/plugins/conftest.py +++ b/tests/plugins/conftest.py @@ -9,11 +9,23 @@ def pytest_configure(config): """Configure pytest with compatibility workarounds. - Pre-patch sys.modules to provide a mock ``mcp.types`` during collection. - This prevents a ValueError in pydantic's RootModel metaclass when MCP - is not installed in the test environment. + Pre-patch sys.modules to provide a mock ``mcp.types`` during collection, + but only when the real ``mcp`` package is not importable. Previously we + checked ``"mcp" not in sys.modules`` which is true on a clean start — + that installed MagicMocks for ``mcp.client`` etc. and then, when other + tests (e.g. ``tests/agents/test_compaction.py``) transitively imported + ``pydantic_ai.mcp``, its ``from mcp.client.sse import sse_client`` blew + up with "'mcp.client' is not a package" because the MagicMock had no + ``__path__``. + + Real ``mcp`` is installed in this project's dev environment, so the + mock path is only needed in bare CI images. Probe with a real import + first; only fall back to mocks if that fails. """ - if "mcp" not in sys.modules: + try: + import mcp # noqa: F401 + import mcp.types # noqa: F401 + except ImportError: mcp_mock = MagicMock() mcp_mock.types = MagicMock() sys.modules["mcp"] = mcp_mock From 561956eda49b21b05de8ba95b43ecae361bc4c10 Mon Sep 17 00:00:00 2001 From: breedx Date: Mon, 27 Apr 2026 17:22:37 +0000 Subject: [PATCH 2/2] style: reformat lambda for ruff 0.15 CI installs unpinned ruff, which now wraps the negated isinstance lambda differently. --- code_puppy/agents/_runtime.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code_puppy/agents/_runtime.py b/code_puppy/agents/_runtime.py index 0d5e2b281..70a7dc563 100644 --- a/code_puppy/agents/_runtime.py +++ b/code_puppy/agents/_runtime.py @@ -409,8 +409,8 @@ async def run_agent_task() -> Any: except* Exception as other: unexpected = _collect_exceptions( other, - lambda e: not isinstance( - e, (asyncio.CancelledError, UsageLimitExceeded) + lambda e: ( + not isinstance(e, (asyncio.CancelledError, UsageLimitExceeded)) ), ) for exc in unexpected: