From 6a8ac60edfc3ea2690c8620dc379f5f3d2554fe8 Mon Sep 17 00:00:00 2001 From: extrange <29305375+extrange@users.noreply.github.com> Date: Thu, 12 Nov 2026 06:27:26 +0000 Subject: [PATCH] test: satisfy ruff in test_ibkr.py Ruff 0.17 flags four pre-existing issues in the IBKR connection test: unsorted imports (I001), a bare Exception (TRY002), and a silent try/except/pass (S110 + BLE001). The lock-file-maintenance bump to ruff 0.17.4 turns these into a failing lint gate. Sort the imports, raise a dedicated IBKRConnectionError, and catch the OSError the client actually raises while keeping the caught error as the cause. --- tests/test_ibkr.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/tests/test_ibkr.py b/tests/test_ibkr.py index 12890d0..33efbde 100644 --- a/tests/test_ibkr.py +++ b/tests/test_ibkr.py @@ -1,18 +1,28 @@ -from ib_insync import IB import pytest +from ib_insync import IB + +MAX_ATTEMPTS = 3 + + +class IBKRConnectionError(RuntimeError): + """Raised when the IB Gateway API does not accept a connection.""" @pytest.mark.asyncio async def test_ib_connection(): - attempts = 0 ib = IB() - while not ib.isConnected(): - if attempts >= 3: - raise Exception("Failed to connect to IBKR after 3 attempts") + last_error: OSError | None = None + for _ in range(MAX_ATTEMPTS): + if ib.isConnected(): + break try: await ib.connectAsync("localhost", 8888, clientId=0) - except Exception: - pass - attempts += 1 + except OSError as exc: + last_error = exc + + if not ib.isConnected(): + raise IBKRConnectionError( + f"Failed to connect to IBKR after {MAX_ATTEMPTS} attempts" + ) from last_error ib.disconnect()