Repository navigation
Full-suite pytest collection: partial-module caching poisons initialization/services.* packages across unrelated test files #12463
Description
Summary
Running python3 -m pytest --collect-only -p no:xdist -q across the full autobot-backend/ suite produces collection errors on files that collect cleanly when run standalone or in small targeted groups (confirmed via #12436/#12437 investigation). Root cause is distinct from stub-leak issues previously fixed (#12437, #12452 etc.) — this is Python's own import-caching behavior interacting with pytest's --import-mode=importlib.
Root cause
Several collectors that run early (alphabetically/directory order) in a full-suite run fail mid-import with unrelated pre-existing errors, e.g.:
initialization/lifespan_test.pyfails viafastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that <MagicMock ...> is a valid Pydantic field typewhile executinginitialization/__init__.py→.routers→.router_registry→core_routers.py→api.approval_gates.agents/agent_optimizer_test.py,training/completion_trainer_test.py, and others fail similarly for unrelated reasons.
Because Python inserts a module into sys.modules before exec_module() runs, a mid-import exception leaves a half-populated module object cached under that name (e.g. sys.modules["initialization"] exists but is missing names defined after the failing import line, like load_core_routers).
Every subsequent test file in the same collection session that does from initialization import (load_core_routers, ...) (or similar for services.mesh_brain, services.agents, services.slm_client, services.llm_service) gets the broken cached module instead of a fresh import attempt, and fails with a confusing, unrelated-looking error:
ImportError: cannot import name 'load_core_routers' from 'initialization'
ModuleNotFoundError: No module named 'services.mesh_brain'
ModuleNotFoundError: No module named 'services.agents'
ImportError: cannot import name '_SERVICE_JWT_TTL_HOURS' from 'services.slm_client'
ImportError: cannot import name 'LLMService' from 'services.llm_service'
Confirmed files affected by this cascade in a full-suite --collect-only run (as of this filing):
mcp/mcp_security_test.py, system_integration_test.py, security/security_integration_test.py, performance_benchmarks_test.py, services/slm_client_test.py, services/documentation_watcher_staleness_test.py, tests/agents/test_subagent_spawning.py.
Verified NOT caused by #12436/#12437: all 7 files above collect cleanly (0 errors, 147 tests) when run together in an isolated group without the other early-failing collectors present. The cascade only appears in a full-suite run, once an earlier collector (e.g. initialization/lifespan_test.py) partially imports and fails.
Suggested fix directions (needs design decision — filing rather than fixing to keep #12436/#12437's PR scoped)
- Fix the primary failures first (the
fastapi.exceptions.FastAPIError: Invalid args for response field! ... <MagicMock ...>errors ininitialization/lifespan_test.py,judges/judge_integration_test.py,api/collaboration_test.py,api/marketplace_test.py,api/telegram_bot_test.py,api/whatsapp_test.py,tests/api/test_advanced_control_takeover_enum_12208.py,tests/api/test_marketplace.py,tests/test_diagnostics_router_registration.py— likely a stub inconftest.pyleaking aMagicMockinto a route's response-model type annotation). Fixing these removes the poisoning trigger for the whole cascade. - Independently, consider a pytest hook (
pytest_collectstart/pytest_exception_interact, or apytest_collectreporthandler) that evicts anysys.modulesentry for a module whoseexec_module()raised during collection, so subsequent files get a clean re-import attempt rather than a poisoned cache.
Evidence
Full-repo collect-only baseline (before #12436/#12437 fix): 19266 tests collected, 27 errors.
After #12436/#12437 fix (only those 2 files' errors removed): 19292 tests collected, 25 errors — this issue accounts for the remaining pre-existing errors not touched by that PR.
Related: #12436, #12437 (test collector fixes, PR resolves those; this issue is a separate discovery from that investigation).
Activity
fix(conftest): narrow collection-eviction hook to uninitialized/stub …
fix(test-infra): full-suite pytest collection cascade — MagicMock res…
mrveiss commentedon Jul 25, 2026
Fixed in #12475 (merged): real-loaded api.schemas_workflows (killed the shared-MagicMock sys.modules poison), guarded llc/tests services stub, added a narrowed pytest collection-eviction hook (uninitialized/stub modules only). Full-suite collection 25→6 errors (remaining are #12455 dead tests + a scanner NameError + local-only dep gaps).
github-actions commentedon Jul 25, 2026
PR #12475 (merged to
Dev_new_gui) references this issue with a close keyword.If this issue is fully resolved, close it manually. If work remains, no action is needed.
test(testkit): add the canonical sys.modules StubSet helper (#13451) (#…