fix(lint): bring basedpyright rule counts back under their budget limits

This commit is contained in:
mateo-berri 2026-08-05 10:23:02 -07:00
parent 54fb717de1
commit 469d5126f6
34 changed files with 86 additions and 105 deletions

View file

View file

@ -2150,9 +2150,9 @@ def __getattr__(name: str) -> Any:
# Lazy load encoding from main.py to avoid heavy tiktoken import
if name == "encoding":
from ._lazy_imports import _get_litellm_globals
from ._lazy_imports import get_litellm_globals
_globals = _get_litellm_globals()
_globals = get_litellm_globals()
# Check if already cached
if "encoding" not in _globals:
from .main import encoding as _encoding
@ -2162,9 +2162,9 @@ def __getattr__(name: str) -> Any:
# Lazy load bedrock_tool_name_mappings instance
if name == "bedrock_tool_name_mappings":
from ._lazy_imports import _get_litellm_globals
from ._lazy_imports import get_litellm_globals
_globals = _get_litellm_globals()
_globals = get_litellm_globals()
# Check if already cached
if "bedrock_tool_name_mappings" not in _globals:
from .llms.bedrock.chat.invoke_handler import (
@ -2176,9 +2176,9 @@ def __getattr__(name: str) -> Any:
# Lazy load AzureOpenAIError exception class
if name == "AzureOpenAIError":
from ._lazy_imports import _get_litellm_globals
from ._lazy_imports import get_litellm_globals
_globals = _get_litellm_globals()
_globals = get_litellm_globals()
# Check if already cached
if "AzureOpenAIError" not in _globals:
from .llms.azure.common_utils import AzureOpenAIError as _AzureOpenAIError
@ -2188,9 +2188,9 @@ def __getattr__(name: str) -> Any:
# Lazy load openaiOSeriesConfig instance
if name == "openaiOSeriesConfig":
from ._lazy_imports import _get_litellm_globals
from ._lazy_imports import get_litellm_globals
_globals = _get_litellm_globals()
_globals = get_litellm_globals()
if "openaiOSeriesConfig" not in _globals:
# Import the config class and instantiate it
config_class = __getattr__("OpenAIOSeriesConfig")
@ -2206,9 +2206,9 @@ def __getattr__(name: str) -> Any:
"nvidiaNimEmbeddingConfig": "NvidiaNimEmbeddingConfig",
}
if name in _config_instances:
from ._lazy_imports import _get_litellm_globals
from ._lazy_imports import get_litellm_globals
_globals = _get_litellm_globals()
_globals = get_litellm_globals()
if name not in _globals:
# Import the config class and instantiate it
config_class = __getattr__(_config_instances[name])
@ -2221,9 +2221,9 @@ def __getattr__(name: str) -> Any:
# Lazy load provider_list
if name == "provider_list":
from ._lazy_imports import _get_litellm_globals
from ._lazy_imports import get_litellm_globals
_globals = _get_litellm_globals()
_globals = get_litellm_globals()
# Check if already cached
if "provider_list" not in _globals:
# LlmProviders is eagerly imported above, so we can import it directly
@ -2234,9 +2234,9 @@ def __getattr__(name: str) -> Any:
# Lazy load priority_reservation_settings instance
if name == "priority_reservation_settings":
from ._lazy_imports import _get_litellm_globals
from ._lazy_imports import get_litellm_globals
_globals = _get_litellm_globals()
_globals = get_litellm_globals()
# Check if already cached
if "priority_reservation_settings" not in _globals:
# Import the class and instantiate it
@ -2246,9 +2246,9 @@ def __getattr__(name: str) -> Any:
# Lazy load logging_callback_manager instance
if name == "logging_callback_manager":
from ._lazy_imports import _get_litellm_globals
from ._lazy_imports import get_litellm_globals
_globals = _get_litellm_globals()
_globals = get_litellm_globals()
# Check if already cached
if "logging_callback_manager" not in _globals:
# Import the class and instantiate it
@ -2258,9 +2258,9 @@ def __getattr__(name: str) -> Any:
# Lazy load _service_logger module
if name == "_service_logger":
from ._lazy_imports import _get_litellm_globals
from ._lazy_imports import get_litellm_globals
_globals = _get_litellm_globals()
_globals = get_litellm_globals()
# Check if already cached
if "_service_logger" not in _globals:
# Import the module lazily

