From 5ca8c0154fcc634a7cd3c3e4d7655fd47dcf2c17 Mon Sep 17 00:00:00 2001 From: icn5381 <255778606+icn5381@users.noreply.github.com> Date: Mon, 17 Aug 2026 13:49:43 +0800 Subject: [PATCH] Narrow img_data before format detection to clear the basedpyright budget The None-tolerant flow left img_data Optional at every subscript in the format-detection branches, adding ten reportOptionalSubscript errors against a zero-error budget. An early return on None (plus an explicit annotation) narrows the type before detection; complexity returns to the upstream baseline, so the C901 budget stays level too. --- litellm/litellm_core_utils/token_counter.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/litellm/litellm_core_utils/token_counter.py b/litellm/litellm_core_utils/token_counter.py index bb34986d9b9..f11a85fe64e 100644 --- a/litellm/litellm_core_utils/token_counter.py +++ b/litellm/litellm_core_utils/token_counter.py @@ -212,7 +212,7 @@ def get_image_dimensions( Returns: Tuple[int, int]: The width and height of the image. """ - img_data = None + img_data: bytes | None = None if data.startswith(("http://", "https://")): try: client: Final = _get_httpx_client() @@ -233,8 +233,11 @@ def get_image_dimensions( img_data = _decode_base64_image(data) # A URL that could not be fetched (or base64 that could not be decoded) - # leaves img_data unset — fall through to the default dimensions below. - img_type: Final = get_image_type(img_data) if img_data is not None else None + # leaves img_data unset — return the default dimensions. + if img_data is None: + return DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_HEIGHT + + img_type: Final = get_image_type(img_data) if img_type == "png": w, h = struct.unpack(">LL", img_data[16:24])