"""Mount a probe router on the shared app — and actually take it back off. Several gate tests prove a negative ("an ungated route WOULD answer this caller, so the sweep above isn't vacuous") by mounting a throwaway ``APIRouter`` on the real ``app.main:app``. That app is a process-global shared by all ~15k tests, so anything mounted has to come back off. The obvious teardown — filtering ``app.router.routes`` by ``.path`` — is a **no-op** under FastAPI 0.141: ``include_router`` no longer copies the child's routes up, it appends one deferred ``_IncludedRouter`` wrapper that has no ``.path`` at all. The filter kept it, the probe stayed mounted for the rest of the session, and the first later test to call ``app.openapi()`` blew up on the probe's unresolvable annotation. That's how ``test_knowledge_loop``/``test_partner_directory``'s ``test_the_endpoint_list_matches_the_router`` came to pass alone and fail in a full run. So restore the list itself instead of pattern-matching its contents, and drop the memoised OpenAPI schema on both edges — ``app.openapi()`` re-generates only when ``app.router._get_routes_version()`` moves, and that version is a *sum* that mount+unmount returns to its old value. Annotation caveat: an endpoint defined inside a test function still gets its annotations resolved against the **module's** globals (every test module here is ``from __future__ import annotations``, so they are all strings). Import anything you annotate with at module level, or FastAPI will treat it as an unresolved forward ref and turn it into a request-body field. """ from __future__ import annotations from collections.abc import Iterator from contextlib import contextmanager from fastapi import APIRouter, FastAPI @contextmanager def mounted_router(app: FastAPI, router: APIRouter) -> Iterator[None]: """Mount ``router`` on ``app`` for the duration of the block.""" saved = list(app.router.routes) app.include_router(router) app.openapi_schema = None try: yield finally: app.router.routes[:] = saved app.openapi_schema = None __all__ = ["mounted_router"]