From 2df7d47b2a52cf5e14298a43e1c68800bc3177bc Mon Sep 17 00:00:00 2001 From: Daoyuan Li <94409450+DaoyuanLi2816@users.noreply.github.com> Date: Tue, 11 Aug 2026 17:47:42 -0700 Subject: [PATCH] test(llm-error): rename a stand-in, not the shared FakeError (#4744) * test(llm-error): rename a stand-in, not the shared FakeError `exc.__class__.__name__ = "ReadError"` on a `FakeError` instance renames the class itself, so `FakeError` stays named "ReadError" for the rest of the session and every later test asserting error_type == "FakeError" fails. Declaration order hides it: the renaming test runs after its victims. Use the existing _ReadError stand-in, which is already named "ReadError" and is how the sibling _max_attempts_for test builds the same case. Add an autouse fixture so a future slip fails the test that causes it. * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * style: format FakeError guard --------- Co-authored-by: Willem Jiang Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- .../test_llm_error_handling_middleware.py | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/backend/tests/test_llm_error_handling_middleware.py b/backend/tests/test_llm_error_handling_middleware.py index acf480b11..28d220d88 100644 --- a/backend/tests/test_llm_error_handling_middleware.py +++ b/backend/tests/test_llm_error_handling_middleware.py @@ -39,6 +39,25 @@ def _reset_process_limiter() -> Iterator[None]: mod._CAP_RESOLVED = False +@pytest.fixture(autouse=True) +def _guard_shared_exception_name() -> Iterator[None]: + """Fail the test that renames the shared ``FakeError`` class. + + Several tests below drive classification by name (``type(exc).__name__``), + so they set ``exc.__class__.__name__``. That mutates the *class*, not the + instance: doing it to ``FakeError`` renames it for the rest of the session + and every later test asserting ``error_type == "FakeError"`` fails. Use a + dedicated stand-in (``_ReadError`` and friends) instead. Without this guard + the damage only surfaces in whichever test happens to run next. + """ + original_name = FakeError.__name__ + yield + try: + assert FakeError.__name__ == "FakeError", "this test renamed the shared FakeError class; rename a dedicated stand-in such as _ReadError instead of exc.__class__ on FakeError" + finally: + FakeError.__name__ = original_name + + def _make_app_config() -> AppConfig: """Minimal AppConfig for middleware tests; circuit_breaker uses defaults.""" return AppConfig(sandbox=SandboxConfig(use="test")) @@ -810,8 +829,7 @@ def test_user_message_for_read_error_uses_generic_transient_copy() -> None: Regression guard for the #3195 CR feedback. """ middleware = _build_middleware() - exc = FakeError("connection dropped mid-stream") - exc.__class__.__name__ = "ReadError" + exc = _ReadError("connection dropped mid-stream") message = middleware._build_user_message(exc, reason="transient")