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 <willem.jiang@gmail.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Daoyuan Li 2026-08-11 17:47:42 -07:00 committed by GitHub
parent 1e8cedb9f4
commit 2df7d47b2a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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")