Import `pydantic_ai.mcp` before the loop in the gh-aw engine tests
Closes #12812
What problem this solves
test_engine_definition.py::test_imported_tools_and_gateway_mcp_tools_both_execute fails whenever its file is the only thing collected, with blockbuster.blockbuster.BlockingError: Blocking call to ScandirIterator.__next__.
The test imports the generated gh-aw wrapper inside a running loop, and that wrapper does from pydantic_ai.mcp import load_mcp_toolsets. That is the first import of jsonschema in the process, and jsonschema_specifications/_core.py walks its schemas package directory on the way in, which the blocking-call detector flags. Full-suite runs only pass because collection imports every test module first and tests/test_mcp.py imports mcp at module level.
The failed half-import also leaves mcp broken for the rest of the process: running this test before tests/harness/azure_durable_task/test_transports.py::test_mcp_discovery_instructions_and_calls makes the latter fail with AttributeError: module 'mcp' has no attribute 'types'.
How it is fixed
pydantic_ai.mcp is imported once at module level, under try_import() so a lane without the mcp extra still collects the file. That import runs during collection, with no loop running, and the wrapper's in-loop from pydantic_ai.mcp import ... becomes a cache hit. Test-only change: production imports the wrapper at process start, outside any loop, so the failure is not reachable there.
Verification
Root cause reproduced on main without any pydantic-ai test in the way (BlockingError: Blocking call to ScandirIterator.__next__ — the exact error from #12805), and clean once the import happens before the loop:
import asyncio
async def run():
from blockbuster import BlockBuster
buster = BlockBuster(['pydantic_ai'])
buster.activate()
try:
import pydantic_ai.mcp # clean once imported at module level
finally:
buster.deactivate()
asyncio.run(run())
pytest tests/harness/gh_aw/test_engine_definition.py --collect-only -q -p no:randomly→ 71 tests collected with the new module-level import.ruff check+ruff format --checkclean.
The gh-aw test itself can't run on this machine: it needs node, and the local node aborts at startup (assert 134 == 0 from the launcher's subprocess.run) on every test in the file, including the ones untouched here. CI is the only place the fixed test can run, so please let the shard confirm it. The mechanism it depends on (the second import of a module being a sys.modules hit) doesn't need the run to be trusted.
Summary by cubic
Fixes the gh-aw engine test failing when run alone because the generated wrapper imports pydantic_ai.mcp inside the running loop, and the first jsonschema import walks a package directory, tripping the blocking-call detector.
- Imports
pydantic_ai.mcpat module level undertry_import(), so the in-loop import becomes a cache hit and collection still works without themcpextra. - Silences the
pyrightunused-import diagnostic for the side-effect-only import, matching the convention intests/conftest.py. - Also stops the failed half-import from leaving
mcpbroken for later tests in the same process.