View file

@ -54,7 +54,7 @@ from ._lazy_imports_registry import (
)
def _get_litellm_globals() -> dict:
def get_litellm_globals() -> dict:
"""
Get the globals dictionary of the litellm module.
@ -233,7 +233,7 @@ def _generic_lazy_import(name: str, import_map: dict[str, tuple[str, str]], cate
raise AttributeError(f"{category} lazy import: unknown attribute {name!r}")
# Step 2: Get the cache (where we store imported things)
_globals: Final = _get_litellm_globals()
_globals: Final = get_litellm_globals()
# Step 3: If we've already imported it, just return the cached version
if name in _globals:
@ -332,7 +332,7 @@ def _lazy_import_utils_module(name: str) -> Any:
Handler for utils module lazy imports.
This uses a custom implementation because utils module needs to use
_get_utils_globals() instead of _get_litellm_globals() for caching.
_get_utils_globals() instead of get_litellm_globals() for caching.
"""
# Check if this attribute exists in our map
if name not in _UTILS_MODULE_IMPORT_MAP:
@ -379,7 +379,7 @@ def _lazy_import_llm_client_cache(name: str) -> Any:
- "in_memory_llm_clients_cache" is a singleton instance of that class
So we need custom logic to handle both cases.
"""
_globals: Final = _get_litellm_globals()
_globals: Final = get_litellm_globals()
# If already cached, return it
if name in _globals:
@ -412,7 +412,7 @@ def _lazy_import_http_handlers(name: str) -> Any:
- They need configuration (timeout, etc.) from the module globals
- They use factory functions instead of direct instantiation
"""
_globals: Final = _get_litellm_globals()
_globals: Final = get_litellm_globals()
if name == "module_level_aclient":
# Create an async HTTP client using the factory function

View file

@ -180,6 +180,7 @@ def get_audio_file_content_hash(file_obj: FileTypes) -> str:
if isinstance(file_obj, tuple):
if len(file_obj) < 2:
fallback_filename = str(file_obj[0]) if len(file_obj) > 0 else None
file_content_obj = None
else:
fallback_filename = str(file_obj[0]) if file_obj[0] is not None else None
file_content_obj = file_obj[1]
@ -206,7 +207,7 @@ def get_audio_file_content_hash(file_obj: FileTypes) -> str:
except OSError:
fallback_filename = str(file_content_obj)
file_content = None
elif hasattr(file_content_obj, "read"):
elif file_content_obj is not None and hasattr(file_content_obj, "read"):
try:
current_position: Final = file_content_obj.tell() if hasattr(file_content_obj, "tell") else None
if hasattr(file_content_obj, "seek"):

View file

@ -3684,7 +3684,7 @@ def _convert_to_bedrock_tool_call_invoke(
# cache_control applies to the whole original
# tool call; attach after the last split block.
if tool.get("cache_control", None) is not None:
_cache_point_block = litellm.AmazonConverseConfig()._get_cache_point_block(
_cache_point_block = litellm.AmazonConverseConfig().get_cache_point_block(
{"cache_control": tool["cache_control"]},
block_type="content_block",
model=model,
@ -3701,7 +3701,7 @@ def _convert_to_bedrock_tool_call_invoke(
# Check for cache_control and add a separate cachePoint block
if tool.get("cache_control", None) is not None:
cache_point_block = litellm.AmazonConverseConfig()._get_cache_point_block(
cache_point_block = litellm.AmazonConverseConfig().get_cache_point_block(
{"cache_control": tool["cache_control"]},
block_type="content_block",
model=model,
@ -4360,7 +4360,7 @@ class BedrockConverseMessagesProcessor:
elif element["type"] == "document":
_part = BedrockConverseMessagesProcessor._process_document_message(element)
_parts.append(_part)
_cache_point_block = litellm.AmazonConverseConfig()._get_cache_point_block(
_cache_point_block = litellm.AmazonConverseConfig().get_cache_point_block(
message_block=cast(OpenAIMessageContentListBlock, element),
block_type="content_block",
model=model,
@ -4370,7 +4370,7 @@ class BedrockConverseMessagesProcessor:
user_content.extend(_parts)
elif message_block["content"] and isinstance(message_block["content"], str):
_part = BedrockContentBlock(text=messages[msg_i]["content"])
_cache_point_block = litellm.AmazonConverseConfig()._get_cache_point_block(
_cache_point_block = litellm.AmazonConverseConfig().get_cache_point_block(
message_block, block_type="content_block", model=model
)
user_content.append(_part)
@ -4417,7 +4417,7 @@ class BedrockConverseMessagesProcessor:
# Add a separate cachePoint block if cache_control is present
if tool_msg_cache_control is not None:
cache_point_block = litellm.AmazonConverseConfig()._get_cache_point_block(
cache_point_block = litellm.AmazonConverseConfig().get_cache_point_block(
{"cache_control": tool_msg_cache_control},
block_type="content_block",
model=model,
@ -4496,7 +4496,7 @@ class BedrockConverseMessagesProcessor:
assistants_part = await BedrockImageProcessor.process_image_async(image_url=image_url)
assistants_parts.append(assistants_part)
# Add cache point block for assistant content elements
_cache_point_block = litellm.AmazonConverseConfig()._get_cache_point_block(
_cache_point_block = litellm.AmazonConverseConfig().get_cache_point_block(
message_block=cast(OpenAIMessageContentListBlock, element),
block_type="content_block",
model=model,
@ -4510,7 +4510,7 @@ class BedrockConverseMessagesProcessor:
assistant_content.append(BedrockContentBlock(text=_assistant_content))
# If content is empty/whitespace, skip it (don't add a placeholder)
# Add cache point block for assistant string content
_cache_point_block = litellm.AmazonConverseConfig()._get_cache_point_block(
_cache_point_block = litellm.AmazonConverseConfig().get_cache_point_block(
assistant_message_block, block_type="content_block", model=model
)
if _cache_point_block is not None:
@ -4733,7 +4733,7 @@ def _bedrock_converse_messages_pt(
elif element["type"] == "document":
_part = BedrockConverseMessagesProcessor._process_document_message(element)
_parts.append(_part)
_cache_point_block = litellm.AmazonConverseConfig()._get_cache_point_block(
_cache_point_block = litellm.AmazonConverseConfig().get_cache_point_block(
message_block=cast(OpenAIMessageContentListBlock, element),
block_type="content_block",
model=model,
@ -4743,7 +4743,7 @@ def _bedrock_converse_messages_pt(
user_content.extend(_parts)
elif message_block["content"] and isinstance(message_block["content"], str):
_part = BedrockContentBlock(text=messages[msg_i]["content"])
_cache_point_block = litellm.AmazonConverseConfig()._get_cache_point_block(
_cache_point_block = litellm.AmazonConverseConfig().get_cache_point_block(
message_block, block_type="content_block", model=model
)
user_content.append(_part)
@ -4792,7 +4792,7 @@ def _bedrock_converse_messages_pt(
# Add a separate cachePoint block if cache_control is present
if tool_msg_cache_control is not None:
cache_point_block = litellm.AmazonConverseConfig()._get_cache_point_block(
cache_point_block = litellm.AmazonConverseConfig().get_cache_point_block(
{"cache_control": tool_msg_cache_control},
block_type="content_block",
model=model,
@ -4874,7 +4874,7 @@ def _bedrock_converse_messages_pt(
assistants_part = BedrockImageProcessor.process_image_sync(image_url=image_url)
assistants_parts.append(assistants_part)
# Add cache point block for assistant content elements
_cache_point_block = litellm.AmazonConverseConfig()._get_cache_point_block(
_cache_point_block = litellm.AmazonConverseConfig().get_cache_point_block(
message_block=cast(OpenAIMessageContentListBlock, element),
block_type="content_block",
model=model,
@ -4887,7 +4887,7 @@ def _bedrock_converse_messages_pt(
if _assistant_content.strip():
assistant_content.append(BedrockContentBlock(text=_assistant_content))
# Add cache point block for assistant string content
_cache_point_block = litellm.AmazonConverseConfig()._get_cache_point_block(
_cache_point_block = litellm.AmazonConverseConfig().get_cache_point_block(
assistant_message_block, block_type="content_block", model=model
)
if _cache_point_block is not None:

View file

@ -1081,7 +1081,7 @@ class AmazonConverseConfig(BaseConfig):
optional_params["maxTokens"] = thinking_token_budget + DEFAULT_MAX_TOKENS
@overload
def _get_cache_point_block(
def get_cache_point_block(
self,
message_block: OpenAIMessageContentListBlock
| ChatCompletionUserMessage
@ -1093,7 +1093,7 @@ class AmazonConverseConfig(BaseConfig):
pass
@overload
def _get_cache_point_block(
def get_cache_point_block(
self,
message_block: OpenAIMessageContentListBlock
| ChatCompletionUserMessage
@ -1104,7 +1104,7 @@ class AmazonConverseConfig(BaseConfig):
) -> ContentBlock | None:
pass
def _get_cache_point_block(
def get_cache_point_block(
self,
message_block: OpenAIMessageContentListBlock
| ChatCompletionUserMessage
@ -1149,14 +1149,14 @@ class AmazonConverseConfig(BaseConfig):
system_prompt_indices.append(idx)
if isinstance(message["content"], str) and message["content"]:
system_content_blocks.append(SystemContentBlock(text=message["content"]))
cache_block = self._get_cache_point_block(message, block_type="system", model=model)
cache_block = self.get_cache_point_block(message, block_type="system", model=model)
if cache_block:
system_content_blocks.append(cache_block)
elif isinstance(message["content"], list):
for m in message["content"]:
if m.get("type") == "text" and m.get("text"):
system_content_blocks.append(SystemContentBlock(text=m["text"]))
cache_block = self._get_cache_point_block(m, block_type="system", model=model)
cache_block = self.get_cache_point_block(m, block_type="system", model=model)
if cache_block:
system_content_blocks.append(cache_block)
if len(system_prompt_indices) > 0:

View file

@ -40,7 +40,7 @@ class XAIOAuthLoginRequiredError(XAIOAuthError):
class _CallbackHandler(BaseHTTPRequestHandler):
server: "_CallbackServer"
server: "_CallbackServer" # pyright: ignore[reportIncompatibleVariableOverride] # stdlib stubs type server as BaseServer; _CallbackServer is the only server this handler is registered on
def do_GET(self) -> None:
parsed: Final = urlparse(self.path)

View file

@ -9,10 +9,19 @@ MCP Spec Reference:
https://modelcontextprotocol.io/specification/2025-11-25/client/elicitation
"""
from typing import Any, Final, Union
from typing import TYPE_CHECKING, Any, Final, Union
from litellm._logging import verbose_logger
if TYPE_CHECKING:
from mcp.types import (
ElicitRequestFormParams,
ElicitRequestParams,
ElicitRequestURLParams,
ElicitResult,
ErrorData,
)
# Guard imports that require the mcp package
try:
from mcp.types import (

View file

@ -18,7 +18,15 @@ if typing.TYPE_CHECKING:
from fastapi import Request
from mcp.client.session import ClientSession
from mcp.shared.context import RequestContext
from mcp.types import ContentBlock, SamplingMessageContentBlock
from mcp.types import (
ContentBlock,
CreateMessageResult,
CreateMessageResultWithTools,
ErrorData,
SamplingMessageContentBlock,
TextContent,
ToolUseContent,
)
from litellm.proxy._types import UserAPIKeyAuth
from litellm.proxy.utils import ProxyLogging

View file

@ -79,6 +79,8 @@ from litellm.types.utils import CallTypes, StandardLoggingMCPToolCall
from litellm.utils import Rules, client, function_setup
if TYPE_CHECKING:
from mcp.server.session import ServerSession as _McpServerSession
from litellm.proxy._experimental.mcp_server.db import OAuthCredentialPayload
# Short-lived in-memory cache for BYOK credentials.
@ -144,10 +146,6 @@ try:
# Robust auth lookup keyed by session_object.
_session_obj_auth_storage: "weakref.WeakKeyDictionary[Any, MCPAuthenticatedUser]" = weakref.WeakKeyDictionary()
active_mcp_session_var: Final[contextvars.ContextVar[_McpServerSession | None]] = contextvars.ContextVar(
"active_mcp_session", default=None
)
except ImportError as e:
verbose_logger.debug("MCP module not found: %s", e)
MCP_AVAILABLE = False
@ -163,6 +161,10 @@ except ImportError as e:
Server = None
TextResourceContents = None
active_mcp_session_var: Final[contextvars.ContextVar["_McpServerSession | None"]] = contextvars.ContextVar(
"active_mcp_session", default=None
)
# Global variables to track initialization
_SESSION_MANAGERS_INITIALIZED = False

View file

@ -1,11 +1,10 @@
from typing import Any, Dict, Final, List, Literal, Optional, Union
from typing import Dict, Final, Optional, Union
import litellm
from litellm._logging import verbose_proxy_logger
from litellm.caching.caching import DualCache
from litellm.integrations.custom_guardrail import CustomGuardrail
from litellm.proxy._types import UserAPIKeyAuth
from litellm.proxy.guardrails.guardrail_helpers import should_proceed_based_on_metadata
from litellm.types.utils import CallTypesLiteral
# Global counter for tracking which guardrail was called (for load balancing tests)

View file

@ -1,9 +1,7 @@
import time
from typing import Any, Final, Optional
from typing import Final
import litellm
from litellm import CustomLLM, ImageObject, ImageResponse, completion, get_llm_provider
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler
from litellm import CustomLLM
from litellm.types.utils import ModelResponse

View file

@ -1,6 +1,4 @@
from typing import List
from typing_extensions import Dict, Required, TypedDict, override
from typing_extensions import TypedDict
from litellm.integrations.custom_logger import CustomLogger

View file

@ -1,8 +1,6 @@
# Import types from the Google GenAI SDK
from typing import TYPE_CHECKING, Any, Dict, List, Optional, TypeAlias
from typing import TYPE_CHECKING, Any, Dict, Optional
from pydantic import BaseModel
from typing_extensions import TypedDict
from litellm.types.llms.openai import BaseLiteLLMOpenAIResponseObject

View file

@ -1,7 +1,4 @@
import os
from datetime import datetime as dt
from enum import Enum
from typing import Any, Dict, Final, List, Literal, Optional, Set
from typing import Any, Dict, Final, List
from typing_extensions import TypedDict

View file

@ -2,10 +2,10 @@
Type definitions for Anthropic Skills API
"""
from typing import Any, Dict, List, Literal, Optional, Union
from typing import Any, Dict, List, Optional
from pydantic import BaseModel, Field
from typing_extensions import Required, TypedDict
from pydantic import BaseModel
from typing_extensions import TypedDict
# Skills API Request Types

View file

@ -1,4 +1,4 @@
from typing import Any, Dict, Final, Iterable, List, Literal, Optional, Union
from typing import List, Literal
from typing_extensions import Required, TypedDict

View file

@ -1,6 +1,4 @@
from typing import List
from typing_extensions import Dict, Required, TypedDict, override
from typing_extensions import TypedDict
from litellm.llms.custom_llm import CustomLLM

View file

@ -1,19 +1,12 @@
import json
from typing import Any, Dict, Final, List, Literal, Optional, Union
from typing import Any, Dict, List, Literal, Optional, Union
from pydantic import BaseModel
from typing_extensions import (
Protocol,
Required,
Self,
TypedDict,
TypeGuard,
get_origin,
override,
runtime_checkable,
)
from .openai import ChatCompletionToolCallChunk, ChatCompletionUsageBlock
from .openai import ChatCompletionUsageBlock
class GenericStreamingChunk(TypedDict, total=False):

View file

@ -1,16 +1,8 @@
import json
from typing import Any, List, Optional, Union
from typing import List
from pydantic import BaseModel
from typing_extensions import (
Protocol,
Required,
Self,
TypedDict,
TypeGuard,
get_origin,
override,
runtime_checkable,
)

View file

@ -1,6 +1,4 @@
import json
from enum import Enum
from typing import Any, Dict, List, Literal, Optional, Tuple, Union
from typing import Dict
from typing_extensions import TypedDict

View file

@ -1,16 +1,7 @@
import json
from enum import Enum
from typing import Any, Dict, List, Literal, Optional, Tuple, Union
from typing import Optional
from typing_extensions import (
Protocol,
Required,
Self,
TypedDict,
TypeGuard,
get_origin,
override,
runtime_checkable,
)

View file

@ -1,7 +1,6 @@
from typing import Any, Dict, Final, List, Literal, Optional, Union
from typing import Any, Dict, Final, List, Optional
from fastapi import HTTPException
from pydantic import BaseModel, EmailStr, field_validator
from pydantic import BaseModel, field_validator
from litellm.proxy._types import (
LiteLLM_UserTableWithKeyCount,