From 1175559c397b9f35faaca8622b5165d786d2a23b Mon Sep 17 00:00:00 2001 From: "devin-ai-integration[bot]" <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Wed, 23 Sep 2026 17:50:09 -0700 Subject: [PATCH] feat(lint): add LIT013 flagging *-ok suppressions that suppress nothing and remove the 240 stale ones (#42793) --- litellm/_logging.py | 4 +- .../transformation.py | 4 +- litellm/experimental_mcp_client/client.py | 6 +- litellm/integrations/custom_guardrail.py | 2 +- .../integrations/newrelic/newrelic_metrics.py | 4 +- .../integrations/otel/plumbing/providers.py | 4 +- .../integrations/otel/presets/destinations.py | 2 +- litellm/integrations/prometheus.py | 2 +- litellm/integrations/shadow_eval_logger.py | 7 +- .../interactions/background_cost_polling.py | 4 +- litellm/litellm_core_utils/core_helpers.py | 2 +- .../get_supported_openai_params.py | 4 +- .../json_fragment_accumulator.py | 32 +- litellm/litellm_core_utils/litellm_logging.py | 2 +- .../convert_dict_to_response.py | 4 +- .../prompt_templates/common_utils.py | 4 +- .../litellm_core_utils/provider_affinity.py | 2 +- .../streaming_chunk_builder_utils.py | 4 +- .../chat/guardrail_translation/handler.py | 44 +- litellm/llms/anthropic/common_utils.py | 2 +- .../messages/response_cache.py | 4 +- .../messages/streaming_iterator.py | 2 +- .../responses_adapters/transformation.py | 6 +- .../llms/azure/passthrough/transformation.py | 4 +- .../image_generation/flux_transformation.py | 4 +- .../base_llm/passthrough/transformation.py | 4 +- .../bedrock/messages/mantle_transformation.py | 2 +- litellm/llms/bedrock/realtime/handler.py | 4 +- .../flux_lora_depth_transformation.py | 6 +- .../llms/fal_ai/image_edit/transformation.py | 6 +- .../gpt_image_2_transformation.py | 8 +- litellm/llms/gigachat/authenticator.py | 2 +- litellm/llms/gigachat/chat/streaming.py | 8 +- .../llms/mistral/batches/transformation.py | 2 +- .../mongodb/vector_stores/transformation.py | 2 +- .../nvidia_nim/passthrough/transformation.py | 2 +- .../llms/nvidia_nim/rerank/transformation.py | 2 +- .../llms/openai/chat/gpt_transformation.py | 4 +- litellm/llms/openai/openai.py | 8 +- litellm/llms/snowflake/chat/transformation.py | 34 +- .../text_to_speech/transformation.py | 16 +- .../xai/audio_transcription/transformation.py | 4 +- litellm/ocr/main.py | 4 +- litellm/passthrough/main.py | 12 +- .../_experimental/mcp_server/contracts.py | 16 +- litellm/proxy/_experimental/mcp_server/db.py | 2 - .../mcp_server/legacy_callbacks.py | 4 +- .../_experimental/mcp_server/mcp_debug.py | 8 +- .../_experimental/mcp_server/operations.py | 4 +- .../_experimental/mcp_server/tool_search.py | 6 +- .../proxy/agent_endpoints/agent_registry.py | 8 +- litellm/proxy/auth/auth_utils.py | 2 +- litellm/proxy/client/cli/commands/agents.py | 4 +- .../client/cli/commands/claude_settings.py | 2 +- .../client/cli/commands/codex_settings.py | 1 - litellm/proxy/client/cli/commands/pi.py | 6 +- .../auth_cache_invalidation_pubsub.py | 8 +- .../proxy/common_utils/reset_budget_job.py | 20 +- litellm/proxy/common_utils/sse_keepalive.py | 8 +- litellm/proxy/db/baseline_accounting.py | 6 +- litellm/proxy/db/shadow_eval_funnel.py | 2 +- .../guardrails/guardrail_hooks/alice/alice.py | 2 +- .../guardrail_hooks/bedrock_guardrails.py | 10 +- .../guardrails/guardrail_hooks/presidio.py | 2 +- .../proxy/hooks/autorouter_baseline_cache.py | 6 +- litellm/proxy/hooks/batch_rate_limiter.py | 6 +- .../hooks/parallel_request_limiter_v3.py | 4 +- litellm/proxy/litellm_pre_call_utils.py | 8 +- .../auto_router_endpoints.py | 6 +- .../config_override_endpoints.py | 8 +- .../management_v1/budgets.py | 2 +- .../mcp_management_endpoints.py | 8 +- .../management_endpoints/scim/scim_v2.py | 1 - .../management_endpoints/team_endpoints.py | 6 +- .../management_helpers/bulk_user_creation.py | 2 +- .../batch_guardrails.py | 6 +- .../llm_passthrough_endpoints.py | 16 +- .../vertex_passthrough_logging_handler.py | 4 +- .../managed_id_rewriter.py | 8 +- .../pass_through_endpoints.py | 4 +- .../streaming_handler.py | 4 +- .../proxy/policy_engine/pipeline_executor.py | 10 +- litellm/proxy/proxy_server.py | 10 +- litellm/proxy/rag_endpoints/endpoints.py | 2 +- .../proxy/response_api_endpoints/endpoints.py | 6 +- .../spend_tracking/carried_budget_state.py | 4 +- litellm/proxy/utils.py | 21 +- litellm/rerank_api/main.py | 4 +- litellm/responses/additional_tools.py | 7 +- .../transformation.py | 4 +- litellm/responses/streaming_iterator.py | 12 +- litellm/responses/utils.py | 2 +- litellm/router.py | 30 +- .../complexity_router/complexity_router.py | 4 +- litellm/router_strategy/tag_based_routing.py | 4 +- .../auto_router_tuning_baseline.py | 2 +- .../router_utils/fallback_event_handlers.py | 2 +- litellm/rust_bridge/lifecycle.py | 2 +- litellm/rust_bridge/logger.py | 2 +- .../auto_router_endpoints.py | 2 +- .../managed_id_rewriter.py | 4 +- litellm/types/utils.py | 8 +- litellm/utils.py | 12 +- scripts/check_type_discipline.py | 419 ++++++++++-------- scripts/type_discipline_gate.py | 36 +- tests/e2e/lifecycle.py | 7 +- tests/e2e/load/proxy_usage.py | 2 +- tests/integration/conftest.py | 1 - .../test_exception_handler_reconnect_retry.py | 21 +- .../guardrail_hooks/test_conduct.py | 8 +- .../test_check_type_discipline.py | 104 +++-- tests/test_litellm_rust/support/isolation.py | 4 +- tests/unit/messages/test_dispatch.py | 4 +- type-discipline-budget.json | 3 + 114 files changed, 540 insertions(+), 732 deletions(-) diff --git a/litellm/_logging.py b/litellm/_logging.py index 802b01b2e90..c65795babff 100644 --- a/litellm/_logging.py +++ b/litellm/_logging.py @@ -631,9 +631,9 @@ class LevelRoutingStreamHandler(logging.StreamHandler): ) preferred: Final = sys.stdout if is_stdout_record else sys.stderr if preferred is None or getattr(preferred, "closed", False): - self.stream = sys.stderr # rebind-ok: fall back to the pre-fix stream rather than raising per record + self.stream = sys.stderr else: - self.stream = preferred # rebind-ok: StreamHandler.emit writes self.stream under the handler lock + self.stream = preferred super().emit(record) diff --git a/litellm/completion_extras/litellm_responses_transformation/transformation.py b/litellm/completion_extras/litellm_responses_transformation/transformation.py index 4c321b12573..31af5a144eb 100644 --- a/litellm/completion_extras/litellm_responses_transformation/transformation.py +++ b/litellm/completion_extras/litellm_responses_transformation/transformation.py @@ -191,8 +191,6 @@ def _as_chat_reasoning_items( ) -> list[ChatCompletionReasoningItem] | None: if not reasoning_items: return None - # cast-ok: _BuiltReasoningItem is the structural shape ChatCompletionReasoningItem - # describes, and TypedDict invariance is what stops the two from unifying here. return cast(list[ChatCompletionReasoningItem], list(reasoning_items)) @@ -1370,7 +1368,7 @@ class OpenAiResponsesToChatCompletionStreamIterator(BaseModelResponseIterator): if tool_call_index_map is None: return output_index if output_index not in tool_call_index_map: - tool_call_index_map[output_index] = len(tool_call_index_map) # mutable-ok: per-stream accumulator state + tool_call_index_map[output_index] = len(tool_call_index_map) return tool_call_index_map[output_index] @staticmethod diff --git a/litellm/experimental_mcp_client/client.py b/litellm/experimental_mcp_client/client.py index 1ccae8de35f..1206f9abcbd 100644 --- a/litellm/experimental_mcp_client/client.py +++ b/litellm/experimental_mcp_client/client.py @@ -764,7 +764,7 @@ class MCPClient: follow_redirects=True, event_hooks=MappingProxyType( {"response": [capture_upstream_error_response], "request": [guard] if guard else []} - ), # mutable-ok: httpx types require lists of hooks + ), ) return factory @@ -921,9 +921,7 @@ class MCPClient: with anyio.fail_after(max(self.timeout, MCP_TOOL_LISTING_TIMEOUT)): for page_index in range(MCP_TOOL_LISTING_MAX_PAGES): try: - page = await fetch_page( # rebind-ok: each SDK page replaces the previous one - None if cursor is None else PaginatedRequestParams(cursor=cursor) - ) + page = await fetch_page(None if cursor is None else PaginatedRequestParams(cursor=cursor)) except MCPError as error: if page_index > 0 and error.error.code == METHOD_NOT_FOUND: raise RuntimeError("MCP list operation became unavailable during pagination") from error diff --git a/litellm/integrations/custom_guardrail.py b/litellm/integrations/custom_guardrail.py index ffa0bc36f6b..5d64eff526b 100644 --- a/litellm/integrations/custom_guardrail.py +++ b/litellm/integrations/custom_guardrail.py @@ -1641,5 +1641,5 @@ def log_guardrail_information(func): return async_wrapper(*args, **kwargs) return sync_wrapper(*args, **kwargs) - vars(wrapper)[LOGS_GUARDRAIL_INFORMATION_MARKER] = True # rebind-ok: stamps the wrapper this call just built + vars(wrapper)[LOGS_GUARDRAIL_INFORMATION_MARKER] = True return wrapper diff --git a/litellm/integrations/newrelic/newrelic_metrics.py b/litellm/integrations/newrelic/newrelic_metrics.py index da952b78d3f..0a45a7e52c3 100644 --- a/litellm/integrations/newrelic/newrelic_metrics.py +++ b/litellm/integrations/newrelic/newrelic_metrics.py @@ -366,9 +366,7 @@ class NewRelicMetricsLogger(CustomBatchLogger): error to keep the client-error path (drop) distinct from 5xx (retry).""" payload: Final = build_metric_payload(records=batch, window_start=window_start, now=time.time()) try: - status = ( - await self.async_send_compressed_data(payload) - ).status_code # rebind-ok: reassigned from the raised HTTPStatusError below + status = (await self.async_send_compressed_data(payload)).status_code except HTTPStatusError as e: status = e.response.status_code except Exception as e: # noqa: BLE001 # transport/network failure re-queues the batch diff --git a/litellm/integrations/otel/plumbing/providers.py b/litellm/integrations/otel/plumbing/providers.py index e3474edaf14..8bac36aad76 100644 --- a/litellm/integrations/otel/plumbing/providers.py +++ b/litellm/integrations/otel/plumbing/providers.py @@ -353,7 +353,7 @@ class _DrainPool: def _drain_until_closed(self) -> None: while True: - processor: SpanProcessor | None = self._pending.get() # rebind-ok: loop variable + processor: SpanProcessor | None = self._pending.get() if processor is None: return _shutdown_quietly(processor) @@ -572,7 +572,7 @@ class TenantFanOutSpanProcessor(SpanProcessor): span, destination.span_scope ): continue - processor = self._acquire(destination) # rebind-ok: loop variable; pyright forbids Final in a loop + processor = self._acquire(destination) if processor is None: continue try: diff --git a/litellm/integrations/otel/presets/destinations.py b/litellm/integrations/otel/presets/destinations.py index 63801e623af..e6cb775af1d 100644 --- a/litellm/integrations/otel/presets/destinations.py +++ b/litellm/integrations/otel/presets/destinations.py @@ -151,7 +151,7 @@ def destination_for( endpoint, protocol = resolved return OtelDestination( endpoint=endpoint, - headers=MappingProxyType(dict(headers)), # mutable-ok: MappingProxyType needs a concrete mapping to wrap + headers=MappingProxyType(dict(headers)), resource_attributes=MappingProxyType({"service.name": service_name}) if service_name else _NO_ATTRS, callback_name=callback_name, protocol=protocol, diff --git a/litellm/integrations/prometheus.py b/litellm/integrations/prometheus.py index d62a6c3427a..2fdcb8ef745 100644 --- a/litellm/integrations/prometheus.py +++ b/litellm/integrations/prometheus.py @@ -131,7 +131,7 @@ def _paginated_table(repository: BaseRepository[_TableRowT]) -> _PaginatedPrisma """View a repository's prisma table through the pagination surface budget metrics need.""" return cast( _PaginatedPrismaTable[_TableRowT], - repository.table, # cast-ok: prisma rows carry the budget columns the domain model declares + repository.table, ) diff --git a/litellm/integrations/shadow_eval_logger.py b/litellm/integrations/shadow_eval_logger.py index 84106b77a7d..19d9bee7493 100644 --- a/litellm/integrations/shadow_eval_logger.py +++ b/litellm/integrations/shadow_eval_logger.py @@ -873,7 +873,6 @@ class ShadowEvalLogger(CustomLogger): await prisma.db.litellm_shadowevalattempt.group_by( by=["job_id"], count=True, - # mutable-ok: Prisma aggregate spec sum={"judge_cost": True, "shadow_cost": True, "shadow_classifier_cost": True}, where={"job_id": {"in": [str(record.id) for record in records]}}, # mutable-ok: Prisma filter ) @@ -901,7 +900,7 @@ class ShadowEvalLogger(CustomLogger): {target: tuple(job for _, job in group) for target, group in groupby(by_target, key=itemgetter(0))} ) await self._jobs_cache.async_set_cache(_JOBS_CACHE_KEY, jobs) - self._job_starts = {} # rebind-ok: new generation, counts absorbed into the fill + self._job_starts = {} return jobs except Exception as e: # noqa: BLE001 # a DB blip must never break request logging verbose_logger.debug("shadow_eval: active-job read failed: %s", e) @@ -1033,7 +1032,7 @@ class ShadowEvalLogger(CustomLogger): real_cache_hit=real_cache_hit, control_tier=control_tier, shadow_params=shadow_params, - parent_metadata=MappingProxyType(dict(request_metadata)), # mutable-ok: frozen snapshot + parent_metadata=MappingProxyType(dict(request_metadata)), ) ).add_done_callback(self._release_shadow_slot) except Exception as e: # noqa: BLE001 # logging hooks must never fail the request @@ -1347,7 +1346,7 @@ class ShadowEvalLogger(CustomLogger): { "role": "user", "content": _judge_user_prompt(conversation, response_a, response_b, _tool_definitions_text(tools)), - }, # mutable-ok: SDK message + }, ] try: response: Final = await judge_acompletion( diff --git a/litellm/interactions/background_cost_polling.py b/litellm/interactions/background_cost_polling.py index 51325354e7d..b48c7c03573 100644 --- a/litellm/interactions/background_cost_polling.py +++ b/litellm/interactions/background_cost_polling.py @@ -79,9 +79,7 @@ async def _fetch_interaction(context: BackgroundInteractionPollContext) -> Inter custom_llm_provider=context.custom_llm_provider, api_key=context.api_key, api_base=context.api_base, - **{ - "no-log": True - }, # mutable-ok: "no-log" is not a valid identifier, so it can only be passed through a mapping + **{"no-log": True}, ) diff --git a/litellm/litellm_core_utils/core_helpers.py b/litellm/litellm_core_utils/core_helpers.py index d7fbe9f7e09..a2d40279c49 100644 --- a/litellm/litellm_core_utils/core_helpers.py +++ b/litellm/litellm_core_utils/core_helpers.py @@ -764,4 +764,4 @@ def set_response_cost_in_hidden_params(response: _CarriesHiddenParams, cost: flo **(additional_headers if isinstance(additional_headers, Mapping) else _NO_HEADERS), RESPONSE_COST_HEADER: cost, } - hidden_params["additional_headers"] = merged # rebind-ok: the caller's record is the point + hidden_params["additional_headers"] = merged diff --git a/litellm/litellm_core_utils/get_supported_openai_params.py b/litellm/litellm_core_utils/get_supported_openai_params.py index 08b8816e17d..680f31a797f 100644 --- a/litellm/litellm_core_utils/get_supported_openai_params.py +++ b/litellm/litellm_core_utils/get_supported_openai_params.py @@ -32,9 +32,7 @@ def get_supported_openai_params( - None if unmapped """ if not custom_llm_provider: - custom_llm_provider = declared_authenticating_provider( - model - ) # rebind-ok: resolving would run the provider's OAuth flow + custom_llm_provider = declared_authenticating_provider(model) if not custom_llm_provider: try: custom_llm_provider = litellm.get_llm_provider(model=model)[1] diff --git a/litellm/litellm_core_utils/json_fragment_accumulator.py b/litellm/litellm_core_utils/json_fragment_accumulator.py index 81d18dd0119..e262f05932c 100644 --- a/litellm/litellm_core_utils/json_fragment_accumulator.py +++ b/litellm/litellm_core_utils/json_fragment_accumulator.py @@ -21,20 +21,18 @@ class JSONFragmentAccumulator: def __init__(self) -> None: self._chunks: list[str] = [] # mutable-ok: O(1) append; string concat would copy the buffer each time - self._buffer: str = ( - "" # mutable-ok: lazily materialized join of _chunks, rebuilt only when _chunks is non-empty - ) - self._offset: int = 0 # mutable-ok: cursor past already-consumed values; avoids re-slicing on every pop - self._could_close: bool = False # mutable-ok: cached heuristic; rescanning past fragments was itself O(n^2) + self._buffer: str = "" + self._offset: int = 0 + self._could_close: bool = False def __bool__(self) -> bool: return bool(self._chunks) or self._offset < len(self._buffer) def append(self, fragment: str) -> None: - self._chunks.append(fragment) # mutable-ok: see __init__ + self._chunks.append(fragment) stripped: Final = fragment.rstrip() if stripped: - self._could_close = stripped[-1] in ("}", "]") # mutable-ok: see __init__ + self._could_close = stripped[-1] in ("}", "]") def could_close_json(self) -> bool: """ @@ -50,8 +48,8 @@ class JSONFragmentAccumulator: if not self._chunks: return unconsumed: Final = self._buffer[self._offset :] - self._buffer = unconsumed + "".join(self._chunks) # mutable-ok: merge pending fragments, once per append batch - self._offset = 0 # mutable-ok: see __init__ + self._buffer = unconsumed + "".join(self._chunks) + self._offset = 0 self._chunks = [] # mutable-ok: see __init__ def pop_next_value(self) -> tuple[bool, object]: @@ -69,7 +67,7 @@ class JSONFragmentAccumulator: while start < length and self._buffer[start].isspace(): start += 1 if start >= length: - self._offset = start # mutable-ok: see __init__ + self._offset = start return False, None decoder: Final = json.JSONDecoder() try: @@ -77,11 +75,11 @@ class JSONFragmentAccumulator: except json.JSONDecodeError: return False, None decoded, end_index = cast("tuple[object, int]", raw_value) # cast-ok: raw_decode returns tuple[Any, int] - self._offset = end_index # mutable-ok: see __init__ + self._offset = end_index if self._offset >= len(self._buffer): - self._buffer = "" # mutable-ok: see __init__ - self._offset = 0 # mutable-ok: see __init__ - self._could_close = False # mutable-ok: buffer is empty, nothing can close + self._buffer = "" + self._offset = 0 + self._could_close = False return True, decoded def snapshot(self) -> str: @@ -91,7 +89,7 @@ class JSONFragmentAccumulator: def set(self, value: str) -> None: """Replace the buffer's contents with a single fragment.""" self._chunks = [] # mutable-ok: see __init__ - self._buffer = value # mutable-ok: see __init__ - self._offset = 0 # mutable-ok: see __init__ + self._buffer = value + self._offset = 0 stripped: Final = value.rstrip() - self._could_close = bool(stripped) and stripped[-1] in ("}", "]") # mutable-ok: see __init__ + self._could_close = bool(stripped) and stripped[-1] in ("}", "]") diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py index b038762a476..0603414cabd 100644 --- a/litellm/litellm_core_utils/litellm_logging.py +++ b/litellm/litellm_core_utils/litellm_logging.py @@ -6667,7 +6667,7 @@ def get_standard_logging_object_payload( "version": 3, "status": "unknown", "reason": "pending_projection", - } # mutable-ok: spend-log JSON serialization requires plain mappings + } if captured_baseline is not None else ( { # mutable-ok: spend-log JSON serialization requires plain mappings diff --git a/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py b/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py index 87524d86c61..9ea730a873f 100644 --- a/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py +++ b/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py @@ -372,9 +372,7 @@ from collections import defaultdict def _handle_invalid_parallel_tool_calls( - tool_calls: list[ - ChatCompletionMessageToolCall | ChatCompletionMessageCustomToolCall - ], # mutable-ok: patched in place via slice assignment + tool_calls: list[ChatCompletionMessageToolCall | ChatCompletionMessageCustomToolCall], ): """ Handle hallucinated parallel tool call from openai - https://community.openai.com/t/model-tries-to-call-unknown-function-multi-tool-use-parallel/490653 diff --git a/litellm/litellm_core_utils/prompt_templates/common_utils.py b/litellm/litellm_core_utils/prompt_templates/common_utils.py index 6c45622649f..378295e1b7a 100644 --- a/litellm/litellm_core_utils/prompt_templates/common_utils.py +++ b/litellm/litellm_core_utils/prompt_templates/common_utils.py @@ -208,7 +208,7 @@ def _content_parts_contain_image(parts: Sequence[object]) -> bool: for _ in range(_IMAGE_SCAN_MAX_DEPTH): if any(isinstance(part, Mapping) and part.get("type") in _IMAGE_CONTENT_PART_TYPES for part in frontier): return True - frontier = tuple( # rebind-ok: depth-bounded frontier walk + frontier = tuple( nested for part in frontier if isinstance(part, Mapping) @@ -2020,7 +2020,7 @@ def _anthropic_content_lists(messages: Sequence[object]) -> Iterator[object]: def _strip_encrypted_reasoning_from_blocks(content: object) -> None: blocks: Final = cast(list[object], content) # cast-ok: narrowed by the caller's isinstance kept: Final = tuple(block for block in blocks if not is_encrypted_reasoning_block(block)) - blocks[:] = kept # rebind-ok: shared with fallback snapshot + blocks[:] = kept def _reasoning_replay_group_key(indexed_block: tuple[int, Mapping[str, object]]) -> str: diff --git a/litellm/litellm_core_utils/provider_affinity.py b/litellm/litellm_core_utils/provider_affinity.py index 31cd9a7ff69..33bf2ee7079 100644 --- a/litellm/litellm_core_utils/provider_affinity.py +++ b/litellm/litellm_core_utils/provider_affinity.py @@ -83,7 +83,7 @@ def get_stable_session_id(litellm_params: object | None) -> str | None: return None -def add_provider_affinity_header( # mutable-ok: downstream handlers add auth and signing headers +def add_provider_affinity_header( headers: Mapping[str, object], litellm_params: object | None ) -> dict[str, object]: # mutable-ok: downstream handlers add auth and signing headers header_name: Final = _get_provider_affinity_header_name(litellm_params) diff --git a/litellm/litellm_core_utils/streaming_chunk_builder_utils.py b/litellm/litellm_core_utils/streaming_chunk_builder_utils.py index aa4e0cf5495..d975c3551f3 100644 --- a/litellm/litellm_core_utils/streaming_chunk_builder_utils.py +++ b/litellm/litellm_core_utils/streaming_chunk_builder_utils.py @@ -475,9 +475,7 @@ class ChunkProcessor: def get_combined_tool_content( self, tool_call_chunks: Sequence["_ToolCallChunk"] - ) -> list[ - ChatCompletionMessageToolCall | ChatCompletionMessageCustomToolCall - ]: # mutable-ok: assigned verbatim to Message.tool_calls, a list field + ) -> list[ChatCompletionMessageToolCall | ChatCompletionMessageCustomToolCall]: tool_calls_list: list[ ChatCompletionMessageToolCall | ChatCompletionMessageCustomToolCall ] = [] # mutable-ok: see return type diff --git a/litellm/llms/anthropic/chat/guardrail_translation/handler.py b/litellm/llms/anthropic/chat/guardrail_translation/handler.py index 24ff63c9433..a78f633f5d7 100644 --- a/litellm/llms/anthropic/chat/guardrail_translation/handler.py +++ b/litellm/llms/anthropic/chat/guardrail_translation/handler.py @@ -199,9 +199,7 @@ def _write_back_system_block(system: object, block_idx: int, response: str) -> N return text_blocks: Final = tuple(block for block in system if isinstance(block, dict) and block.get("type") == "text") if block_idx < len(text_blocks): - text_blocks[block_idx]["text"] = ( - response # mutable-ok: guardrails rewrite the caller's request payload in place - ) + text_blocks[block_idx]["text"] = response def _write_back_message_text(message: _WritableMessage, target: MessageTextTarget, response: str) -> None: @@ -211,22 +209,16 @@ def _write_back_message_text(message: _WritableMessage, target: MessageTextTarge match target: case MessageContentTarget(): if isinstance(content, str): - message["content"] = response # mutable-ok: guardrails rewrite the caller's request payload in place + message["content"] = response case ContentBlockTextTarget(content_idx=content_idx): if isinstance(content, list): - content[content_idx]["text"] = ( - response # mutable-ok: guardrails rewrite the caller's request payload in place - ) + content[content_idx]["text"] = response case ToolResultStringTarget(content_idx=content_idx): if isinstance(content, list): - content[content_idx]["content"] = ( - response # mutable-ok: guardrails rewrite the caller's request payload in place - ) + content[content_idx]["content"] = response case ToolResultBlockTextTarget(content_idx=content_idx, block_idx=block_idx): if isinstance(content, list): - content[content_idx]["content"][block_idx]["text"] = ( - response # mutable-ok: guardrails rewrite the caller's request payload in place - ) + content[content_idx]["content"][block_idx]["text"] = response case _: assert_never(target) @@ -248,9 +240,9 @@ def _write_back_tool_use( block: Final = content[target.content_idx] if isinstance(content, list) else None if not isinstance(block, dict): return - block["input"] = rewritten_input # mutable-ok: guardrails rewrite the caller's request payload in place + block["input"] = rewritten_input if shape.name is not None and shape.name != block.get("name"): - block["name"] = shape.name # mutable-ok: guardrails rewrite the caller's request payload in place + block["name"] = shape.name @dataclass(frozen=True, slots=True) @@ -603,13 +595,9 @@ class AnthropicMessagesHandler(BaseTranslation): *(item for one_message in extracted for item in one_message.scanned), ) texts_to_check: Final = [item.text for item in scanned] # mutable-ok: GenericGuardrailAPIInputs takes list[str] - images_to_check: Final = [ - image for one_message in extracted for image in one_message.images - ] # mutable-ok: GenericGuardrailAPIInputs takes list[str] + images_to_check: Final = [image for one_message in extracted for image in one_message.images] scanned_tool_calls: Final = tuple(item for one_message in extracted for item in one_message.tool_calls) - tool_calls_to_check: Final = [ - item.tool_call for item in scanned_tool_calls - ] # mutable-ok: GenericGuardrailAPIInputs takes list[ChatCompletionToolCallChunk] + tool_calls_to_check: Final = [item.tool_call for item in scanned_tool_calls] pre_guardrail_tool_calls: Final = _tool_call_shapes(tool_calls_to_check) # Step 2: Apply guardrail to all texts and tool calls in batch @@ -697,9 +685,7 @@ class AnthropicMessagesHandler(BaseTranslation): return data - def _hoisted_top_level_system_message( - self, data: dict - ) -> AllMessageValues | None: # mutable-ok: API message payload + def _hoisted_top_level_system_message(self, data: dict) -> AllMessageValues | None: """Return the system message produced by translating the top-level prompt.""" system: Final = data.get("system") if not system: @@ -736,7 +722,7 @@ class AnthropicMessagesHandler(BaseTranslation): if isinstance(content, str): return ( {"role": "system", "content": content} if content else None # mutable-ok: API message payload - ) # mutable-ok: API message payload + ) if not isinstance(content, list): return None blocks: Final[list[dict[str, object]]] = [] # mutable-ok: API message payload @@ -749,14 +735,14 @@ class AnthropicMessagesHandler(BaseTranslation): anthropic_block: dict[str, object] = { # mutable-ok: API message payload "type": "text", "text": text, - } # mutable-ok: API message payload + } cache_control = block.get("cache_control") if cache_control: anthropic_block["cache_control"] = deepcopy(cache_control) blocks.append(anthropic_block) return ( {"role": "system", "content": blocks} if blocks else None # mutable-ok: API message payload - ) # mutable-ok: API message payload + ) @staticmethod def _fold_leading_systems_into_top_level( @@ -1098,9 +1084,7 @@ class AnthropicMessagesHandler(BaseTranslation): match item.target: case SystemStringTarget(): if isinstance(data.get("system"), str): - data["system"] = ( - guardrail_response # mutable-ok: guardrails rewrite the caller's request payload in place - ) + data["system"] = guardrail_response case SystemBlockTextTarget(block_idx=block_idx): _write_back_system_block(data.get("system"), block_idx, guardrail_response) case ( diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.py index c0e6006633e..bf2d588dd3a 100644 --- a/litellm/llms/anthropic/common_utils.py +++ b/litellm/llms/anthropic/common_utils.py @@ -1591,7 +1591,7 @@ def _flatten_web_search_results_in_message(message: object) -> object: return {**message, "content": [b for b in rewritten if b is not None]} # mutable-ok: JSON wire format -def flatten_unencrypted_web_search_results_in_anthropic_messages( # mutable-ok: as sibling sanitizers +def flatten_unencrypted_web_search_results_in_anthropic_messages( messages: list[Any], ) -> list[Any]: """ diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/response_cache.py b/litellm/llms/anthropic/experimental_pass_through/messages/response_cache.py index 86dfe8ff451..dc2d4408c20 100644 --- a/litellm/llms/anthropic/experimental_pass_through/messages/response_cache.py +++ b/litellm/llms/anthropic/experimental_pass_through/messages/response_cache.py @@ -88,9 +88,7 @@ class AnthropicMessagesStreamCacheWriter: try: events: Final = _split_sse_events(collected_stream.decode("utf-8")) - cached_payload: Final = { - CACHED_STREAM_EVENTS_KEY: events - } # mutable-ok: cache backends serialize plain dicts + cached_payload: Final = {CACHED_STREAM_EVENTS_KEY: events} await litellm.cache.async_add_cache( cached_payload, dynamic_cache_object=self.caching_handler.dual_cache, diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/streaming_iterator.py b/litellm/llms/anthropic/experimental_pass_through/messages/streaming_iterator.py index 98c5c6d6d4e..0bd46382fef 100644 --- a/litellm/llms/anthropic/experimental_pass_through/messages/streaming_iterator.py +++ b/litellm/llms/anthropic/experimental_pass_through/messages/streaming_iterator.py @@ -186,7 +186,7 @@ def _sse_event(event_type: str, payload: Mapping[str, object]) -> bytes: def _incomplete_stream_error_sse_event() -> bytes: - return _sse_event( # mutable-ok: one-shot JSON payload, never mutated after construction + return _sse_event( "error", {"type": "error", "error": {"type": "api_error", "message": INCOMPLETE_STREAM_ERROR_MESSAGE}}, ) diff --git a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py index 1fdb0318bab..e3d3425f8a6 100644 --- a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py +++ b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py @@ -148,13 +148,11 @@ class LiteLLMAnthropicToResponsesAPIAdapter: if isinstance(content, str): return ( [{"type": "input_text", "text": content}] if content else [] # mutable-ok: API message payload - ) # mutable-ok: API message payload + ) if not isinstance(content, list): return [] # mutable-ok: API message payload return [ # mutable-ok: API message payload - with_prompt_cache_breakpoint( - {"type": "input_text", "text": text}, block.get("prompt_cache_breakpoint") - ) # mutable-ok: API message payload + with_prompt_cache_breakpoint({"type": "input_text", "text": text}, block.get("prompt_cache_breakpoint")) for block in content if isinstance(block, dict) and block.get("type") == "text" and (text := block.get("text")) # pyright: ignore[reportUnnecessaryIsInstance] # untrusted client payload ] diff --git a/litellm/llms/azure/passthrough/transformation.py b/litellm/llms/azure/passthrough/transformation.py index c40cefecdd0..a648a24f5e3 100644 --- a/litellm/llms/azure/passthrough/transformation.py +++ b/litellm/llms/azure/passthrough/transformation.py @@ -59,9 +59,7 @@ def logged_responses_stream(all_chunks: Sequence[str], logging_obj: Logging) -> terminal_event: Final = OpenAIResponsesAPIConfig.parse_terminal_event_from_stream_chunks(all_chunks=all_chunks) if terminal_event is None: return None - logging_obj.call_type = ( - RESPONSES_RELAY_SHAPE.call_type.value - ) # rebind-ok: routes cost calculation to the relayed shape's pricing path + logging_obj.call_type = RESPONSES_RELAY_SHAPE.call_type.value return terminal_event diff --git a/litellm/llms/azure_ai/image_generation/flux_transformation.py b/litellm/llms/azure_ai/image_generation/flux_transformation.py index ac9ec24420b..b6a9caf147b 100644 --- a/litellm/llms/azure_ai/image_generation/flux_transformation.py +++ b/litellm/llms/azure_ai/image_generation/flux_transformation.py @@ -73,9 +73,7 @@ class AzureFoundryFluxImageGenerationConfig(GPTImageGenerationConfig): normalized_model: Final = model.lower().replace(".", "-").replace("_", "-") return "flux-2-flex" if "flux-2-flex" in normalized_model else "flux-2-pro" - def get_supported_openai_params( # mutable-ok: inherited config contract returns a list - self, model: str - ) -> list[OpenAIImageGenerationOptionalParams]: + def get_supported_openai_params(self, model: str) -> list[OpenAIImageGenerationOptionalParams]: if not self.is_flux2_model(model): return super().get_supported_openai_params(model) return [ # mutable-ok: BaseImageGenerationConfig requires a list diff --git a/litellm/llms/base_llm/passthrough/transformation.py b/litellm/llms/base_llm/passthrough/transformation.py index f2a12c3f22d..84cbd4204e3 100644 --- a/litellm/llms/base_llm/passthrough/transformation.py +++ b/litellm/llms/base_llm/passthrough/transformation.py @@ -95,9 +95,7 @@ def logged_relay_shape( parsed: Final = shape.parse(body) except ValidationError: return None - logging_obj.call_type = ( - shape.call_type.value - ) # rebind-ok: routes cost calculation to the relayed shape's pricing path + logging_obj.call_type = shape.call_type.value return parsed diff --git a/litellm/llms/bedrock/messages/mantle_transformation.py b/litellm/llms/bedrock/messages/mantle_transformation.py index 052eb90a833..66744275778 100644 --- a/litellm/llms/bedrock/messages/mantle_transformation.py +++ b/litellm/llms/bedrock/messages/mantle_transformation.py @@ -45,7 +45,7 @@ def _move_betas_into_header(request: Mapping[str, object], headers: dict[str, st if betas: headers["anthropic-beta"] = ",".join(betas) # rebind-ok: the handler signs and sends this same dict return - headers.pop("anthropic-beta", None) # rebind-ok: a caller header Mantle rejects in full must not reach it + headers.pop("anthropic-beta", None) class AmazonMantleMessagesConfig(AmazonAnthropicClaudeMessagesConfig): diff --git a/litellm/llms/bedrock/realtime/handler.py b/litellm/llms/bedrock/realtime/handler.py index d17590bdaaa..049313c3c96 100644 --- a/litellm/llms/bedrock/realtime/handler.py +++ b/litellm/llms/bedrock/realtime/handler.py @@ -489,9 +489,7 @@ class BedrockRealtime(BaseAWSLLM): parsed_client_message = _parse_client_message(message) is_session_update = _json_str(parsed_client_message.get("type")) == "session.update" if is_session_update: - client_ws.scope[BEDROCK_REALTIME_PENDING_SESSION_UPDATE_SCOPE_KEY] = ( - message # rebind-ok: scope outlives the attempt - ) + client_ws.scope[BEDROCK_REALTIME_PENDING_SESSION_UPDATE_SCOPE_KEY] = message transformed_messages = transformation_config.transform_realtime_request( message=message, diff --git a/litellm/llms/fal_ai/image_edit/flux_lora_depth_transformation.py b/litellm/llms/fal_ai/image_edit/flux_lora_depth_transformation.py index fa469d638d2..0b6205ff302 100644 --- a/litellm/llms/fal_ai/image_edit/flux_lora_depth_transformation.py +++ b/litellm/llms/fal_ai/image_edit/flux_lora_depth_transformation.py @@ -27,7 +27,7 @@ class FalAIFluxLoraDepthEditConfig(FalAIImageEditConfig): def get_supported_openai_params(self, model: str) -> list: # mutable-ok: base class contract returns a list return list(SUPPORTED_OPENAI_PARAMS) # mutable-ok: base class contract returns a list - def map_openai_params( # mutable-ok: base class contract returns a dict + def map_openai_params( self, image_edit_optional_params: ImageEditOptionalRequestParams, model: str, @@ -63,9 +63,7 @@ class FalAIFluxLoraDepthEditConfig(FalAIImageEditConfig): if len(images) > 1: raise ValueError(f"{FLUX_LORA_DEPTH_ENDPOINT} accepts exactly one control image") provider_params: Final[Mapping[str, object]] = MappingProxyType( - { - key: value for key, value in image_edit_optional_request_params.items() if key != "mask" - } # mutable-ok: frozen by MappingProxyType + {key: value for key, value in image_edit_optional_request_params.items() if key != "mask"} ) request_body: Final[dict[str, object]] = { # mutable-ok: base class contract returns a dict "prompt": prompt, diff --git a/litellm/llms/fal_ai/image_edit/transformation.py b/litellm/llms/fal_ai/image_edit/transformation.py index 6e6a872839a..839c15c4c28 100644 --- a/litellm/llms/fal_ai/image_edit/transformation.py +++ b/litellm/llms/fal_ai/image_edit/transformation.py @@ -84,7 +84,7 @@ class FalAIImageEditConfig(BaseImageEditConfig): def get_supported_openai_params(self, model: str) -> list: # mutable-ok: base class contract returns a list return list(SUPPORTED_OPENAI_PARAMS) # mutable-ok: base class contract returns a list - def map_openai_params( # mutable-ok: base class contract returns a dict + def map_openai_params( self, image_edit_optional_params: ImageEditOptionalRequestParams, model: str, @@ -146,9 +146,7 @@ class FalAIImageEditConfig(BaseImageEditConfig): MappingProxyType({"mask_url": to_data_url(mask)}) if mask is not None else MappingProxyType({}) ) provider_params: Final[Mapping[str, object]] = MappingProxyType( - { - key: value for key, value in image_edit_optional_request_params.items() if key != "mask" - } # mutable-ok: frozen by MappingProxyType + {key: value for key, value in image_edit_optional_request_params.items() if key != "mask"} ) request_body: Final[dict[str, object]] = { # mutable-ok: base class contract returns a dict "prompt": prompt, diff --git a/litellm/llms/fal_ai/image_generation/gpt_image_2_transformation.py b/litellm/llms/fal_ai/image_generation/gpt_image_2_transformation.py index ca301662cf8..0d008555f8b 100644 --- a/litellm/llms/fal_ai/image_generation/gpt_image_2_transformation.py +++ b/litellm/llms/fal_ai/image_generation/gpt_image_2_transformation.py @@ -101,12 +101,10 @@ class FalAIGPTImage2Config(FalAIBaseConfig): endpoint: Final[str] = model if model.startswith(self.MODEL_PREFIX) else f"{self.MODEL_PREFIX}{model}" return f"{base_url}/{endpoint}" - def get_supported_openai_params( # mutable-ok: base class contract returns a list - self, model: str - ) -> list[OpenAIImageGenerationOptionalParams]: + def get_supported_openai_params(self, model: str) -> list[OpenAIImageGenerationOptionalParams]: return list(SUPPORTED_OPENAI_PARAMS) # mutable-ok: base class contract returns a list - def map_openai_params( # mutable-ok: base class contract returns a dict + def map_openai_params( self, non_default_params: Mapping[str, object], optional_params: Mapping[str, object], @@ -138,7 +136,7 @@ class FalAIGPTImage2Config(FalAIBaseConfig): return map_gpt_image_quality(value, model) return value - def transform_image_generation_request( # mutable-ok: base class contract returns a dict + def transform_image_generation_request( self, model: str, prompt: str, diff --git a/litellm/llms/gigachat/authenticator.py b/litellm/llms/gigachat/authenticator.py index 73086ba395b..a85dcd9c70d 100644 --- a/litellm/llms/gigachat/authenticator.py +++ b/litellm/llms/gigachat/authenticator.py @@ -243,7 +243,7 @@ def _parse_token_response(response: httpx.Response) -> tuple[str, int]: ) # expires_at is in milliseconds - expires_at: int # rebind-ok: conditionally assigned from str or int + expires_at: int if isinstance(expires_at_raw, str): expires_at = int(expires_at_raw) # rebind-ok: conditionally assigned from str or int else: diff --git a/litellm/llms/gigachat/chat/streaming.py b/litellm/llms/gigachat/chat/streaming.py index 0a4cbd8e520..908412d9c31 100644 --- a/litellm/llms/gigachat/chat/streaming.py +++ b/litellm/llms/gigachat/chat/streaming.py @@ -30,7 +30,7 @@ class GigaChatModelResponseIterator: def chunk_parser(self, chunk: Mapping[str, object]) -> GenericStreamingChunk: """Parse a single streaming chunk from GigaChat.""" - choices: Sequence = chunk.get("choices") or () # mutable-ok: tuple literal as default + choices: Sequence = chunk.get("choices") or () if not choices: return GenericStreamingChunk( text="", @@ -56,7 +56,7 @@ class GigaChatModelResponseIterator: if chunk_finish_reason == "function_call" and isinstance(raw_function_call, Mapping) and raw_function_call: func_call: Final[Mapping[str, object]] = raw_function_call args_raw: Final[object] = func_call.get("arguments") or {} - args_str: str # rebind-ok: conditionally assigned from dict or str + args_str: str if isinstance(args_raw, dict): args_str = json.dumps(args_raw, ensure_ascii=False) # rebind-ok: build from dict else: @@ -80,10 +80,10 @@ class GigaChatModelResponseIterator: usage = convert_usage(validated_usage) _prompt_details: dict | None = ( usage.prompt_tokens_details.model_dump() if usage.prompt_tokens_details else None - ) # rebind-ok: conditional + ) _completion_details: dict | None = ( usage.completion_tokens_details.model_dump() if usage.completion_tokens_details else None - ) # rebind-ok: conditional + ) usage_block = ChatCompletionUsageBlock( # pyright: ignore[reportCallIssue] # TypedDict kwarg constructor prompt_tokens=usage.prompt_tokens, completion_tokens=usage.completion_tokens, diff --git a/litellm/llms/mistral/batches/transformation.py b/litellm/llms/mistral/batches/transformation.py index ef9ee5ff503..d3ed6a3af62 100644 --- a/litellm/llms/mistral/batches/transformation.py +++ b/litellm/llms/mistral/batches/transformation.py @@ -33,7 +33,7 @@ OpenAIBatchStatus: TypeAlias = Literal[ "validating", "failed", "in_progress", "finalizing", "completed", "expired", "cancelling", "cancelled" ] -_NO_HEADERS: Final[Mapping[str, str]] = MappingProxyType({}) # mutable-ok: frozen at module scope +_NO_HEADERS: Final[Mapping[str, str]] = MappingProxyType({}) _STATUS_MAP: Final[MappingProxyType[MistralBatchStatus, OpenAIBatchStatus]] = MappingProxyType( { "QUEUED": "validating", diff --git a/litellm/llms/mongodb/vector_stores/transformation.py b/litellm/llms/mongodb/vector_stores/transformation.py index a59f39d3be8..94d3aef48cc 100644 --- a/litellm/llms/mongodb/vector_stores/transformation.py +++ b/litellm/llms/mongodb/vector_stores/transformation.py @@ -197,7 +197,7 @@ class MongoDBVectorStoreConfig(BaseQueryEmbeddingVectorStoreConfig): **headers, "Authorization": f"Bearer {api_key}", "Content-Type": "application/json", - } # mutable-ok: writable HTTP headers + } def get_complete_url(self, api_base: str | None, litellm_params: Mapping[str, object]) -> str: if not api_base: diff --git a/litellm/llms/nvidia_nim/passthrough/transformation.py b/litellm/llms/nvidia_nim/passthrough/transformation.py index 7de1ce4d631..e8e7da8e10b 100644 --- a/litellm/llms/nvidia_nim/passthrough/transformation.py +++ b/litellm/llms/nvidia_nim/passthrough/transformation.py @@ -110,7 +110,7 @@ class NvidiaNimPassthroughConfig(BasePassthroughConfig): return { **headers, "Authorization": f"Bearer {api_key}", - } # mutable-ok: base class contract returns dict for httpx + } @staticmethod def get_api_base(api_base: str | None = None) -> str | None: diff --git a/litellm/llms/nvidia_nim/rerank/transformation.py b/litellm/llms/nvidia_nim/rerank/transformation.py index 93e00dad9a1..15cfdb6bece 100644 --- a/litellm/llms/nvidia_nim/rerank/transformation.py +++ b/litellm/llms/nvidia_nim/rerank/transformation.py @@ -215,7 +215,7 @@ class NvidiaNimRerankConfig(BaseRerankConfig): elif isinstance(doc, dict): # Preserve only the structured passage fields supported by the # selected rerank route. - supported_fields: NvidiaNimPassageObject = {} # mutable-ok: assembling a request TypedDict + supported_fields: NvidiaNimPassageObject = {} if "text" in self.SUPPORTED_PASSAGE_FIELDS and "text" in doc: supported_fields["text"] = doc["text"] if "image" in self.SUPPORTED_PASSAGE_FIELDS and "image" in doc: diff --git a/litellm/llms/openai/chat/gpt_transformation.py b/litellm/llms/openai/chat/gpt_transformation.py index b63684db782..62351d8e39a 100644 --- a/litellm/llms/openai/chat/gpt_transformation.py +++ b/litellm/llms/openai/chat/gpt_transformation.py @@ -596,9 +596,7 @@ class OpenAIGPTConfig(BaseLLMModelInfo, BaseConfig): for choice in choices: ## HANDLE JSON MODE - anthropic returns single function call] tool_calls = choice["message"].get("tool_calls", None) - new_tool_calls: list[ChatCompletionMessageToolCall | ChatCompletionMessageCustomToolCall] | None = ( - None # mutable-ok: holds _handle_invalid_parallel_tool_calls' list; Message.__init__ expects list - ) + new_tool_calls: list[ChatCompletionMessageToolCall | ChatCompletionMessageCustomToolCall] | None = None message_content = choice["message"].get("content", None) if tool_calls is not None: _openai_tool_calls = [] diff --git a/litellm/llms/openai/openai.py b/litellm/llms/openai/openai.py index 7ac0d988074..63874ca9619 100644 --- a/litellm/llms/openai/openai.py +++ b/litellm/llms/openai/openai.py @@ -1427,9 +1427,7 @@ class OpenAIChatCompletion(BaseLLM, BaseOpenAILLM): }, ) - request_data: Final = ( # mutable-ok: the OpenAI SDK takes the request body as a dict - {**data, "extra_headers": headers} if headers else data - ) + request_data: Final = {**data, "extra_headers": headers} if headers else data response = await openai_aclient.images.generate(**request_data, timeout=timeout) stringified_response: Final = response.model_dump() ## LOGGING @@ -1513,9 +1511,7 @@ class OpenAIChatCompletion(BaseLLM, BaseOpenAILLM): ) ## COMPLETION CALL - request_data: Final = ( # mutable-ok: the OpenAI SDK takes the request body as a dict - {**data, "extra_headers": headers} if headers else data - ) + request_data: Final = {**data, "extra_headers": headers} if headers else data _response: Final = openai_client.images.generate(**request_data, timeout=timeout) response: Final = _response.model_dump() diff --git a/litellm/llms/snowflake/chat/transformation.py b/litellm/llms/snowflake/chat/transformation.py index aeff902f655..734d0e20818 100644 --- a/litellm/llms/snowflake/chat/transformation.py +++ b/litellm/llms/snowflake/chat/transformation.py @@ -118,7 +118,7 @@ def _convert_image_url_to_anthropic(block: Mapping[str, object]) -> object: anthropic_process_openai_file_message({"type": "file", "file": {"file_data": url}}) if select_anthropic_content_block_type_for_file(_data_uri_media_type(url)) == "document" else create_anthropic_image_param( - image_url if isinstance(image_url, dict) else url, # mutable-ok: caller's JSON block + image_url if isinstance(image_url, dict) else url, format=_image_url_field(image_url, "format"), is_bedrock_invoke=True, ) @@ -191,12 +191,8 @@ def _signed_thinking_blocks(msg: object) -> list[dict[str, object]]: # mutable- ] -def _clean_input_schema(schema: object) -> object: # mutable-ok: JSON schema copy - return ( - {key: value for key, value in schema.items() if key != "$schema"} - if isinstance(schema, Mapping) - else schema # mutable-ok: JSON schema copy - ) # mutable-ok: JSON schema copy +def _clean_input_schema(schema: object) -> object: + return {key: value for key, value in schema.items() if key != "$schema"} if isinstance(schema, Mapping) else schema class SnowflakeConfig(SnowflakeBaseConfig, OpenAIGPTConfig): @@ -299,9 +295,7 @@ class SnowflakeConfig(SnowflakeBaseConfig, OpenAIGPTConfig): ) return anthropic_tools - def _extract_system_and_messages( # mutable-ok: JSON wire messages - self, messages: list[AllMessageValues] - ) -> tuple[list[dict] | None, list[dict]]: + def _extract_system_and_messages(self, messages: list[AllMessageValues]) -> tuple[list[dict] | None, list[dict]]: """ Split messages into system prompt and conversation turns for Anthropic format. @@ -330,9 +324,7 @@ class SnowflakeConfig(SnowflakeBaseConfig, OpenAIGPTConfig): { # mutable-ok: JSON wire system block "type": "text", "text": block.get("text", ""), - **( - {"cache_control": block["cache_control"]} if "cache_control" in block else {} - ), # mutable-ok: JSON wire block + **({"cache_control": block["cache_control"]} if "cache_control" in block else {}), } for block in content if isinstance(block, Mapping) and block.get("type") == "text" @@ -372,7 +364,7 @@ class SnowflakeConfig(SnowflakeBaseConfig, OpenAIGPTConfig): ] if isinstance(content, list) else [*thinking_blocks, *([{"type": "text", "text": content}] if content else [])] - ) # rebind-ok: loop-local normalized content + ) conversation.append({"role": "assistant", "content": thinking_content}) else: conversation.append({"role": "assistant", "content": content}) @@ -380,9 +372,7 @@ class SnowflakeConfig(SnowflakeBaseConfig, OpenAIGPTConfig): tool_call_id_value = ( msg.get("tool_call_id", "") if isinstance(msg, dict) else getattr(msg, "tool_call_id", "") ) - tool_call_id = ( - tool_call_id_value if isinstance(tool_call_id_value, str) else "" - ) # rebind-ok: normalized loop value + tool_call_id = tool_call_id_value if isinstance(tool_call_id_value, str) else "" tool_result_block = _convert_tool_result_to_anthropic(content, tool_call_id, msg_cache_control) if ( conversation @@ -395,13 +385,13 @@ class SnowflakeConfig(SnowflakeBaseConfig, OpenAIGPTConfig): else: conversation.append( {"role": "user", "content": [tool_result_block]} # mutable-ok: JSON wire message - ) # mutable-ok: JSON wire message + ) else: - conversation.append( # mutable-ok: JSON wire message + conversation.append( { # mutable-ok: JSON wire message "role": role, "content": _convert_image_url_blocks_to_anthropic(content), - } # mutable-ok: JSON wire message + } ) system: Final[list[dict] | None] = system_parts if system_parts else None # mutable-ok: JSON wire messages @@ -516,11 +506,11 @@ class SnowflakeConfig(SnowflakeBaseConfig, OpenAIGPTConfig): "messages": conversation, "stream": stream, **optional_params, - **extra_body, # mutable-ok: JSON wire body + **extra_body, } ) if system is not None: - body["system"] = normalize_cache_control_in_anthropic_payload( # mutable-ok: JSON wire payload + body["system"] = normalize_cache_control_in_anthropic_payload( {"system": system} # mutable-ok: JSON wire payload )["system"] diff --git a/litellm/llms/vertex_ai/text_to_speech/transformation.py b/litellm/llms/vertex_ai/text_to_speech/transformation.py index d382f43495f..6c2c59d98e1 100644 --- a/litellm/llms/vertex_ai/text_to_speech/transformation.py +++ b/litellm/llms/vertex_ai/text_to_speech/transformation.py @@ -43,9 +43,7 @@ else: LiteLLMLoggingObj = Any HttpxBinaryResponseContent = Any -_LyriaVoice: TypeAlias = ( - str | dict | None -) # mutable-ok: inherited interface supports structured provider voice dictionaries +_LyriaVoice: TypeAlias = str | dict | None class VertexAITextToSpeechConfig(BaseTextToSpeechConfig, VertexBase): @@ -664,21 +662,15 @@ class VertexAILyriaTextToSpeechConfig(VertexAITextToSpeechConfig): if model_info["vertex_ai_audio_api"] == "lyria_predict": predictions: Final = response_json.get("predictions") or () if predictions: - audio_data = predictions[0].get("audioContent") or predictions[0].get( - "bytesBase64Encoded" - ) # rebind-ok: predict response supplies the generated audio value + audio_data = predictions[0].get("audioContent") or predictions[0].get("bytesBase64Encoded") mime_type = predictions[0].get("mimeType") # rebind-ok: predict response supplies its audio MIME type else: for step in response_json.get("steps") or response_json.get("outputs") or (): content_items = step.get("content") or () if step.get("type") == "model_output" else (step,) for content in content_items: if content.get("type") == "audio" and content.get("data"): - audio_data = content[ - "data" - ] # rebind-ok: interactions response supplies the generated audio value - mime_type = content.get( - "mime_type" - ) # rebind-ok: interactions response supplies its audio MIME type + audio_data = content["data"] + mime_type = content.get("mime_type") if audio_data is None: raise ValueError(f"No generated audio found in Vertex AI {base_model} response") binary_data: Final = base64.b64decode(audio_data) diff --git a/litellm/llms/xai/audio_transcription/transformation.py b/litellm/llms/xai/audio_transcription/transformation.py index feeabed0d9c..49447413a37 100644 --- a/litellm/llms/xai/audio_transcription/transformation.py +++ b/litellm/llms/xai/audio_transcription/transformation.py @@ -168,9 +168,7 @@ class XAIAudioTranscriptionConfig(BaseAudioTranscriptionConfig): for word in payload.words ] - hidden_params: Final[dict[str, object]] = dict( - payload.model_dump(mode="json") - ) # mutable-ok: TranscriptionResponse._hidden_params is a dict + hidden_params: Final[dict[str, object]] = dict(payload.model_dump(mode="json")) if payload.duration is not None: hidden_params["audio_transcription_duration"] = payload.duration response._hidden_params = hidden_params # pyright: ignore[reportPrivateUsage] # TranscriptionResponse exposes no public hidden-params setter diff --git a/litellm/ocr/main.py b/litellm/ocr/main.py index 06830ed4b53..3ca6c1295c5 100644 --- a/litellm/ocr/main.py +++ b/litellm/ocr/main.py @@ -173,9 +173,7 @@ def _prepare_ocr_request( custom_llm_provider=custom_llm_provider, extra_headers=extra_headers, provider_config=ocr_provider_config, - optional_params=cast( - dict[str, object], optional_params - ), # cast-ok: provider configs return heterogeneous OCR options + optional_params=cast(dict[str, object], optional_params), litellm_params=dict(litellm_params), effective_timeout=effective_timeout, litellm_logging_obj=litellm_logging_obj, diff --git a/litellm/passthrough/main.py b/litellm/passthrough/main.py index 73d8bab686b..ef931827d85 100644 --- a/litellm/passthrough/main.py +++ b/litellm/passthrough/main.py @@ -428,9 +428,7 @@ def llm_passthrough_route( _is_async: Final = bool(kwargs.get("allm_passthrough_route", False)) - litellm_logging_obj: Final = cast( - LiteLLMLoggingObj, kwargs.get("litellm_logging_obj") - ) # cast-ok: logging obj is constructed upstream; tests inject mocks + litellm_logging_obj: Final = cast(LiteLLMLoggingObj, kwargs.get("litellm_logging_obj")) model, custom_llm_provider, api_key, api_base = get_llm_provider( model=model, @@ -516,9 +514,7 @@ def llm_passthrough_route( forward_headers=False, ) - _request_data: dict | None = ( - data if isinstance(data, dict) else (json if isinstance(json, dict) else None) - ) # rebind-ok: conditional + _request_data: dict | None = data if isinstance(data, dict) else (json if isinstance(json, dict) else None) headers, signed_json_body = provider_config.sign_request( headers=headers, litellm_params=litellm_params_dict, @@ -544,9 +540,7 @@ def llm_passthrough_route( ) ## IS STREAMING REQUEST - _streaming_request_data: dict = ( - data if isinstance(data, dict) else (json if isinstance(json, dict) else {}) - ) # rebind-ok: conditional + _streaming_request_data: dict = data if isinstance(data, dict) else (json if isinstance(json, dict) else {}) is_streaming_request: Final = provider_config.is_streaming_request( endpoint=endpoint, request_data=_streaming_request_data, diff --git a/litellm/proxy/_experimental/mcp_server/contracts.py b/litellm/proxy/_experimental/mcp_server/contracts.py index c3129d171ad..1879e285789 100644 --- a/litellm/proxy/_experimental/mcp_server/contracts.py +++ b/litellm/proxy/_experimental/mcp_server/contracts.py @@ -57,24 +57,20 @@ class OperationContext: ) -> tuple[ UserAPIKeyAuth | None, str | None, - list[str] | None, # mutable-ok: detached legacy server-list payload - dict[str, dict[str, str]] | None, # mutable-ok: legacy auth dispatch requires concrete dict headers - dict[str, str] | None, # mutable-ok: detached legacy header payload - dict[str, str] | None, # mutable-ok: detached legacy header payload + list[str] | None, + dict[str, dict[str, str]] | None, + dict[str, str] | None, + dict[str, str] | None, str | None, ]: return ( self.user_api_key_auth, self.mcp_auth_header, list(self.mcp_servers) if self.mcp_servers is not None else None, # mutable-ok: legacy policy list input - { - key: dict(value) for key, value in self.mcp_server_auth_headers.items() - } # mutable-ok: legacy auth dispatch checks concrete dict headers + {key: dict(value) for key, value in self.mcp_server_auth_headers.items()} if self.mcp_server_auth_headers is not None else None, - dict(self.oauth2_headers) - if self.oauth2_headers is not None - else None, # mutable-ok: legacy OAuth header input + dict(self.oauth2_headers) if self.oauth2_headers is not None else None, dict(self.raw_headers) if self.raw_headers is not None else None, # mutable-ok: legacy request header input self.client_ip, ) diff --git a/litellm/proxy/_experimental/mcp_server/db.py b/litellm/proxy/_experimental/mcp_server/db.py index 30ee8b7a4fc..18723a76b2b 100644 --- a/litellm/proxy/_experimental/mcp_server/db.py +++ b/litellm/proxy/_experimental/mcp_server/db.py @@ -636,8 +636,6 @@ async def get_all_mcp_servers( where: Final[prisma_db_types.LiteLLM_MCPServerTableWhereInput] = ( {"approval_status": approval_status} if approval_status is not None - # mutable-ok: prisma where-inputs must be plain dicts, and both `NOT` and `not` drop - # NULL rows (measured), so the OR is the only NULL-preserving way to exclude drafts else {"OR": [{"approval_status": None}, {"approval_status": {"not": MCPApprovalStatus.draft}}]} ) mcp_servers: Final = await _db_find_mcp_server_rows(prisma_client, where) diff --git a/litellm/proxy/_experimental/mcp_server/legacy_callbacks.py b/litellm/proxy/_experimental/mcp_server/legacy_callbacks.py index 9e321062643..f52d2a006d2 100644 --- a/litellm/proxy/_experimental/mcp_server/legacy_callbacks.py +++ b/litellm/proxy/_experimental/mcp_server/legacy_callbacks.py @@ -55,9 +55,7 @@ def create_sampling_callback( params=params, default_model=getattr(litellm, "default_mcp_sampling_model", None), user_api_key_auth=captured.user_api_key_auth, - raw_headers=dict(captured.raw_headers) - if captured.raw_headers is not None - else None, # mutable-ok: handler consumes an owned request header dict + raw_headers=dict(captured.raw_headers) if captured.raw_headers is not None else None, client_ip=captured.client_ip, ) diff --git a/litellm/proxy/_experimental/mcp_server/mcp_debug.py b/litellm/proxy/_experimental/mcp_server/mcp_debug.py index ff482b80b50..70da73fa045 100644 --- a/litellm/proxy/_experimental/mcp_server/mcp_debug.py +++ b/litellm/proxy/_experimental/mcp_server/mcp_debug.py @@ -174,9 +174,7 @@ class MCPAuthDiagnostics: { "x-mcp-debug-auth-resolution": AuthResolution.multiple.value, "x-mcp-debug-auth-resolutions": json.dumps( - { - server_id: source.value for server_id, source in self._outcomes[:32] - }, # mutable-ok: JSON encoder requires a concrete dict + {server_id: source.value for server_id, source in self._outcomes[:32]}, separators=(",", ":"), ensure_ascii=True, ), @@ -597,9 +595,7 @@ async def capture_upstream_error_response(response: httpx.Response | httpx2.Resp ) except (asyncio.TimeoutError, httpx.HTTPError, httpx.StreamError, httpx2.HTTPError, httpx2.StreamError): response._content = b"" # pyright: ignore[reportPrivateUsage] # rebind-ok: httpx auth retries must survive diagnostic read failures - response.extensions[_CAPTURE_EXTENSION] = ( - "(unavailable: error body read failed)" # rebind-ok: httpx response hooks communicate through extensions - ) + response.extensions[_CAPTURE_EXTENSION] = "(unavailable: error body read failed)" return response.extensions[_CAPTURE_EXTENSION] = preview # rebind-ok: httpx response hooks communicate through extensions diff --git a/litellm/proxy/_experimental/mcp_server/operations.py b/litellm/proxy/_experimental/mcp_server/operations.py index 26bf68d9932..dcab43bdc76 100644 --- a/litellm/proxy/_experimental/mcp_server/operations.py +++ b/litellm/proxy/_experimental/mcp_server/operations.py @@ -3103,9 +3103,7 @@ class GatewayOperations: return await _execute_mcp_tool( name=operation.name, arguments=dict(operation.arguments), # mutable-ok: existing tool hooks own mutable argument data - allowed_mcp_servers=list( - operation.allowed_mcp_servers - ), # mutable-ok: legacy dispatch list contract + allowed_mcp_servers=list(operation.allowed_mcp_servers), start_time=operation.start_time, user_api_key_auth=auth, mcp_auth_header=token, diff --git a/litellm/proxy/_experimental/mcp_server/tool_search.py b/litellm/proxy/_experimental/mcp_server/tool_search.py index 3650c722103..3c060752934 100644 --- a/litellm/proxy/_experimental/mcp_server/tool_search.py +++ b/litellm/proxy/_experimental/mcp_server/tool_search.py @@ -103,7 +103,7 @@ def _tool_result(tool: Tool) -> ToolSearchResult: "name": tool.name, "description": tool.description or "", "inputSchema": tool.input_schema, - } # mutable-ok: wire schema payload + } def _scored_result(tool: Tool, score: float) -> ToolSearchResult: @@ -112,7 +112,7 @@ def _scored_result(tool: Tool, score: float) -> ToolSearchResult: "description": tool.description or "", "inputSchema": tool.input_schema, "score": score, - } # mutable-ok: wire schema payload + } _MCP_PROXY_IDENTITY_META_KEY: Final[str] = "litellm.ai/proxy_tool_identity" @@ -120,7 +120,7 @@ _MCP_PROXY_IDENTITY_META_KEY: Final[str] = "litellm.ai/proxy_tool_identity" def with_mcp_proxy_identity(tool: Tool, server_id: str) -> Tool: identity: Final[MCPProxyToolIdentity] = {"server_id": server_id, "tool_name": tool.name} - return tool.model_copy( # mutable-ok: Pydantic requires mutable update and metadata mappings + return tool.model_copy( update={ # mutable-ok: Pydantic update payload "meta": {**(tool.meta or {}), _MCP_PROXY_IDENTITY_META_KEY: identity} # mutable-ok: metadata mapping } diff --git a/litellm/proxy/agent_endpoints/agent_registry.py b/litellm/proxy/agent_endpoints/agent_registry.py index d6b12e830e1..3d56c2b5326 100644 --- a/litellm/proxy/agent_endpoints/agent_registry.py +++ b/litellm/proxy/agent_endpoints/agent_registry.py @@ -135,9 +135,7 @@ def _dump_agent_params(raw: Mapping[str, object]) -> dict[str, object]: _AGENT_PARAMS_MASKER: Final = SensitiveDataMasker() _REDACT_AGENT_PARAMS_MAX_DEPTH: Final = 10 -_AGENT_PARAMS_ADAPTER: Final[TypeAdapter[dict[str, object]]] = TypeAdapter( - dict[str, object] -) # mutable-ok: safe_dumps() and AgentResponse.litellm_params both require a real dict, not a Mapping +_AGENT_PARAMS_ADAPTER: Final[TypeAdapter[dict[str, object]]] = TypeAdapter(dict[str, object]) _AGENT_PARAMS_SEQUENCE_ADAPTER: Final[TypeAdapter[tuple[object, ...]]] = TypeAdapter(tuple[object, ...]) _EMPTY_LITELLM_PARAMS: Final[Mapping[str, object]] = MappingProxyType({}) @@ -189,7 +187,7 @@ def _redact_agent_params_tree(value: object, _depth: int) -> object: else _redact_agent_params_tree(nested_value, _depth + 1) ) for key, nested_value in typed_params.items() - } # mutable-ok: consumed by json.dumps()/AgentResponse.litellm_params, both of which require a real dict + } def parse_agent_litellm_params(value: object) -> Mapping[str, object]: @@ -318,7 +316,7 @@ def _restore_redacted_litellm_params( key: value for key in all_keys if (value := _resolved_agent_param_value(key, incoming, existing, _depth)) is not _MISSING_AGENT_PARAM - } # mutable-ok: fed to safe_dumps() for JSON-column storage, which requires a real dict + } class GrantMigrationResult(NamedTuple): diff --git a/litellm/proxy/auth/auth_utils.py b/litellm/proxy/auth/auth_utils.py index c002b2b9508..c0123ae45a3 100644 --- a/litellm/proxy/auth/auth_utils.py +++ b/litellm/proxy/auth/auth_utils.py @@ -1410,7 +1410,7 @@ def log_once_if_budget_reservation_disabled( "Set disable_budget_reservation to False or remove it to restore " "hard per-request budget enforcement." ) - constants.budget_reservation_disabled_info_emitted = True # rebind-ok: process-wide one-shot sentinel + constants.budget_reservation_disabled_info_emitted = True def is_pass_through_provider_route(route: str) -> bool: diff --git a/litellm/proxy/client/cli/commands/agents.py b/litellm/proxy/client/cli/commands/agents.py index 15b111ff016..7d50131cb88 100644 --- a/litellm/proxy/client/cli/commands/agents.py +++ b/litellm/proxy/client/cli/commands/agents.py @@ -241,9 +241,7 @@ def prepare_codex( _Preparer: TypeAlias = Callable[[str, str, Mapping[str, str]], Sequence[str]] -_PREPARERS: Final[Mapping[str, _Preparer]] = MappingProxyType( - {"pi": prepare_pi, "codex": prepare_codex} # mutable-ok: MappingProxyType freezes the provider registry -) +_PREPARERS: Final[Mapping[str, _Preparer]] = MappingProxyType({"pi": prepare_pi, "codex": prepare_codex}) def agent_launch_args(command: str, base_url: str) -> list[str]: diff --git a/litellm/proxy/client/cli/commands/claude_settings.py b/litellm/proxy/client/cli/commands/claude_settings.py index b3fdc4695cb..13ed483586e 100644 --- a/litellm/proxy/client/cli/commands/claude_settings.py +++ b/litellm/proxy/client/cli/commands/claude_settings.py @@ -621,7 +621,7 @@ def unconfigure_claude_settings( ) target: Final = _write_target(settings_path) file_removed: Final = not settings and not (receipt.file_existed and target.exists()) - kept_receipt: Final = ( # mutable-ok: pydantic serializes the update as given and rejects a mappingproxy + kept_receipt: Final = ( receipt.model_copy(update={"written": {item.key: _fingerprint(absent) for item in withheld}}) if withheld else None diff --git a/litellm/proxy/client/cli/commands/codex_settings.py b/litellm/proxy/client/cli/commands/codex_settings.py index 686eaa47ff0..5b01c31683a 100644 --- a/litellm/proxy/client/cli/commands/codex_settings.py +++ b/litellm/proxy/client/cli/commands/codex_settings.py @@ -106,7 +106,6 @@ def _with(document: TOMLDocument, path: str, snapshot: str | None) -> TOMLDocume if section and section not in document and snapshot is not None: contents: Final = tomlkit.parse(tomlkit.dumps(MappingProxyType({key: tomlkit.parse(snapshot).item("value")}))) return tomlkit.parse(document.as_string() + "\n" + tomlkit.dumps(MappingProxyType({section: contents}))) - # mutable-ok: TOMLKit editing requires private node mutation to preserve comments and order updated: Final = tomlkit.parse(document.as_string()) parent: Final = _table(_mapping(updated).get(section)) if section else updated if parent is None: diff --git a/litellm/proxy/client/cli/commands/pi.py b/litellm/proxy/client/cli/commands/pi.py index 5c749959638..f5834f94fb8 100644 --- a/litellm/proxy/client/cli/commands/pi.py +++ b/litellm/proxy/client/cli/commands/pi.py @@ -175,7 +175,7 @@ def _model_entry( ) output: Final[dict[str, JsonValue]] = ( # mutable-ok: JSON field {"maxTokens": limit.max_tokens} if limit and limit.max_tokens else {} - ) # mutable-ok: JSON field + ) return {"id": model_id, **context, **output} # mutable-ok: JSON serialization requires a mutable object @@ -208,9 +208,7 @@ def sync_models_json( ) -> PiSyncError | None: """Replace only the litellm provider entry, leaving the rest of the file intact.""" try: - current: Final = ( # mutable-ok: JSON object default - _MODELS_FILE_ADAPTER.validate_json(path.read_text()) if path.exists() else {} - ) + current: Final = _MODELS_FILE_ADAPTER.validate_json(path.read_text()) if path.exists() else {} except (OSError, ValidationError) as e: return PiSyncError(f"Could not read {path} as a JSON object: {e}. Fix or move the file, then retry.") existing_providers: Final = current.get("providers", {}) # mutable-ok: JSON object default diff --git a/litellm/proxy/common_utils/auth_cache_invalidation_pubsub.py b/litellm/proxy/common_utils/auth_cache_invalidation_pubsub.py index 2bb53c7723d..11cb66d1a7f 100644 --- a/litellm/proxy/common_utils/auth_cache_invalidation_pubsub.py +++ b/litellm/proxy/common_utils/auth_cache_invalidation_pubsub.py @@ -184,17 +184,17 @@ class AuthCacheInvalidationSubscriber: backoff_seconds = _BACKOFF_INITIAL_SECONDS # rebind-ok: exponential backoff accumulator across reconnects while True: try: - client = _pubsub_capable_client(self._redis_cache) # rebind-ok: re-resolved on every reconnect + client = _pubsub_capable_client(self._redis_cache) if client is None: verbose_proxy_logger.warning( "auth cache invalidation subscriber disabled: cluster redis client has no pub/sub support; " "cross-worker eviction falls back to the local cache TTL" ) return - pubsub = client.pubsub() # rebind-ok: fresh pubsub per reconnect + pubsub = client.pubsub() try: await pubsub.subscribe(auth_cache_invalidation_channel(self._redis_cache)) - backoff_seconds = _BACKOFF_INITIAL_SECONDS # rebind-ok: reset after successful subscribe + backoff_seconds = _BACKOFF_INITIAL_SECONDS await self._consume(pubsub) finally: await self._close_pubsub(pubsub) @@ -207,7 +207,7 @@ class AuthCacheInvalidationSubscriber: backoff_seconds, ) await asyncio.sleep(backoff_seconds) - backoff_seconds = min(backoff_seconds * 2, _BACKOFF_MAX_SECONDS) # rebind-ok: backoff accumulator + backoff_seconds = min(backoff_seconds * 2, _BACKOFF_MAX_SECONDS) async def _consume(self, pubsub: _ConfigSyncPubSub) -> None: while True: diff --git a/litellm/proxy/common_utils/reset_budget_job.py b/litellm/proxy/common_utils/reset_budget_job.py index 3efc189a475..b35b876b475 100644 --- a/litellm/proxy/common_utils/reset_budget_job.py +++ b/litellm/proxy/common_utils/reset_budget_job.py @@ -240,12 +240,8 @@ def _queue_budget_linked_resets( one transaction, so the reverse order lets the zero re-match a row the decrement just moved into the (0, cap] range and erase its carried spend.""" for budget_id, cap in cascade.rollover_caps.items(): - writes.queue_spend_zero( - where={"budget_id": budget_id, **extra, "spend": {"gt": 0, "lte": cap}} - ) # mutable-ok: prisma where filter must be a dict - writes.queue_spend_decrement( - where={"budget_id": budget_id, **extra, "spend": {"gt": cap}}, amount=cap - ) # mutable-ok: prisma where filter must be a dict + writes.queue_spend_zero(where={"budget_id": budget_id, **extra, "spend": {"gt": 0, "lte": cap}}) + writes.queue_spend_decrement(where={"budget_id": budget_id, **extra, "spend": {"gt": cap}}, amount=cap) plain_ids: Final = tuple(bid for bid in cascade.budget_ids if bid not in cascade.rollover_caps) if plain_ids: writes.queue_spend_zero(where=_budget_link_where(plain_ids, extra)) @@ -267,16 +263,10 @@ def _queue_enduser_resets(writes: LinkedSpendResetWrites, cascade: "_BudgetCasca return cap: Final = cascade.rollover_caps.get(default_budget_id) if cap is None: - writes.queue_spend_zero( - where={"budget_id": None, **_SPENT_ROWS_WHERE} - ) # mutable-ok: prisma where filter must be a dict + writes.queue_spend_zero(where={"budget_id": None, **_SPENT_ROWS_WHERE}) return - writes.queue_spend_zero( - where={"budget_id": None, "spend": {"gt": 0, "lte": cap}} - ) # mutable-ok: prisma where filter must be a dict - writes.queue_spend_decrement( - where={"budget_id": None, "spend": {"gt": cap}}, amount=cap - ) # mutable-ok: prisma where filter must be a dict + writes.queue_spend_zero(where={"budget_id": None, "spend": {"gt": 0, "lte": cap}}) + writes.queue_spend_decrement(where={"budget_id": None, "spend": {"gt": cap}}, amount=cap) @dataclass(frozen=True, slots=True) diff --git a/litellm/proxy/common_utils/sse_keepalive.py b/litellm/proxy/common_utils/sse_keepalive.py index 26fccf8ee82..cf98a7e9224 100644 --- a/litellm/proxy/common_utils/sse_keepalive.py +++ b/litellm/proxy/common_utils/sse_keepalive.py @@ -65,9 +65,7 @@ async def _keepalive_ping_stream( ping_interval_seconds: float, ping_chunk: str, ) -> AsyncGenerator[str, None]: - pending = asyncio.ensure_future( - stream.__anext__() - ) # rebind-ok: re-armed with the next __anext__ after each delivered chunk + pending = asyncio.ensure_future(stream.__anext__()) try: while True: await asyncio.wait({pending}, timeout=ping_interval_seconds) @@ -125,9 +123,7 @@ async def _keepalive_ping_byte_stream( stream: AsyncGenerator[bytes, None], ping_interval_seconds: float, ) -> AsyncGenerator[bytes, None]: - pending = asyncio.ensure_future( - stream.__anext__() - ) # rebind-ok: re-armed with the next __anext__ after each delivered chunk + pending = asyncio.ensure_future(stream.__anext__()) # The tail of the bytes relayed so far, long enough to hold any delimiter. # Seeded as a delimiter because a stream starts at a frame boundary, and kept # across chunks because a delimiter can be split between two transport reads, diff --git a/litellm/proxy/db/baseline_accounting.py b/litellm/proxy/db/baseline_accounting.py index 4219102d9aa..78036237993 100644 --- a/litellm/proxy/db/baseline_accounting.py +++ b/litellm/proxy/db/baseline_accounting.py @@ -491,7 +491,7 @@ class BaselineAccountingStore: tuple(await db.query_raw(_READ_PAGE, scope, after_revision, cursor, _PAGE_TIMESTAMPS, withdraw_from)) ): yield page - cursor = page[-1].started_at # rebind-ok: keyset pagination advances after each complete timestamp group + cursor = page[-1].started_at async def _withdraw(self, db: SupportsRawQueries, scope: str, started_at: float) -> None: async for page in self._pages(db, scope, 0, withdraw_from=started_at): @@ -623,9 +623,7 @@ async def flush_baseline_accounting(client: PrismaClient) -> None: store: Final = BaselineAccountingStore.for_client(client) async with client.baseline_accounting_lock: batch: Final = tuple(client.baseline_accounting_transactions[:32]) - client.baseline_accounting_transactions = client.baseline_accounting_transactions[ - 32: - ] # rebind-ok: drain under lock + client.baseline_accounting_transactions = client.baseline_accounting_transactions[32:] more_queued: Final = bool(client.baseline_accounting_transactions) try: remaining: Final = await asyncio.wait_for(_flush_records(store, batch), timeout=5) diff --git a/litellm/proxy/db/shadow_eval_funnel.py b/litellm/proxy/db/shadow_eval_funnel.py index 9181d3f5035..3578c3def7e 100644 --- a/litellm/proxy/db/shadow_eval_funnel.py +++ b/litellm/proxy/db/shadow_eval_funnel.py @@ -40,7 +40,7 @@ def pending_shadow_eval_funnel_events() -> int: def record_shadow_eval_funnel_event(job_id: str, stage: ShadowEvalFunnelStage) -> None: """Count one skipped request for one job leg; synchronous so the hook's read-modify- write cannot interleave with the flush's snapshot on the shared event loop.""" - counters: Final = _pending.setdefault(job_id, dict.fromkeys(FUNNEL_STAGES, 0)) # mutable-ok: queue entry + counters: Final = _pending.setdefault(job_id, dict.fromkeys(FUNNEL_STAGES, 0)) counters[stage] += 1 diff --git a/litellm/proxy/guardrails/guardrail_hooks/alice/alice.py b/litellm/proxy/guardrails/guardrail_hooks/alice/alice.py index bcc35e7a22f..287031c3528 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/alice/alice.py +++ b/litellm/proxy/guardrails/guardrail_hooks/alice/alice.py @@ -286,7 +286,7 @@ class AliceGuardrail(CustomGuardrail): text = replacement.get("text") if not (isinstance(index, int) and isinstance(text, str) and 0 <= index < len(texts)): raise self._mask_rejected(verdict) - texts[index] = text # mutable-ok: item assignment into the local working copy above + texts[index] = text inputs["texts"] = texts diff --git a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py index 434c52ca6f3..228b31604a3 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py +++ b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py @@ -1218,7 +1218,7 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): bedrock_request_data: Final = { # mutable-ok: outbound JSON request body **base_request_data, "content": content, - } # mutable-ok: outbound JSON request body + } prepared_request: Final = await run_aws_signing( self._prepare_request, credentials=credentials, @@ -1266,9 +1266,7 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): ) response_usage: Final = bedrock_guardrail_response.get("usage") if isinstance(response_usage, dict): - completed_chunk_usages.append( - response_usage - ) # rebind-ok: accumulator threaded from make_bedrock_api_request, recording this billed call + completed_chunk_usages.append(response_usage) return bedrock_guardrail_response status_code, detail_message = self._parse_bedrock_guardrail_error_response(httpx_response) @@ -2860,9 +2858,9 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): return except ModifyResponseException as e: if raw_sse: - e.model = _pre_block_response.model or e.model # rebind-ok: exc.model defaults to the guardrail + e.model = _pre_block_response.model or e.model if e.original_response is None: - e.original_response = _pre_block_response # rebind-ok: the block builder reads usage off this + e.original_response = _pre_block_response for block_chunk in AnthropicMessagesHandler().build_block_sse_chunks(e, stream_started=False): yield block_chunk return diff --git a/litellm/proxy/guardrails/guardrail_hooks/presidio.py b/litellm/proxy/guardrails/guardrail_hooks/presidio.py index fe91d6d7a28..eb07c19a580 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/presidio.py +++ b/litellm/proxy/guardrails/guardrail_hooks/presidio.py @@ -168,7 +168,7 @@ class _OPTIONAL_PresidioPIIMasking(CustomGuardrail): # Per-loop semaphores bounding chunked-analyze fan-out across ALL # concurrent oversized blocks/requests on this instance, not per call - self._loop_chunk_semaphores: _LoopSemaphores = {} # mutable-ok: per-loop semaphore cache + self._loop_chunk_semaphores: _LoopSemaphores = {} if mock_testing is True: # for testing purposes only return diff --git a/litellm/proxy/hooks/autorouter_baseline_cache.py b/litellm/proxy/hooks/autorouter_baseline_cache.py index 8cea7d0e364..0c006730dba 100644 --- a/litellm/proxy/hooks/autorouter_baseline_cache.py +++ b/litellm/proxy/hooks/autorouter_baseline_cache.py @@ -230,12 +230,10 @@ class AutoRouterBaselineCache(CustomLogger): async def invalidate_baseline_cache(logging_obj: Logging, reason: str, *, completed: bool = False) -> None: context: Final = logging_obj.baseline_cache_context if context is not None: - logging_obj.baseline_cache_context = replace( - context, invalidated=reason - ) # rebind-ok: request-owned retry marker + logging_obj.baseline_cache_context = replace(context, invalidated=reason) logging_obj.baseline_observation = context.capture.model_copy( update=MappingProxyType( - { # rebind-ok: capture uncertainty for failure logging + { "observation": context.capture.observation.model_copy( update=MappingProxyType( { diff --git a/litellm/proxy/hooks/batch_rate_limiter.py b/litellm/proxy/hooks/batch_rate_limiter.py index a5b6cabf519..22a17bd4cd8 100644 --- a/litellm/proxy/hooks/batch_rate_limiter.py +++ b/litellm/proxy/hooks/batch_rate_limiter.py @@ -114,9 +114,7 @@ class BatchFileUsage(BaseModel): # each target a different model, so the project's per-model ITPM/OTPM # quota for a row's actual model must be charged with that row's own # tokens -- see `_create_project_io_descriptors_for_models`. - per_model_usage: dict[str, dict[str, int]] = Field( - default_factory=dict - ) # mutable-ok: accumulated incrementally per row while parsing the batch file + per_model_usage: dict[str, dict[str, int]] = Field(default_factory=dict) class _PROXY_BatchRateLimiter(CustomLogger): @@ -465,7 +463,7 @@ class _PROXY_BatchRateLimiter(CustomLogger): body: Final[Mapping[str, object]] = ( MappingProxyType(_BATCH_BODY_ADAPTER.validate_python(raw_body)) if isinstance(raw_body, Mapping) - else MappingProxyType({}) # mutable-ok: immediately frozen empty fallback + else MappingProxyType({}) ) # `max_tokens`/`max_completion_tokens` cap chat completions; `/v1/responses` # rows cap output with `max_output_tokens` instead -- omitting it here diff --git a/litellm/proxy/hooks/parallel_request_limiter_v3.py b/litellm/proxy/hooks/parallel_request_limiter_v3.py index 33744906b13..17cf7382246 100644 --- a/litellm/proxy/hooks/parallel_request_limiter_v3.py +++ b/litellm/proxy/hooks/parallel_request_limiter_v3.py @@ -3210,7 +3210,7 @@ class _PROXY_MaxParallelRequestsHandler_v3(CustomLogger): filtered_content = [ # mutable-ok: token_counter requires list content blocks block for block in content if not (isinstance(block, dict) and block.get("type") == "input_audio") ] - sanitized.append( # mutable-ok: token_counter requires mutable message dicts + sanitized.append( {**message, "content": filtered_content} # mutable-ok: token_counter requires message dicts ) return sanitized @@ -3572,7 +3572,7 @@ class _PROXY_MaxParallelRequestsHandler_v3(CustomLogger): try: await asyncio.shield(cleanup) except asyncio.CancelledError as exc: - cancellation = exc # rebind-ok: retain the latest cancellation without interrupting slot release + cancellation = exc cleanup.result() if cancellation is not None: raise cancellation diff --git a/litellm/proxy/litellm_pre_call_utils.py b/litellm/proxy/litellm_pre_call_utils.py index 3bf6fea62f7..8fc5faee2c9 100644 --- a/litellm/proxy/litellm_pre_call_utils.py +++ b/litellm/proxy/litellm_pre_call_utils.py @@ -137,7 +137,7 @@ def add_otel_trace_id_to_request( return data["litellm_trace_id"] = trace_id # rebind-ok: data is an out-param if isinstance(metadata, dict): - metadata["trace_id"] = trace_id # rebind-ok: metadata is the request's own out-param dict + metadata["trace_id"] = trace_id def _session_id_from_baggage(baggage: str) -> str | None: @@ -3142,11 +3142,7 @@ async def move_guardrails_to_metadata( - Moves include_guardrail_response into request metadata before provider dispatch """ if "include_guardrail_response" in data: - data[_metadata_variable_name][ - "include_guardrail_response" - ] = ( # rebind-ok: pre-call hooks mutate the shared request dict in place - data.pop("include_guardrail_response") is True - ) + data[_metadata_variable_name]["include_guardrail_response"] = data.pop("include_guardrail_response") is True # Early-out: skip all guardrails processing when nothing is configured key_metadata: Final = user_api_key_dict.metadata diff --git a/litellm/proxy/management_endpoints/auto_router_endpoints.py b/litellm/proxy/management_endpoints/auto_router_endpoints.py index 2ae8639fe61..9708161397a 100644 --- a/litellm/proxy/management_endpoints/auto_router_endpoints.py +++ b/litellm/proxy/management_endpoints/auto_router_endpoints.py @@ -1448,7 +1448,7 @@ def _target_labels( """Display labels by (target_type, target_id): a key's (alias, masked name), a team's (alias, None), a user's (email, None).""" return MappingProxyType( - { # mutable-ok: MappingProxyType needs a dict to wrap + { key: value for key, value in chain( ((("key", row.token), (row.key_alias, row.key_name)) for row in key_rows), @@ -1548,7 +1548,7 @@ async def _shadow_eval_results( await _query_raw(prisma_client, _ATTEMPT_AGG_BY_LEG_SQL, leg_ids) or () ) verdicts_by_target: Final[Mapping[tuple[str, str], ShadowEvalSlice]] = MappingProxyType( - { # mutable-ok: MappingProxyType needs a dict to wrap + { target_by_leg[slice.group]: slice.model_copy( update={"group": target_by_leg[slice.group][1]} # mutable-ok: pydantic update payload ) @@ -1760,7 +1760,7 @@ async def start_shadow_eval( "id": leg_id, "target_type": target_type, "target_id": target_id, - } # mutable-ok: Prisma payload + } for leg_id, (target_type, target_id) in zip(leg_ids, requested_targets) ] ) diff --git a/litellm/proxy/management_endpoints/config_override_endpoints.py b/litellm/proxy/management_endpoints/config_override_endpoints.py index b095ecc1fe5..9d182d4e259 100644 --- a/litellm/proxy/management_endpoints/config_override_endpoints.py +++ b/litellm/proxy/management_endpoints/config_override_endpoints.py @@ -796,9 +796,7 @@ async def get_cyberark_config( field_schema: Final = _build_field_schema(CyberArkConfig) - db_record: Final = await _config_overrides_table(prisma_client).find_unique( - where={"config_type": "cyberark"} - ) # mutable-ok: prisma where clause + db_record: Final = await _config_overrides_table(prisma_client).find_unique(where={"config_type": "cyberark"}) if db_record is not None and db_record.config_value is not None: config_data: Final = _parse_config_value(db_record.config_value) @@ -860,9 +858,7 @@ async def delete_cyberark_config( deleted = False # rebind-ok: set true once the DB row is removed try: - await _config_overrides_table(prisma_client).delete( - where={"config_type": "cyberark"} - ) # mutable-ok: prisma where clause + await _config_overrides_table(prisma_client).delete(where={"config_type": "cyberark"}) deleted = True # rebind-ok: set true once the DB row is removed except RecordNotFoundError: verbose_proxy_logger.debug("No existing CyberArk config record to delete") diff --git a/litellm/proxy/management_endpoints/management_v1/budgets.py b/litellm/proxy/management_endpoints/management_v1/budgets.py index ea13e4547bd..106dbfaf7b7 100644 --- a/litellm/proxy/management_endpoints/management_v1/budgets.py +++ b/litellm/proxy/management_endpoints/management_v1/budgets.py @@ -115,7 +115,7 @@ def _scope(caller: UserAPIKeyAuth) -> Scope: # budget_duration is deliberately absent from `sortable`: the column holds strings # like "7d" and "30d", so a lexicographic ORDER BY puts "30d" ahead of "7d". BUDGET_FILTERS: Final[Mapping[str, FilterSpec]] = MappingProxyType( - { # mutable-ok: an immutable mapping has no literal form; MappingProxyType freezes this one and it never escapes + { "budget_duration": FilterSpec(type=str, ops=frozenset(("in", "is_null"))), "max_budget": FilterSpec(type=float, ops=frozenset(("gte", "lte", "is_null"))), "created_at": FilterSpec(type=datetime, ops=frozenset(("gte", "lte"))), diff --git a/litellm/proxy/management_endpoints/mcp_management_endpoints.py b/litellm/proxy/management_endpoints/mcp_management_endpoints.py index 9ad78876043..c22081ebb3a 100644 --- a/litellm/proxy/management_endpoints/mcp_management_endpoints.py +++ b/litellm/proxy/management_endpoints/mcp_management_endpoints.py @@ -706,9 +706,7 @@ if MCP_AVAILABLE: if not caller_user_id: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, - detail={ - "error": "User ID not found in token" - }, # mutable-ok: FastAPI HTTPException detail requires a plain dict + detail={"error": "User ID not found in token"}, ) return caller_user_id @@ -1865,9 +1863,7 @@ if MCP_AVAILABLE: classified: Final = tuple(_classify(index, conversion) for index, conversion in enumerate(conversions)) outcomes: Final = tuple( - [ - await _create(entry) if isinstance(entry, ConvertedConnector) else entry for entry in classified - ] # mutable-ok: await is illegal in a generator expression here + [await _create(entry) if isinstance(entry, ConvertedConnector) else entry for entry in classified] ) imported: Final = tuple(entry for entry in outcomes if isinstance(entry, MCPConnectorImportResult)) diff --git a/litellm/proxy/management_endpoints/scim/scim_v2.py b/litellm/proxy/management_endpoints/scim/scim_v2.py index 3292a0141d1..0cf201b3a00 100644 --- a/litellm/proxy/management_endpoints/scim/scim_v2.py +++ b/litellm/proxy/management_endpoints/scim/scim_v2.py @@ -582,7 +582,6 @@ async def _users_named_by_member_value( subject: Final = value.strip() email: Final[_CaseInsensitiveMatch] = {"equals": subject, "mode": "insensitive"} rows: Final = await _table(UserRepository(prisma_client)).find_many( - # mutable-ok: the Prisma serializer requires concrete dicts and a concrete list where={"OR": [{"sso_user_id": subject}, {"user_email": email}]}, take=take, ) diff --git a/litellm/proxy/management_endpoints/team_endpoints.py b/litellm/proxy/management_endpoints/team_endpoints.py index d092fc2fbb7..09c6c05e22b 100644 --- a/litellm/proxy/management_endpoints/team_endpoints.py +++ b/litellm/proxy/management_endpoints/team_endpoints.py @@ -2996,7 +2996,7 @@ async def _update_team_members_list( # extend() consumes the generator as it appends, so a member already added by this # same call is seen by the next _member_already_in_team check - the batch dedupes # against itself exactly as the append-one-at-a-time loop this replaced did. - complete_team_data.members_with_roles.extend( # rebind-ok: this helper's contract is to grow the caller's roster in place + complete_team_data.members_with_roles.extend( m for m in resolved_members if not _member_already_in_team(m, complete_team_data) ) @@ -4137,9 +4137,7 @@ async def reset_team_member_budget_fn( team_default_budget_id: Final = await _existing_team_default_budget_id(team_obj, prisma_client) budget_link: Final = ( - { - "connect": {"budget_id": team_default_budget_id} - } # mutable-ok: prisma client requires a plain dict data= argument + {"connect": {"budget_id": team_default_budget_id}} if team_default_budget_id is not None else {"disconnect": True} # mutable-ok: same prisma data= argument ) diff --git a/litellm/proxy/management_helpers/bulk_user_creation.py b/litellm/proxy/management_helpers/bulk_user_creation.py index 56abe3b6a3f..dd3f4ff1b12 100644 --- a/litellm/proxy/management_helpers/bulk_user_creation.py +++ b/litellm/proxy/management_helpers/bulk_user_creation.py @@ -543,7 +543,7 @@ async def _write_team_roster( already_present: Final = frozenset(member.user_id for member in roster if member.user_id) new_members: Final = tuple(member for member in members if member.user_id not in already_present) budget_ids: Final = tuple( - [ # mutable-ok: budgets are created one at a time on the transaction's single connection + [ await _resolve_member_budget_id( prisma_client=prisma_client, user_api_key_dict=user_api_key_dict, diff --git a/litellm/proxy/openai_files_endpoints/batch_guardrails.py b/litellm/proxy/openai_files_endpoints/batch_guardrails.py index 53d51db2b7f..1db4474fc40 100644 --- a/litellm/proxy/openai_files_endpoints/batch_guardrails.py +++ b/litellm/proxy/openai_files_endpoints/batch_guardrails.py @@ -355,9 +355,7 @@ def build_scan_metadata(request_metadata: Mapping[str, object]) -> Mapping[str, Passing the whole thing through would carry values that cannot be copied, such as the parent OTel span, and would hand every record proxy state it has no business seeing. """ - return MappingProxyType( - {key: value for key, value in request_metadata.items() if key in _SCAN_METADATA_KEYS} - ) # mutable-ok: MappingProxyType freezes the comprehension + return MappingProxyType({key: value for key, value in request_metadata.items() if key in _SCAN_METADATA_KEYS}) async def _scan_record( @@ -546,7 +544,7 @@ def rewrite_batch_input_file(file_source: BinaryIO, result: BatchScanResult) -> """ redacted: Final = MappingProxyType( {change.line_number: change for change in result.changes if isinstance(change, RecordRedacted)} - ) # mutable-ok: MappingProxyType freezes the lookup table + ) dropped: Final = frozenset(change.line_number for change in result.changes if isinstance(change, RecordDropped)) output: Final = tempfile.SpooledTemporaryFile( # noqa: SIM115 # the caller uploads this handle diff --git a/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py b/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py index 2d071343844..4e60c318f03 100644 --- a/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py +++ b/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py @@ -468,9 +468,7 @@ async def fal_ai_proxy_route( endpoint_func: Final = create_pass_through_route( endpoint=endpoint, target=str(updated_url), - custom_headers={ - "Authorization": f"Key {fal_ai_api_key}" - }, # mutable-ok: pass-through request headers require a mutable mapping + custom_headers={"Authorization": f"Key {fal_ai_api_key}"}, custom_llm_provider="fal_ai", is_streaming_request=False, ) @@ -3801,13 +3799,9 @@ async def gigachat_proxy_route( raw_model: Final = request_body.get("model") model: Final = raw_model if isinstance(raw_model, str) else None if model: - is_router_model = is_passthrough_request_using_router_model( - request_body, llm_router - ) # rebind-ok: conditionally set to True + is_router_model = is_passthrough_request_using_router_model(request_body, llm_router) elif any(word in endpoint for word in ("completions", "embeddings")): - raise HTTPException( - status_code=400, detail={"error": "Model is required in request body"} - ) # mutable-ok: HTTPException detail dict + raise HTTPException(status_code=400, detail={"error": "Model is required in request body"}) # If router model, use dedicated router passthrough handler # This uses the same common processing path as non-router models @@ -3908,9 +3902,7 @@ async def handle_gigachat_passthrough_router_model( is_streaming: Final = request_body.get("stream", False) # pyright: ignore[reportUnknownVariableType] # request_body is dict[Unknown, Unknown] - data: dict[str, Any] = await _read_request_body( - request=request - ) # mutable-ok: mutated in place by proxy pipeline; pyright: ignore[reportExplicitAny] # Any needed for proxy pipeline + data: dict[str, Any] = await _read_request_body(request=request) # Any needed for proxy pipeline if user_api_key_dict is not None: auth_metadata: Final = { metadata_key: value diff --git a/litellm/proxy/pass_through_endpoints/llm_provider_handlers/vertex_passthrough_logging_handler.py b/litellm/proxy/pass_through_endpoints/llm_provider_handlers/vertex_passthrough_logging_handler.py index 48c1ced47ae..2cdeddbea30 100644 --- a/litellm/proxy/pass_through_endpoints/llm_provider_handlers/vertex_passthrough_logging_handler.py +++ b/litellm/proxy/pass_through_endpoints/llm_provider_handlers/vertex_passthrough_logging_handler.py @@ -447,9 +447,7 @@ class VertexPassthroughLoggingHandler: kwargs["model"] = model # rebind-ok: callback metadata records the resolved model kwargs["custom_llm_provider"] = "vertex_ai" # rebind-ok: callback metadata records the resolved provider - standard_pass_through_response_object: Final[ - StandardPassThroughResponseObject - ] = { # mutable-ok: callback contract requires a concrete response dictionary + standard_pass_through_response_object: Final[StandardPassThroughResponseObject] = { "response": json_response, } return { # mutable-ok: passthrough logging contract requires a concrete result dictionary diff --git a/litellm/proxy/pass_through_endpoints/managed_id_rewriter.py b/litellm/proxy/pass_through_endpoints/managed_id_rewriter.py index 567d8375737..8e1dba928af 100644 --- a/litellm/proxy/pass_through_endpoints/managed_id_rewriter.py +++ b/litellm/proxy/pass_through_endpoints/managed_id_rewriter.py @@ -81,9 +81,7 @@ if TYPE_CHECKING: from litellm.integrations.custom_logger import CustomLogger from litellm.proxy.utils import PrismaClient -_RowT = TypeVar( - "_RowT", bound=ManagedResourceRow -) # rebind-ok: TypeVar declarations must stay bare assignments for pyright +_RowT = TypeVar("_RowT", bound=ManagedResourceRow) # --------------------------------------------------------------------------- # Field map @@ -998,9 +996,7 @@ async def _build_list_where_with_cursor( params: Final = query_params or {} after_id: Final[str | None] = params.get("after") before_id: Final[str | None] = params.get("before") - where: PrismaWhere = dict( - owner_filter - ) # rebind-ok: narrowed with the cursor boundary when a valid cursor row exists + where: PrismaWhere = dict(owner_filter) fetch_order: SortOrder = "desc" # rebind-ok: flipped to asc when paging backwards from a before cursor cursor_id: Final = after_id or before_id diff --git a/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py b/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py index c2874ac948f..f985c1d49d1 100644 --- a/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py +++ b/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py @@ -819,9 +819,7 @@ def _resolve_team_callback_wiring( user_api_key_dict=user_api_key_dict, proxy_config=proxy_config ) if callback_settings_obj and callback_settings_obj.callback_vars: - for ( - item - ) in callback_settings_obj.callback_vars.items(): # rebind-ok: dict.items iteration for env-ref validation + for item in callback_settings_obj.callback_vars.items(): validate_no_callback_env_reference(item[0], item[1], source="key/team callback metadata") except Exception: # noqa: BLE001 - a broken logging config must never fail the passthrough request verbose_proxy_logger.exception( diff --git a/litellm/proxy/pass_through_endpoints/streaming_handler.py b/litellm/proxy/pass_through_endpoints/streaming_handler.py index e1f13f2bee0..8f0f87e6e69 100644 --- a/litellm/proxy/pass_through_endpoints/streaming_handler.py +++ b/litellm/proxy/pass_through_endpoints/streaming_handler.py @@ -217,9 +217,7 @@ class PassThroughStreamingHandler: async for chunk in response.aiter_bytes(): raw_bytes.append(chunk) PassThroughStreamingHandler._stamp_first_chunk_if_needed(litellm_logging_obj) - complete_frames, pending = split_complete_sse_frames( - pending + chunk - ) # rebind-ok: SSE frame reassembly buffer across transport chunks + complete_frames, pending = split_complete_sse_frames(pending + chunk) if complete_frames: yield ProxyBaseLLMRequestProcessing._process_chunk_with_cost_injection( complete_frames, resolved_model_name, litellm_logging_obj diff --git a/litellm/proxy/policy_engine/pipeline_executor.py b/litellm/proxy/policy_engine/pipeline_executor.py index e9d23436b59..6c05ca0b22c 100644 --- a/litellm/proxy/policy_engine/pipeline_executor.py +++ b/litellm/proxy/policy_engine/pipeline_executor.py @@ -108,7 +108,7 @@ _GuardrailMethodT = TypeVar("_GuardrailMethodT", bound=Callable[..., object]) def _logged_by_inner_guardrail(method: _GuardrailMethodT) -> _GuardrailMethodT: - vars(method)[LOGS_GUARDRAIL_INFORMATION_MARKER] = True # rebind-ok: stamps the method the class body just defined + vars(method)[LOGS_GUARDRAIL_INFORMATION_MARKER] = True return method @@ -278,9 +278,7 @@ def _prepare_hook_input( guardrail loops do this.""" if "metadata" not in data: data["metadata"] = {} # mutable-ok: request metadata bucket, hooks mutate it - data["metadata"]["guardrails"] = [ - step.guardrail - ] # mutable-ok: guardrails list is part of the request-payload shape + data["metadata"]["guardrails"] = [step.guardrail] scans_raw_request: Final = callback.scan_raw_request hook_input: Final[dict] = ( # mutable-ok: same request-payload shape as data @@ -456,7 +454,7 @@ class PipelineExecutor: observer: Final = _StreamRewriteObserver(scanner) deliver_rewrites: Final = type(endpoint_translation).delivers_ended_stream_rewrites originals: Final = copy.deepcopy(streaming_chunks) - hook_input.pop("response", None) # rebind-ok: an earlier step's stored response goes so this step's is stored + hook_input.pop("response", None) try: if deliver_rewrites: await endpoint_translation.process_output_streaming_response( @@ -582,7 +580,7 @@ class PipelineExecutor: {"response": response}, None, None, - ) # mutable-ok: modified-data contract is a plain dict + ) return ("pass", response if isinstance(response, dict) else None, None, None) except Exception as e: diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 9a42ee75c51..26a423d7162 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -5261,9 +5261,7 @@ class ProxyConfig: return with open(f"{user_config_file_path}", "w") as config_file: - yaml.dump( - dict(new_config), config_file, default_flow_style=False - ) # mutable-ok: YAML must serialize a plain dict + yaml.dump(dict(new_config), config_file, default_flow_style=False) async def _save_changed_config_section( self, @@ -10137,7 +10135,7 @@ class ProxyStartupEvent: str(identity): str(fingerprint) for identity, fingerprint in (decoded.items() if isinstance(decoded, Mapping) else ()) } - ) # mutable-ok: MappingProxyType owns the completed immutable baseline + ) snapshot: Final = snapshot_tuning_baselines(deployments) try: await config_table.create( @@ -10163,7 +10161,7 @@ class ProxyStartupEvent: competing_decoded.items() if isinstance(competing_decoded, Mapping) else () ) } - ) # mutable-ok: MappingProxyType owns the completed immutable baseline + ) except Exception as e: # noqa: BLE001 # enforcement is skipped for this boot; refusing every tuned router on a DB blip is the one outcome the gate forbids verbose_proxy_logger.warning("Heuristic-v1 tuning baseline unavailable, gate not enforced this boot: %s", e) return None @@ -10199,7 +10197,7 @@ class ProxyStartupEvent: proxy_logging_obj: ProxyLogging, ) -> ProxyWorkerHeartbeat: """Initializes scheduled background jobs""" - global heuristic_v1_tuning_baselines, store_model_in_db, scheduler, scheduler_executor # rebind-ok: startup publishes the one read-only baseline snapshot + global heuristic_v1_tuning_baselines, store_model_in_db, scheduler, scheduler_executor # MEMORY LEAK FIX: Configure scheduler with optimized settings # Memray analysis showed APScheduler's normalize() and _apply_jitter() causing diff --git a/litellm/proxy/rag_endpoints/endpoints.py b/litellm/proxy/rag_endpoints/endpoints.py index 4f0c9f42421..974bff6338a 100644 --- a/litellm/proxy/rag_endpoints/endpoints.py +++ b/litellm/proxy/rag_endpoints/endpoints.py @@ -824,7 +824,7 @@ async def rag_query( merged_retrieval_config: Final = { **retrieval_config, **store_data, - } # mutable-ok: litellm.aquery requires a plain dict payload + } # Add litellm data request_data: dict[str, object] = {} diff --git a/litellm/proxy/response_api_endpoints/endpoints.py b/litellm/proxy/response_api_endpoints/endpoints.py index 36b7a3a4a8a..69c7f0a09ed 100644 --- a/litellm/proxy/response_api_endpoints/endpoints.py +++ b/litellm/proxy/response_api_endpoints/endpoints.py @@ -97,11 +97,7 @@ def _normalize_tool_dialect( tools: Final = data.get("tools") tool_choice: Final = data.get("tool_choice") normalized_tools: Final = ( - [ - _convert_tool_envelope(tool, to_chat=to_chat) for tool in tools - ] # mutable-ok: body's tools stays a plain JSON list - if isinstance(tools, list) - else tools + [_convert_tool_envelope(tool, to_chat=to_chat) for tool in tools] if isinstance(tools, list) else tools ) normalized_choice: Final = _convert_tool_envelope(tool_choice, to_chat=to_chat) if normalized_tools == tools and normalized_choice == tool_choice: diff --git a/litellm/proxy/spend_tracking/carried_budget_state.py b/litellm/proxy/spend_tracking/carried_budget_state.py index da8bf60ebda..0dfb38272b0 100644 --- a/litellm/proxy/spend_tracking/carried_budget_state.py +++ b/litellm/proxy/spend_tracking/carried_budget_state.py @@ -36,9 +36,7 @@ def carry_team_and_user_budget_state( def carry_organization_budget_state(valid_token: UserAPIKeyAuth, org_table: LiteLLM_OrganizationTable) -> None: budget_table: Final = org_table.litellm_budget_table - valid_token.organization_alias = ( - org_table.organization_alias - ) # rebind-ok: the request credential is pinned in place + valid_token.organization_alias = org_table.organization_alias valid_token.org_budget_snapshot = OrgBudgetSnapshot( # rebind-ok: same object the caller keeps using spend=org_table.spend, max_budget=budget_table.max_budget if budget_table is not None else None, diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index e25b3bed757..78d6c25a336 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -602,15 +602,11 @@ def _partition_post_call_callbacks() -> tuple[tuple[CustomGuardrail, ...], tuple return (guardrails, others) -def _merge_pipeline_metadata_bucket( - data: dict, bucket_key: str, modified_bucket_value: object -) -> None: # mutable-ok: request payload dict, written in place +def _merge_pipeline_metadata_bucket(data: dict, bucket_key: str, modified_bucket_value: object) -> None: if not isinstance(modified_bucket_value, dict): return modified_bucket: Final = cast("dict[str, object]", modified_bucket_value) # cast-ok: metadata buckets are str-keyed - surviving_writes: Final = { - key: value for key, value in modified_bucket.items() if key != "guardrails" - } # mutable-ok: merged into the live request metadata bucket in place + surviving_writes: Final = {key: value for key, value in modified_bucket.items() if key != "guardrails"} existing_bucket: Final = data.get(bucket_key) if isinstance(existing_bucket, dict): cast("dict[str, object]", existing_bucket).update(surviving_writes) # cast-ok: metadata buckets are str-keyed @@ -618,9 +614,7 @@ def _merge_pipeline_metadata_bucket( data[bucket_key] = surviving_writes -def _merge_pipeline_metadata_writes( - data: dict, modified_data: Mapping[str, object] -) -> None: # mutable-ok: request payload dict, written in place +def _merge_pipeline_metadata_writes(data: dict, modified_data: Mapping[str, object]) -> None: """ Copy metadata-bucket writes from a pipeline's working copy back onto the request. @@ -1052,7 +1046,6 @@ def _deployment_attribution_for_model_group(model_group: object, team_id: str | ) return MappingProxyType( { - # mutable-ok: frozen immediately by the outer MappingProxyType **({"custom_llm_provider": shared_provider} if shared_provider is not None else {}), **( { # mutable-ok: frozen immediately by the outer MappingProxyType @@ -1976,9 +1969,7 @@ class ProxyLogging: """ scans_raw_request: Final = callback.scan_raw_request should_use_raw_snapshot: Final = scans_raw_request and raw_request_snapshot is not None - input_data: Final = ( # mutable-ok: same request-payload shape as data - independent_snapshot(raw_request_snapshot) if should_use_raw_snapshot else data - ) + input_data: Final = independent_snapshot(raw_request_snapshot) if should_use_raw_snapshot else data # _process_guardrail_callback always calls mark_pre_call_hook_ran on a # successful run, which unconditionally stamps bookkeeping metadata onto # the dict regardless of whether the guardrail's own hook mutated @@ -2169,9 +2160,7 @@ class ProxyLogging: if pipeline.mode != event_hook: continue - step_input: dict = ( - {**data, "response": current_response} if current_response is not None else data - ) # mutable-ok: same request-payload shape as data + step_input: dict = {**data, "response": current_response} if current_response is not None else data result: PipelineExecutionResult = await PipelineExecutor.execute_steps( steps=pipeline.steps, diff --git a/litellm/rerank_api/main.py b/litellm/rerank_api/main.py index 37ca989b8d3..18ecc250b64 100644 --- a/litellm/rerank_api/main.py +++ b/litellm/rerank_api/main.py @@ -44,9 +44,7 @@ async def arerank( """ Async: Reranks a list of documents based on their relevance to the query """ - _custom_llm_provider: str | None = ( - None # rebind-ok: set by the declared-provider guard or the get_llm_provider unpack; read in the except - ) + _custom_llm_provider: str | None = None try: loop: Final = asyncio.get_event_loop() kwargs["arerank"] = True diff --git a/litellm/responses/additional_tools.py b/litellm/responses/additional_tools.py index ea0d7af350c..5239bd395cc 100644 --- a/litellm/responses/additional_tools.py +++ b/litellm/responses/additional_tools.py @@ -37,12 +37,7 @@ def _tools_of_item(item: object) -> tuple[ALL_RESPONSES_API_TOOL_PARAMS, ...]: parsed: Final = _AdditionalToolsItem.model_validate(item) except ValidationError: return () - return tuple( - cast( - "ALL_RESPONSES_API_TOOL_PARAMS", tool - ) # cast-ok: nested tools carry the same raw tool JSON as top-level tools - for tool in parsed.tools - ) + return tuple(cast("ALL_RESPONSES_API_TOOL_PARAMS", tool) for tool in parsed.tools) def hoist_additional_tools( diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index 3ca2cc28c9a..e421cae0724 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -866,14 +866,14 @@ class LiteLLMCompletionResponsesConfig: elif pending: # Not followed by an assistant message — keep the reasoning # standalone instead of dropping it. - merged.extend( # mutable-ok: append reasoning messages + merged.extend( [_standalone(text, blocks) for text, blocks in pending] # mutable-ok: append reasoning messages ) pending = [] # mutable-ok: reset accumulator merged.append(msg) - merged.extend( # mutable-ok: append trailing reasoning + merged.extend( [_standalone(text, blocks) for text, blocks in pending] # mutable-ok: append trailing reasoning ) diff --git a/litellm/responses/streaming_iterator.py b/litellm/responses/streaming_iterator.py index 59655800af6..64989c4cf1c 100644 --- a/litellm/responses/streaming_iterator.py +++ b/litellm/responses/streaming_iterator.py @@ -170,7 +170,7 @@ def _log_background_task_failure(task: asyncio.Task[object], *, task_name: str) _ERROR_CODE_HTTP_STATUS: Final[Mapping[str, int]] = MappingProxyType( - { # mutable-ok: immediately frozen by MappingProxyType + { "server_error": 500, "rate_limit_exceeded": 429, "insufficient_quota": 429, @@ -1633,9 +1633,7 @@ def _extract_frame_quota_estimate_inputs(msg_obj: Mapping[str, object]) -> tuple params: Final[Mapping[str, object]] = ( nested if _is_json_object(nested) and nested - else MappingProxyType( # mutable-ok: immediately frozen filtered frame - {k: v for k, v in msg_obj.items() if k != "type"} - ) + else MappingProxyType({k: v for k, v in msg_obj.items() if k != "type"}) ) text_parts: Final[list[str]] = [] # mutable-ok: local accumulator built in one pass, not shared pending: Final[list[object]] = [ # mutable-ok: explicit worklist avoids recursion @@ -2297,7 +2295,7 @@ class ResponsesWebSocketStreaming: except RateLimitError as e: try: await self.websocket.send_text( - json.dumps( # mutable-ok: WebSocket wire payload requires JSON objects + json.dumps( { # mutable-ok: WebSocket wire payload requires JSON objects "type": "error", "error": { # mutable-ok: nested WebSocket error object @@ -2743,9 +2741,7 @@ class ManagedResponsesWebSocketHandler: directly (before serialization) to avoid a redundant JSON round-trip on every chunk. Returns the completed event dict, or ``None``. """ - completed_event: _MutableJsonObject | None = ( - None # rebind-ok: captures the completed event once the stream yields it - ) + completed_event: _MutableJsonObject | None = None stream_response: Final = await litellm.aresponses(model=model, **call_kwargs) async for chunk in stream_response: if chunk is None: diff --git a/litellm/responses/utils.py b/litellm/responses/utils.py index a2642795cea..9b0d259eb8a 100644 --- a/litellm/responses/utils.py +++ b/litellm/responses/utils.py @@ -566,7 +566,7 @@ class ResponsesAPIRequestUtils: return items: Final = cast(list[object], request_input) # cast-ok: untyped client json stripped: Final = tuple(ResponsesAPIRequestUtils._without_encrypted_reasoning(item) for item in items) - items[:] = (item for item in stripped if item is not None) # rebind-ok: list shared with fallback snapshot + items[:] = (item for item in stripped if item is not None) @staticmethod def _without_encrypted_reasoning(item: object) -> object | None: diff --git a/litellm/router.py b/litellm/router.py index 7267f6eb3ba..62042e1c969 100644 --- a/litellm/router.py +++ b/litellm/router.py @@ -675,7 +675,7 @@ class RoutingArgs(enum.Enum): # entries their deployments own. Weak so a router nothing references any more, such # as the per-request one built from a caller-supplied user_config, drops out on its # own rather than leaving entries behind that nothing can withdraw. -_live_routers: Final["weakref.WeakSet[Router]"] = weakref.WeakSet() # mutable-ok: identity set of live routers +_live_routers: Final["weakref.WeakSet[Router]"] = weakref.WeakSet() def _replay_live_router_model_cost() -> None: @@ -2954,10 +2954,10 @@ class Router: fallback_headers_are_settled = False async for fallback_item in fallback_response: if not fallback_headers_are_settled: - fallback_headers_are_settled = True # rebind-ok: one-shot latch + fallback_headers_are_settled = True # a fallback that failed over again only repoints itself once it yields - prepared_fallback_hidden_params = ( # rebind-ok: re-read once the fallback yields - Router._adopt_fallback_response_headers(wrapper_ref, fallback_response) + prepared_fallback_hidden_params = Router._adopt_fallback_response_headers( + wrapper_ref, fallback_response ) Router._apply_fallback_hidden_params_to_item(fallback_item, prepared_fallback_hidden_params) if ( @@ -3513,10 +3513,10 @@ class Router: fallback_headers_are_settled = False for fallback_item in fallback_response: if not fallback_headers_are_settled: - fallback_headers_are_settled = True # rebind-ok: one-shot latch + fallback_headers_are_settled = True # a fallback that failed over again only repoints itself once it yields - prepared_fallback_hidden_params = ( # rebind-ok: re-read once the fallback yields - Router._adopt_fallback_response_headers(wrapper_ref, fallback_response) + prepared_fallback_hidden_params = Router._adopt_fallback_response_headers( + wrapper_ref, fallback_response ) Router._apply_fallback_hidden_params_to_item(fallback_item, prepared_fallback_hidden_params) if ( @@ -5459,23 +5459,23 @@ class Router: if _anthropic_stream_should_drop_pre_content_ping(chunk, has_generated_content): continue if _anthropic_stream_commits_now(chunk, has_generated_content, len(buffered_lifecycle_chunks)): - has_generated_content = True # rebind-ok: real content seen, or the buffer cap was hit + has_generated_content = True # A transport can split one SSE data line across byte chunks, so pre-content # detection parses the accumulated buffer plus the current chunk, never the # chunk alone; the buffer is already capped, which bounds this window too. - parse_window = ( # rebind-ok: freshly computed each iteration, never carried over + parse_window = ( b"".join(c for c in (*buffered_lifecycle_chunks, chunk) if isinstance(c, (bytes, bytearray))) # pyright: ignore[reportUnnecessaryIsInstance] # bridge-path chunks are not always bytes at runtime if not has_generated_content and isinstance(chunk, (bytes, bytearray)) # pyright: ignore[reportUnnecessaryIsInstance] # bridge-path chunks are not always bytes at runtime else chunk ) error_event = parse_anthropic_error_event(parse_window) - retriable_pending_error = ( # rebind-ok: freshly computed each iteration, never carried over + retriable_pending_error = ( not has_generated_content and error_event is not None and _is_retriable_anthropic_status(error_event[2]) and not _anthropic_stream_error_is_gateway_verdict(chunk) ) - refusal_stop_details = ( # rebind-ok: freshly computed each iteration, never carried over + refusal_stop_details = ( parse_anthropic_refusal_stop_details(parse_window) if not has_generated_content and error_event is None else None @@ -5493,7 +5493,7 @@ class Router: buffered_lifecycle_chunks = (*buffered_lifecycle_chunks, chunk) continue if retriable_pending_error: - assert error_event is not None # guard-ok: retriable_pending_error implies this + assert error_event is not None _error_type, message, status_code = error_event raise MidStreamFallbackError( message=message, @@ -10951,10 +10951,8 @@ class Router: model_group_info.supports_fast_mode = model_group_info.supports_fast_mode and ( AnthropicModelInfo.supports_fast_mode(litellm_model, llm_provider) ) - deployment_reasoning_efforts = ( - resolve_supported_reasoning_efforts( # rebind-ok: recalculated per deployment - model_info, deployment_is_mapped=deployment_is_mapped - ) + deployment_reasoning_efforts = resolve_supported_reasoning_efforts( + model_info, deployment_is_mapped=deployment_is_mapped ) if deployment_reasoning_efforts is None: reasoning_efforts_unknown = True diff --git a/litellm/router_strategy/complexity_router/complexity_router.py b/litellm/router_strategy/complexity_router/complexity_router.py index 1f4285a7960..0f252952a9d 100644 --- a/litellm/router_strategy/complexity_router/complexity_router.py +++ b/litellm/router_strategy/complexity_router/complexity_router.py @@ -1093,7 +1093,7 @@ def _with_classifier_forecast( if forecast is None: return decision verdict: Final = forecast.verdict - enriched: Final[StandardLoggingRoutingDecision] = { # mutable-ok: routing decisions are JSON TypedDict records + enriched: Final[StandardLoggingRoutingDecision] = { **decision, "classifier_crux": verdict.crux, "classifier_primary_rule": verdict.primary_rule, @@ -2484,7 +2484,7 @@ class ComplexityRouter(CustomLogger): {"role": "user", "content": opening_task}, # mutable-ok: SDK messages are dict-shaped ] if latest_follow_up is not None: - task_messages.append( # mutable-ok: the provider SDK requires a concrete message list + task_messages.append( {"role": "user", "content": latest_follow_up} # mutable-ok: SDK messages are dict-shaped ) diff --git a/litellm/router_strategy/tag_based_routing.py b/litellm/router_strategy/tag_based_routing.py index d4f46e94579..50dce250920 100644 --- a/litellm/router_strategy/tag_based_routing.py +++ b/litellm/router_strategy/tag_based_routing.py @@ -217,9 +217,7 @@ def _strip_routing_prefix(tags: Sequence[str], prefix: str) -> tuple[tuple[str, def _split_tags(tags: Sequence[str]) -> tuple[tuple[str, ...], list[str], tuple[str, ...]]: required: Final = tuple(tag[1:] for tag in tags if tag.startswith("&") and len(tag) > 1) - positive: Final = [ - t for t in tags if not t.startswith("!") and not t.startswith("&") - ] # mutable-ok: feeds _match_deployment's existing list[str]-typed request_tags param + positive: Final = [t for t in tags if not t.startswith("!") and not t.startswith("&")] excluded: Final = tuple(tag[1:] for tag in tags if tag.startswith("!") and len(tag) > 1) return required, positive, excluded diff --git a/litellm/router_utils/auto_router_tuning_baseline.py b/litellm/router_utils/auto_router_tuning_baseline.py index e87548bf6de..4707a51dfb8 100644 --- a/litellm/router_utils/auto_router_tuning_baseline.py +++ b/litellm/router_utils/auto_router_tuning_baseline.py @@ -120,7 +120,7 @@ def snapshot_tuning_baselines(deployments: Iterable[Mapping[str, object]]) -> Ma if (pair := heuristic_v1_router_fingerprint(deployment)) is not None for identity, fingerprint in (pair,) } - ) # mutable-ok: MappingProxyType owns the completed immutable snapshot + ) def is_mutable_tuned_candidate(candidate: Mapping[str, object], baselines: Mapping[str, str]) -> bool: diff --git a/litellm/router_utils/fallback_event_handlers.py b/litellm/router_utils/fallback_event_handlers.py index 4745e4094e6..60585b3cc38 100644 --- a/litellm/router_utils/fallback_event_handlers.py +++ b/litellm/router_utils/fallback_event_handlers.py @@ -655,7 +655,7 @@ async def run_async_fallback( # LOGGING kwargs = litellm_router.log_retry(kwargs=kwargs, e=original_exception) verbose_router_logger.info("Falling back to model_group = %s", mask_sensitive_structure(mg)) - kwargs.pop("_target_order", None) # rebind-ok: next hop must not inherit the previous order target + kwargs.pop("_target_order", None) if isinstance(mg, str): kwargs["model"] = mg elif isinstance(mg, dict): diff --git a/litellm/rust_bridge/lifecycle.py b/litellm/rust_bridge/lifecycle.py index 4096d386964..b3b7a1888c3 100644 --- a/litellm/rust_bridge/lifecycle.py +++ b/litellm/rust_bridge/lifecycle.py @@ -46,7 +46,7 @@ class StreamClosed(Exception): async def _settle(execution: Execution, step: Step) -> Settled: while isinstance(step, Await): try: - value = await step.awaitable # rebind-ok: each selected await produces the next protocol input + value = await step.awaitable except GeneratorExit: raise except BaseException as error: diff --git a/litellm/rust_bridge/logger.py b/litellm/rust_bridge/logger.py index bcd53852a2f..544e7195848 100644 --- a/litellm/rust_bridge/logger.py +++ b/litellm/rust_bridge/logger.py @@ -53,7 +53,7 @@ def emit( extra={ "rust_target": target, "rust_fields": dict(fields), - }, # mutable-ok: LogRecord requires JSON dict extras + }, ) _REDACTION.filter(record) _CORRELATION.filter(record) diff --git a/litellm/types/management_endpoints/auto_router_endpoints.py b/litellm/types/management_endpoints/auto_router_endpoints.py index 334ca0dfb08..e191470ec6e 100644 --- a/litellm/types/management_endpoints/auto_router_endpoints.py +++ b/litellm/types/management_endpoints/auto_router_endpoints.py @@ -156,7 +156,7 @@ class AutoRouterRoutingTestRequest(BaseModel): the serving path. """ return MappingProxyType( - { # mutable-ok: MappingProxyType needs a dict to wrap + { key: value for key, value in (("messages", self.messages), ("system", self.system), ("tools", self.tools)) if value is not None diff --git a/litellm/types/passthrough_endpoints/managed_id_rewriter.py b/litellm/types/passthrough_endpoints/managed_id_rewriter.py index 675aae96a5b..33749cc2ab8 100644 --- a/litellm/types/passthrough_endpoints/managed_id_rewriter.py +++ b/litellm/types/passthrough_endpoints/managed_id_rewriter.py @@ -55,9 +55,7 @@ class ManagedObjectRow(ManagedResourceRow, Protocol): unified_object_id: str -RowT = TypeVar( - "RowT", bound=ManagedResourceRow -) # rebind-ok: TypeVar declarations must stay bare assignments for pyright +RowT = TypeVar("RowT", bound=ManagedResourceRow) class ManagedTable(Protocol[RowT]): diff --git a/litellm/types/utils.py b/litellm/types/utils.py index 3f8471cbde1..064e3040054 100644 --- a/litellm/types/utils.py +++ b/litellm/types/utils.py @@ -1354,9 +1354,7 @@ def add_provider_specific_fields(object: BaseModel, provider_specific_fields: di class Message(SafeAttributeModel, OpenAIObject): content: str | None role: Literal["assistant", "user", "system", "tool", "function"] - tool_calls: ( - list[ChatCompletionMessageToolCall | ChatCompletionMessageCustomToolCall] | None - ) # mutable-ok: public pydantic response field; only the union member is new + tool_calls: list[ChatCompletionMessageToolCall | ChatCompletionMessageCustomToolCall] | None function_call: FunctionCall | None audio: ChatCompletionAudioResponse | None = None images: list[ImageURLListItem] | None = None @@ -1479,9 +1477,7 @@ class Delta(SafeAttributeModel, OpenAIObject): content: str | None role: str | None function_call: FunctionCall | None - tool_calls: ( - list[ChatCompletionDeltaToolCall | ChatCompletionDeltaCustomToolCall] | None - ) # mutable-ok: public pydantic response field; only the union member is new + tool_calls: list[ChatCompletionDeltaToolCall | ChatCompletionDeltaCustomToolCall] | None audio: ChatCompletionAudioResponse | None images: list[ImageURLListItem] | None annotations: list[ChatCompletionAnnotation] | None diff --git a/litellm/utils.py b/litellm/utils.py index f3b9070ecff..64097021dff 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -1927,9 +1927,7 @@ def client(original_function): is_completion_with_fallbacks: Final = kwargs.get("fallbacks") is not None kwargs.pop("_is_litellm_internal_call", None) # discard if injected _is_litellm_internal_call: Final = is_internal_call.get() - _deployment_call_end_time: datetime.datetime | None = ( - None # rebind-ok: set once, from inside the except below, only if the model call itself fails - ) + _deployment_call_end_time: datetime.datetime | None = None try: if logging_obj is None: @@ -2743,9 +2741,7 @@ def _supports_factory(model: str, custom_llm_provider: str | None, key: str) -> try: declared: Final = declared_authenticating_provider(model, custom_llm_provider) if declared is not None: - model = model.removeprefix( - f"{declared}/" - ) # rebind-ok: mirrors get_llm_provider's split without its OAuth flow + model = model.removeprefix(f"{declared}/") custom_llm_provider = declared # rebind-ok: same else: model, custom_llm_provider, _, _ = litellm.get_llm_provider( @@ -2846,9 +2842,7 @@ def is_explicitly_disabled_factory(model: str, custom_llm_provider: str | None, try: declared: Final = declared_authenticating_provider(model, custom_llm_provider) if declared is not None: - model = model.removeprefix( - f"{declared}/" - ) # rebind-ok: mirrors get_llm_provider's split without its OAuth flow + model = model.removeprefix(f"{declared}/") custom_llm_provider = declared # rebind-ok: same else: model, custom_llm_provider, _, _ = litellm.get_llm_provider( diff --git a/scripts/check_type_discipline.py b/scripts/check_type_discipline.py index a2ab4760c4f..2bb65072ad4 100644 --- a/scripts/check_type_discipline.py +++ b/scripts/check_type_discipline.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 """Type-discipline checker: the rules ruff can't enforce. - + Rules ----- LIT001 Mutable collection in a type annotation, anywhere it appears: function @@ -99,19 +99,23 @@ LIT012 TypedDict field without a `ReadOnly[...]` qualifier. A writable key lets the functional form (`X = TypedDict("X", {...})`) is checked too. A base imported from another module is out of reach without import resolution. Suppress with `# writable-ok: `. +LIT013 A `# -ok: ` suppression on a line where none of the rules + that token suppresses fires. Like ruff's RUF100: a marker that suppresses + nothing rots in place and hides real violations that land on the line + later. Delete it. LIT000 Setup failure: a target file could not be read, or contains a syntax error. Reported as a violation rather than crashing the run. - + Usage ----- python check_type_discipline.py litellm/ tests/ Exit code 1 if any violation is found. Stdlib only. """ - + from __future__ import annotations - + import ast import io import os @@ -122,28 +126,50 @@ from dataclasses import dataclass from multiprocessing import Pool from pathlib import Path from collections.abc import Iterable, Iterator, Mapping, Sequence +from types import MappingProxyType from typing import NamedTuple - + # Mutable collection types, banned in *every* annotation. Name-based, so `dict`, # `typing.Dict`, `collections.deque`, and `collections.abc.MutableMapping` all match # however they were imported. The read-only interfaces (Mapping, Sequence, the # immutable AbstractSet / `abc.Set`, Collection) and the immutable concretes (tuple, # frozenset) are the escape hatch and are deliberately absent -- as is the bare name # `Set`, which collides with the read-only `collections.abc.Set`. -MUTABLE_COLLECTIONS = frozenset(( - "dict", "list", "set", - "Dict", "List", "DefaultDict", "OrderedDict", "Counter", "Deque", "ChainMap", - "deque", "defaultdict", - "MutableMapping", "MutableSequence", "MutableSet", -)) +MUTABLE_COLLECTIONS = frozenset( + ( + "dict", + "list", + "set", + "Dict", + "List", + "DefaultDict", + "OrderedDict", + "Counter", + "Deque", + "ChainMap", + "deque", + "defaultdict", + "MutableMapping", + "MutableSequence", + "MutableSet", + ) +) # Callables whose result is a fresh *mutable* collection (LIT002). `tuple` and # `frozenset` are deliberately absent -- they are the wrappers you reach for, and # a generator expression fed to them is the blessed one-shot build. -MUTABLE_CONSTRUCTORS = frozenset(( - "dict", "list", "set", - "deque", "defaultdict", "OrderedDict", "Counter", "ChainMap", -)) +MUTABLE_CONSTRUCTORS = frozenset( + ( + "dict", + "list", + "set", + "deque", + "defaultdict", + "OrderedDict", + "Counter", + "ChainMap", + ) +) # A *qualified* call (`x.deque()`) counts as construction only for names that are rarely # method names; `dict`/`list`/`set` are dropped here because `.dict()` / `.set()` / `.list()` # are common methods (e.g. pydantic's `model.dict()`), not collection construction. A @@ -165,7 +191,7 @@ READONLY_QUALIFIER = "ReadOnly" FIELD_QUALIFIER_WRAPPERS = frozenset(("Required", "NotRequired", "Annotated")) TYPEDDICT_BASE = "TypedDict" MIN_REASON_LEN = 3 - + NOQA_RE = re.compile( r"#\s*noqa" r"(?P:\s*(?P[A-Z]+[0-9]+(?:\s*,\s*[A-Z]+[0-9]+)*))?" @@ -173,9 +199,7 @@ NOQA_RE = re.compile( re.IGNORECASE, ) TYPE_IGNORE_RE = re.compile(r"#\s*type:\s*ignore\b") -IGNORE_RE = re.compile( - r"#\s*(?:pyright|mypy):\s*ignore(?P\[[^\]]*\])?(?P.*)" -) +IGNORE_RE = re.compile(r"#\s*(?:pyright|mypy):\s*ignore(?P\[[^\]]*\])?(?P.*)") MUTABLE_OK_RE = re.compile(r"#\s*mutable-ok(?::\s*(?P.*))?") CAST_OK_RE = re.compile(r"#\s*cast-ok(?::\s*(?P.*))?") GUARD_OK_RE = re.compile(r"#\s*guard-ok(?::\s*(?P.*))?") @@ -183,48 +207,45 @@ KWARGS_OK_RE = re.compile(r"#\s*kwargs-ok(?::\s*(?P.*))?") REBIND_OK_RE = re.compile(r"#\s*rebind-ok(?::\s*(?P.*))?") WRITABLE_OK_RE = re.compile(r"#\s*writable-ok(?::\s*(?P.*))?") +@dataclass(frozen=True, slots=True) +class _OkToken: + """One `*-ok` suppression token: its comment pattern and the rule codes it suppresses.""" + + token: str + pattern: re.Pattern[str] + codes: frozenset[str] + + # Suppression tokens that must each carry a reason (LIT005). -OK_SUPPRESSIONS: tuple[tuple[str, re.Pattern[str]], ...] = ( - ("mutable-ok", MUTABLE_OK_RE), - ("cast-ok", CAST_OK_RE), - ("guard-ok", GUARD_OK_RE), - ("kwargs-ok", KWARGS_OK_RE), - ("rebind-ok", REBIND_OK_RE), - ("writable-ok", WRITABLE_OK_RE), +OK_SUPPRESSIONS: Final[tuple[_OkToken, ...]] = ( + _OkToken("mutable-ok", MUTABLE_OK_RE, frozenset(("LIT001", "LIT002"))), + _OkToken("cast-ok", CAST_OK_RE, frozenset(("LIT006",))), + _OkToken("guard-ok", GUARD_OK_RE, frozenset(("LIT007",))), + _OkToken("kwargs-ok", KWARGS_OK_RE, frozenset(("LIT008",))), + _OkToken("rebind-ok", REBIND_OK_RE, frozenset(("LIT010", "LIT011"))), + _OkToken("writable-ok", WRITABLE_OK_RE, frozenset(("LIT012",))), ) - - + + class Violation(NamedTuple): path: Path line: int code: str message: str - + def render(self) -> str: return f"{self.path}:{self.line}: {self.code} {self.message}" - - -@dataclass(frozen=True, slots=True) -class Comments: - """The lines carrying each valid `*-ok` suppression.""" - mutable_ok_lines: frozenset[int] - cast_ok_lines: frozenset[int] - guard_ok_lines: frozenset[int] - kwargs_ok_lines: frozenset[int] - rebind_ok_lines: frozenset[int] - writable_ok_lines: frozenset[int] - - + # --------------------------------------------------------------------------- # # Comment scanning (LIT003 / LIT004 / LIT005) # --------------------------------------------------------------------------- # - - + + def _reason_of(rest: str) -> str: return rest.strip().lstrip("#-").strip() - + def _valid_ok(regex: re.Pattern[str], text: str) -> bool: """True iff `text` carries this suppression with a reason of usable length.""" m = regex.search(text) @@ -233,35 +254,40 @@ def _valid_ok(regex: re.Pattern[str], text: str) -> bool: def _comment_violations(path: Path, line_no: int, text: str) -> Iterator[Violation]: """Pure: all LIT003/004/005 findings for one comment.""" - for token, regex in OK_SUPPRESSIONS: - m = regex.search(text) + for ok in OK_SUPPRESSIONS: + m = ok.pattern.search(text) if m and len((m.group("reason") or "").strip()) < MIN_REASON_LEN: - yield Violation(path, line_no, "LIT005", f"{token} requires a reason: `# {token}: `") - + yield Violation(path, line_no, "LIT005", f"{ok.token} requires a reason: `# {ok.token}: `") + m = NOQA_RE.search(text) if m: if not m.group("codes"): yield Violation(path, line_no, "LIT003", "noqa requires rule codes: `# noqa: XXX123 # `") elif len(_reason_of(m.group("rest"))) < MIN_REASON_LEN: yield Violation(path, line_no, "LIT003", "noqa requires a reason: `# noqa: XXX123 # `") - + if TYPE_IGNORE_RE.search(text): - yield Violation(path, line_no, "LIT009", - "`# type: ignore` is inert (enableTypeIgnoreComments is false, so " - "basedpyright never honors it); use `# pyright: ignore[ruleName] # `") + yield Violation( + path, + line_no, + "LIT009", + "`# type: ignore` is inert (enableTypeIgnoreComments is false, so " + "basedpyright never honors it); use `# pyright: ignore[ruleName] # `", + ) m = IGNORE_RE.search(text) if m: codes = m.group("codes") if not codes or codes == "[]": - yield Violation(path, line_no, "LIT004", - "ignore requires codes: `# pyright: ignore[ruleName] # `") + yield Violation(path, line_no, "LIT004", "ignore requires codes: `# pyright: ignore[ruleName] # `") elif len(_reason_of(m.group("rest"))) < MIN_REASON_LEN: - yield Violation(path, line_no, "LIT004", - "ignore requires a reason: `# pyright: ignore[ruleName] # `") - - -def scan_comments(path: Path, source: str) -> tuple[Comments, tuple[Violation, ...]]: + yield Violation( + path, line_no, "LIT004", "ignore requires a reason: `# pyright: ignore[ruleName] # `" + ) + + +def scan_comments(path: Path, source: str) -> tuple[Mapping[str, frozenset[int]], tuple[Violation, ...]]: + """Tokenize comments into (token -> lines with a valid reasoned marker, comment violations).""" try: tokens = tokenize.generate_tokens(io.StringIO(source).readline) comment_toks = tuple((t.start[0], t.string) for t in tokens if t.type == tokenize.COMMENT) @@ -269,27 +295,22 @@ def scan_comments(path: Path, source: str) -> tuple[Comments, tuple[Violation, . # tokenize raises TokenError (EOF mid-construct) or a SyntaxError subclass # (IndentationError / TabError) on malformed source; defer to ast.parse below, # which re-raises and is reported as LIT000 rather than crashing the run. - return Comments(frozenset(), frozenset(), frozenset(), frozenset(), frozenset(), frozenset()), () - - def _lines_with(regex: re.Pattern[str]) -> frozenset[int]: - return frozenset(line for line, text in comment_toks if _valid_ok(regex, text)) + return {ok.token: frozenset() for ok in OK_SUPPRESSIONS}, () return ( - Comments( - mutable_ok_lines=_lines_with(MUTABLE_OK_RE), - cast_ok_lines=_lines_with(CAST_OK_RE), - guard_ok_lines=_lines_with(GUARD_OK_RE), - kwargs_ok_lines=_lines_with(KWARGS_OK_RE), - rebind_ok_lines=_lines_with(REBIND_OK_RE), - writable_ok_lines=_lines_with(WRITABLE_OK_RE), + MappingProxyType( + { + ok.token: frozenset(line for line, text in comment_toks if _valid_ok(ok.pattern, text)) + for ok in OK_SUPPRESSIONS + } ), tuple(v for line, text in comment_toks for v in _comment_violations(path, line, text)), ) - - + + # --------------------------------------------------------------------------- # - - + + def _head_name(node: ast.expr) -> str | None: if isinstance(node, ast.Name): return node.id @@ -332,11 +353,13 @@ def mutable_names_in(annotation: ast.AST) -> Iterator[str]: yield from mutable_names_in(inner) for child in ast.iter_child_nodes(annotation): yield from mutable_names_in(child) - - + + def _mutable_ann(path: Path, line: int, name: str, where: str) -> Violation: return Violation( - path, line, "LIT001", + path, + line, + "LIT001", f"mutable `{name}` in {where}: a mutable collection can be grown or rewritten " f"by whoever holds it. Annotate a read-only view -- Mapping[...], Sequence[...], " f"AbstractSet[...], tuple[X, ...], frozenset[X], or a frozen dataclass / " @@ -345,37 +368,30 @@ def _mutable_ann(path: Path, line: int, name: str, where: str) -> Violation: ) -def _annotation_violations( - path: Path, annotation: ast.expr | None, line: int, where: str, ok_lines: frozenset[int] -) -> Iterator[Violation]: - if annotation is None or line in ok_lines: +def _annotation_violations(path: Path, annotation: ast.expr | None, line: int, where: str) -> Iterator[Violation]: + if annotation is None: return yield from (_mutable_ann(path, line, name, where) for name in mutable_names_in(annotation)) - - -def _function_violations( - path: Path, node: ast.FunctionDef | ast.AsyncFunctionDef, comments: Comments -) -> Iterator[Violation]: - mutable_ok = comments.mutable_ok_lines + + +def _function_violations(path: Path, node: ast.FunctionDef | ast.AsyncFunctionDef) -> Iterator[Violation]: args = node.args for arg in (*args.posonlyargs, *args.args, *args.kwonlyargs): - yield from _annotation_violations( - path, arg.annotation, arg.lineno, f"parameter `{arg.arg}` of `{node.name}`", mutable_ok - ) + yield from _annotation_violations(path, arg.annotation, arg.lineno, f"parameter `{arg.arg}` of `{node.name}`") # *args is allowed when typed (it's just a tuple); ruff ANN002 forces the # annotation, so here we only add the LIT001 mutable-collection check on the element type. if args.vararg is not None: - yield from _annotation_violations( - path, args.vararg.annotation, args.vararg.lineno, f"`*args` of `{node.name}`", mutable_ok - ) + yield from _annotation_violations(path, args.vararg.annotation, args.vararg.lineno, f"`*args` of `{node.name}`") # **kwargs is banned outright (LIT008): it erases the keyword contract and forces # Any-typing on everything it carries. ruff can require it be typed (ANN003) but # cannot ban the syntax, so this rule does. - if args.kwarg is not None and args.kwarg.lineno not in comments.kwargs_ok_lines: + if args.kwarg is not None: yield Violation( - path, args.kwarg.lineno, "LIT008", + path, + args.kwarg.lineno, + "LIT008", f"`**{args.kwarg.arg}` is banned: it erases the keyword contract and forces " f"Any-typing; declare explicit keyword parameters, or accept one frozen payload " f"(frozen dataclass / NamedTuple / ReadOnly TypedDict) " @@ -383,25 +399,20 @@ def _function_violations( ) if node.returns is not None: - yield from _annotation_violations( - path, node.returns, node.returns.lineno, f"return type of `{node.name}`", mutable_ok - ) - - -def iter_annotation_violations(path: Path, tree: ast.AST, comments: Comments) -> Iterator[Violation]: + yield from _annotation_violations(path, node.returns, node.returns.lineno, f"return type of `{node.name}`") + + +def iter_annotation_violations(path: Path, tree: ast.AST) -> Iterator[Violation]: # Every annotation is in scope: signatures (params / *args / return) plus every # `x: T` -- class attribute, local, or module global. The latter three are all # ast.AnnAssign, so one walk covers them; only the signature annotations (which # are not AnnAssign) need the dedicated helper. for node in ast.walk(tree): if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): - yield from _function_violations(path, node, comments) + yield from _function_violations(path, node) elif isinstance(node, ast.AnnAssign): target = node.target.id if isinstance(node.target, ast.Name) else "" - yield from _annotation_violations( - path, node.annotation, node.lineno, - f"the type of `{target}`", comments.mutable_ok_lines, - ) + yield from _annotation_violations(path, node.annotation, node.lineno, f"the type of `{target}`") # --------------------------------------------------------------------------- # @@ -421,18 +432,20 @@ def _is_cast_call(node: ast.Call) -> bool: ) -def iter_cast_violations(path: Path, tree: ast.AST, comments: Comments) -> Iterator[Violation]: +def iter_cast_violations(path: Path, tree: ast.AST) -> Iterator[Violation]: for node in ast.walk(tree): - if isinstance(node, ast.Call) and _is_cast_call(node) and node.lineno not in comments.cast_ok_lines: + if isinstance(node, ast.Call) and _is_cast_call(node): yield Violation( - path, node.lineno, "LIT006", + path, + node.lineno, + "LIT006", "cast() is an unchecked assertion (the type checker takes it on faith); " "validate into a frozen dataclass/NamedTuple/ReadOnly TypedDict at the " "boundary instead (suppress: `# cast-ok: `)", ) -def iter_guard_violations(path: Path, tree: ast.AST, comments: Comments) -> Iterator[Violation]: +def iter_guard_violations(path: Path, tree: ast.AST) -> Iterator[Violation]: # TypeGuard/TypeIs are legal only as a function's return annotation (`-> TypeGuard[int]`), # so the walk is confined to `node.returns`; a runtime name that merely happens to read # `TypeGuard` is not a narrowing predicate. ruff bans the import; this flags the use. @@ -440,20 +453,18 @@ def iter_guard_violations(path: Path, tree: ast.AST, comments: Comments) -> Iter if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) or node.returns is None: continue for sub in ast.walk(node.returns): - name = ( - sub.id if isinstance(sub, ast.Name) - else sub.attr if isinstance(sub, ast.Attribute) - else None - ) - if name in UNSAFE_GUARDS and sub.lineno not in comments.guard_ok_lines: + name = sub.id if isinstance(sub, ast.Name) else sub.attr if isinstance(sub, ast.Attribute) else None + if name in UNSAFE_GUARDS: yield Violation( - path, sub.lineno, "LIT007", + path, + sub.lineno, + "LIT007", f"`{name}` narrowing predicate: the checker never verifies the body, so a " f"wrong guard silently corrupts types; parse into a concrete type instead " f"(suppress: `# guard-ok: `)", ) - - + + # --------------------------------------------------------------------------- # # Mutable-collection construction (LIT002) # --------------------------------------------------------------------------- # @@ -477,11 +488,7 @@ def _annotation_node_ids(tree: ast.AST) -> frozenset[int]: not construction, so the LIT002 walk must skip those subtrees. """ return frozenset( - id(sub) - for node in ast.walk(tree) - for ann in _annotations_of(node) - if ann is not None - for sub in ast.walk(ann) + id(sub) for node in ast.walk(tree) for ann in _annotations_of(node) if ann is not None for sub in ast.walk(ann) ) @@ -536,7 +543,9 @@ def _is_typeddict_annotation(annotation: ast.expr) -> bool: if head in TYPEDDICT_ANNOTATION_WRAPPERS: return _is_typeddict_annotation(annotation.slice) if head == "Annotated": - first = annotation.slice.elts[0] if isinstance(annotation.slice, ast.Tuple) and annotation.slice.elts else None + first = ( + annotation.slice.elts[0] if isinstance(annotation.slice, ast.Tuple) and annotation.slice.elts else None + ) return first is not None and _is_typeddict_annotation(first) return head is not None and head not in NON_TYPEDDICT_HEADS name = _head_name(annotation) @@ -591,7 +600,7 @@ def _construction_kind(node: ast.expr) -> str | None: return None -def iter_construction_violations(path: Path, tree: ast.AST, comments: Comments) -> Iterator[Violation]: +def iter_construction_violations(path: Path, tree: ast.AST) -> Iterator[Violation]: in_annotation = _annotation_node_ids(tree) frozen_arguments = _frozen_argument_ids(tree) typeddict_builds = _typeddict_build_ids(tree) @@ -604,10 +613,12 @@ def iter_construction_violations(path: Path, tree: ast.AST, comments: Comments) ): continue kind = _construction_kind(node) - if kind is None or node.lineno in comments.mutable_ok_lines: + if kind is None: continue yield Violation( - path, node.lineno, "LIT002", + path, + node.lineno, + "LIT002", f"mutable {kind}: this builds a collection that can be grown or rewritten. " f"Build it in one shot and freeze it -- a tuple/frozenset wrapping a generator " f"(`tuple(f(x) for x in xs)`), a tuple literal, a frozen dataclass / NamedTuple, " @@ -615,8 +626,8 @@ def iter_construction_violations(path: Path, tree: ast.AST, comments: Comments) f"really must be dynamic) a MappingProxyType wrapping a dict literal or " f"comprehension (suppress: `# mutable-ok: `)", ) - - + + # --------------------------------------------------------------------------- # # Final-annotation discipline (LIT010) and argument immutability (LIT011) # --------------------------------------------------------------------------- # @@ -747,20 +758,11 @@ def _node_bindings(node: ast.AST, in_loop: bool) -> Iterator[Binding]: case ast.NamedExpr(target=ast.Name(id=name, lineno=line)): yield Binding(name, line, "walrus", in_loop) case ast.Import(names=aliases): - yield from ( - Binding((a.asname or a.name).partition(".")[0], node.lineno, "other", in_loop) - for a in aliases - ) + yield from (Binding((a.asname or a.name).partition(".")[0], node.lineno, "other", in_loop) for a in aliases) case ast.ImportFrom(names=aliases): - yield from ( - Binding(a.asname or a.name, node.lineno, "other", in_loop) - for a in aliases - if a.name != "*" - ) + yield from (Binding(a.asname or a.name, node.lineno, "other", in_loop) for a in aliases if a.name != "*") case ast.Delete(targets=targets): - yield from ( - Binding(t.id, t.lineno, "other", in_loop) for t in targets if isinstance(t, ast.Name) - ) + yield from (Binding(t.id, t.lineno, "other", in_loop) for t in targets if isinstance(t, ast.Name)) case ast.FunctionDef(name=name) | ast.AsyncFunctionDef(name=name) | ast.ClassDef(name=name): yield Binding(name, node.lineno, "other", in_loop) case ast.Global(names=names): @@ -792,9 +794,7 @@ def iter_scopes(tree: ast.AST) -> Iterator[ast.AST]: def _function_params(node: ast.FunctionDef | ast.AsyncFunctionDef | ast.Lambda) -> frozenset[str]: a = node.args - return frozenset( - p.arg for p in (*a.posonlyargs, *a.args, *a.kwonlyargs, a.vararg, a.kwarg) if p is not None - ) + return frozenset(p.arg for p in (*a.posonlyargs, *a.args, *a.kwonlyargs, a.vararg, a.kwarg) if p is not None) def _exempt_final_name(name: str) -> bool: @@ -812,26 +812,24 @@ def _is_config_surface(path: Path) -> bool: return path.parts[-2:] == CONFIG_SURFACE_PARTS -def iter_final_violations(path: Path, tree: ast.AST, comments: Comments) -> Iterator[Violation]: +def iter_final_violations(path: Path, tree: ast.AST) -> Iterator[Violation]: for scope in iter_scopes(tree): if isinstance(scope, ast.Module) and _is_config_surface(path): continue - params = ( - _function_params(scope) - if isinstance(scope, (ast.FunctionDef, ast.AsyncFunctionDef)) - else frozenset() - ) + params = _function_params(scope) if isinstance(scope, (ast.FunctionDef, ast.AsyncFunctionDef)) else frozenset() bindings = scope_bindings(scope) declared = frozenset(b.name for b in bindings if b.form == "declared") first = _first_binding_index(bindings) for i, b in enumerate(bindings): if b.name in declared or b.name in params or b.in_loop: continue - if _exempt_final_name(b.name) or b.line in comments.rebind_ok_lines: + if _exempt_final_name(b.name): continue if b.form in ASSIGN_FORMS: yield Violation( - path, b.line, "LIT010", + path, + b.line, + "LIT010", f"`{b.name}` is assigned without a Final declaration, leaving it open to " f"rebinding: annotate `{b.name}: Final = ...` (or `Final[T]`, or a bare " f"`{b.name}: Final[T]` declaration with a single deferred assignment); " @@ -841,7 +839,9 @@ def iter_final_violations(path: Path, tree: ast.AST, comments: Comments) -> Iter ) elif b.form in IMPLICIT_FINAL_FORMS and i > first[b.name]: yield Violation( - path, b.line, "LIT010", + path, + b.line, + "LIT010", f"`{b.name}` is re-bound here after an earlier binding: unpacking and " f"walrus targets cannot carry Final, so their names are implicitly final; " f"bind a fresh name instead, or suppress with `# rebind-ok: `", @@ -895,9 +895,7 @@ def _iter_param_scopes( def _param_owners( scope: ast.AST, bindings: Sequence[Binding], enclosing: Sequence[_EnclosingFunction] ) -> Mapping[str, str]: - own_name = ( - scope.name if isinstance(scope, (ast.FunctionDef, ast.AsyncFunctionDef)) else "" - ) + own_name = scope.name if isinstance(scope, (ast.FunctionDef, ast.AsyncFunctionDef)) else "" nonlocal_params = { b.name: owner.name for b in bindings @@ -908,7 +906,7 @@ def _param_owners( return {**{p: own_name for p in _function_params(scope)}, **nonlocal_params} -def iter_param_violations(path: Path, tree: ast.AST, comments: Comments) -> Iterator[Violation]: +def iter_param_violations(path: Path, tree: ast.AST) -> Iterator[Violation]: for scope, enclosing in _iter_param_scopes(tree): bindings = scope_bindings(scope) owners = _param_owners(scope, bindings, enclosing) @@ -917,19 +915,21 @@ def iter_param_violations(path: Path, tree: ast.AST, comments: Comments) -> Iter for b in bindings: if b.form in SCOPE_STATEMENT_FORMS or b.name not in owners: continue - if b.line in comments.rebind_ok_lines: - continue yield Violation( - path, b.line, "LIT011", + path, + b.line, + "LIT011", f"parameter `{b.name}` of `{owners[b.name]}` is re-bound: the name silently " f"detaches from what the caller passed; bind a new name instead " f"(suppress: `# rebind-ok: `)", ) for name, line in _mutation_sites(scope): - if name not in owners or name in SELF_PARAMS or line in comments.rebind_ok_lines: + if name not in owners or name in SELF_PARAMS: continue yield Violation( - path, line, "LIT011", + path, + line, + "LIT011", f"parameter `{name}` of `{owners[name]}` is mutated in place: the caller's " f"object is rewritten at a distance; build and return a new value instead " f"(suppress: `# rebind-ok: `)", @@ -1016,16 +1016,18 @@ def _functional_fields(tree: ast.AST) -> Iterator[_Field]: yield _Field(owner, key.value, value, value.lineno) -def iter_typeddict_violations(path: Path, tree: ast.AST, comments: Comments) -> Iterator[Violation]: +def iter_typeddict_violations(path: Path, tree: ast.AST) -> Iterator[Violation]: fields = ( *(f for cls in _typeddict_classes(tree) for f in _class_fields(cls)), *_functional_fields(tree), ) for field in fields: - if _has_readonly_qualifier(field.annotation) or field.line in comments.writable_ok_lines: + if _has_readonly_qualifier(field.annotation): continue yield Violation( - path, field.line, "LIT012", + path, + field.line, + "LIT012", f"TypedDict field `{field.name}` of `{field.owner}` is writable: any holder " f"of the payload can rewrite the key after construction. Qualify it as " f"`ReadOnly[...]` (PEP 705; nests freely with Required/NotRequired/Annotated) " @@ -1033,36 +1035,76 @@ def iter_typeddict_violations(path: Path, tree: ast.AST, comments: Comments) -> ) +# --------------------------------------------------------------------------- # +# Suppression application and unused suppressions (LIT013) +# --------------------------------------------------------------------------- # + + +def apply_suppressions( + path: Path, + raw: Sequence[Violation], + suppressions: Mapping[str, frozenset[int]], +) -> tuple[Violation, ...]: + """Drop raw violations a valid `*-ok` marker suppresses; flag markers that suppress nothing.""" + kept = tuple( + v + for v in raw + if not any( + v.line in suppressions.get(ok.token, frozenset()) and v.code in ok.codes + for ok in OK_SUPPRESSIONS + ) + ) + unused = ( + Violation( + path, + line, + "LIT013", + f"`# {ok.token}` suppresses nothing: no " + f"{'/'.join(sorted(ok.codes))} violation on this line, so delete it", + ) + for ok in OK_SUPPRESSIONS + for line in sorted(suppressions.get(ok.token, frozenset())) + if not any(v.line == line and v.code in ok.codes for v in raw) + ) + return (*kept, *unused) + + # --------------------------------------------------------------------------- # # Driver # --------------------------------------------------------------------------- # - - + + def check_file(path: Path) -> tuple[Violation, ...]: try: source = path.read_text(encoding="utf-8") except (OSError, UnicodeDecodeError) as exc: return (Violation(path, 0, "LIT000", f"could not read file: {exc}"),) - - comments, violations = scan_comments(path, source) - + + suppressions, violations = scan_comments(path, source) + try: tree = ast.parse(source, filename=str(path)) except SyntaxError as exc: return (*violations, Violation(path, exc.lineno or 0, "LIT000", f"syntax error: {exc.msg}")) - + return ( *violations, - *iter_annotation_violations(path, tree, comments), - *iter_cast_violations(path, tree, comments), - *iter_guard_violations(path, tree, comments), - *iter_construction_violations(path, tree, comments), - *iter_final_violations(path, tree, comments), - *iter_param_violations(path, tree, comments), - *iter_typeddict_violations(path, tree, comments), + *apply_suppressions( + path, + ( + *iter_annotation_violations(path, tree), + *iter_cast_violations(path, tree), + *iter_guard_violations(path, tree), + *iter_construction_violations(path, tree), + *iter_final_violations(path, tree), + *iter_param_violations(path, tree), + *iter_typeddict_violations(path, tree), + ), + suppressions, + ), ) - - + + def collect_paths(raw: Iterable[str]) -> Iterator[Path]: for item in raw: p = Path(item) @@ -1070,8 +1112,8 @@ def collect_paths(raw: Iterable[str]) -> Iterator[Path]: yield from sorted(p.rglob("*.py")) elif p.suffix == ".py": yield p - - + + PARALLEL_MIN_PATHS = 200 MAX_WORKERS = 8 @@ -1099,18 +1141,17 @@ def main(argv: Sequence[str]) -> int: if not paths: print("usage: check_type_discipline.py ...", file=sys.stderr) return 2 - + targets = tuple(collect_paths(paths)) violations = sorted(scan_paths(targets)) for v in violations: print(v.render()) - + if violations: print(f"\n{len(violations)} violation(s).", file=sys.stderr) return 1 return 0 - - + + if __name__ == "__main__": raise SystemExit(main(sys.argv[1:])) - \ No newline at end of file diff --git a/scripts/type_discipline_gate.py b/scripts/type_discipline_gate.py index 40e61cf7265..4ba1a2ea393 100644 --- a/scripts/type_discipline_gate.py +++ b/scripts/type_discipline_gate.py @@ -17,7 +17,8 @@ without codes or reason), LIT006 (cast), LIT008 (`**kwargs`), LIT009 (inert LIT012 (TypedDict field without a `ReadOnly[...]` qualifier; suppress with `# writable-ok: `) carry limits at or above their current count to ratchet down; LIT005 (`*-ok` suppression without a reason) is frozen at limit 0 -so any net-new reasonless suppression trips the gate; and LIT007 +so any net-new reasonless suppression trips the gate; LIT013 (`*-ok` suppression +that suppresses nothing) is frozen at 0 for the same reason; and LIT007 (TypeGuard/TypeIs) is a hard zero. LIT010 and LIT011 were seeded at 1.5x the count left after the sweep that annotated every never-rebound name with Final, so that headroom is the hard @@ -129,7 +130,9 @@ def base_counts(ref: str) -> dict: # the body (or the `worktree add` itself) failed. rmtree is already best-effort. subprocess.run( ["git", "worktree", "remove", "--force", str(worktree)], - cwd=REPO_ROOT, capture_output=True, text=True, + cwd=REPO_ROOT, + capture_output=True, + text=True, ) shutil.rmtree(parent, ignore_errors=True) @@ -140,10 +143,7 @@ def over_ceiling(head: dict, budget: dict) -> frozenset: A rule can only breach when it is over its limit, so when none are the base comparison cannot change the verdict and the base worktree scan can be skipped. """ - return frozenset( - rule for rule, spec in budget.items() - if head.get(rule, 0) > spec["limit"] - ) + return frozenset(rule for rule, spec in budget.items() if head.get(rule, 0) > spec["limit"]) def evaluate(head: dict, base: dict, budget: dict) -> list: @@ -187,15 +187,11 @@ def cmd_check(base: str) -> None: return new = introduced( head, - parse_changed_lines( - _run(["git", "diff", base_point, "--unified=0", "--no-color", "--", TARGET]) - ), + parse_changed_lines(_run(["git", "diff", base_point, "--unified=0", "--no-color", "--", TARGET])), ) print(f"FAIL: LIT-rule totals exceed their limit (base {base}):") for breach in breaches: - print( - f" {breach.rule}: total {breach.total} over limit {breach.cap} (this change added {breach.added})" - ) + print(f" {breach.rule}: total {breach.total} over limit {breach.cap} (this change added {breach.added})") for violation in sorted(v for v in new if v.code == breach.rule): print(f" {violation.file}:{violation.line}") print( @@ -221,7 +217,8 @@ def ratcheted_budget(budget: dict, current: dict, base: dict, seeded: frozenset """ return { rule: { - "limit": spec["limit"] if rule in seeded + "limit": spec["limit"] + if rule in seeded else max(0, spec["limit"] - max(0, base.get(rule, 0) - current.get(rule, 0))) } for rule, spec in sorted(budget.items()) @@ -231,7 +228,9 @@ def ratcheted_budget(budget: dict, current: dict, base: dict, seeded: frozenset def _base_budget_rules(base_point: str) -> frozenset: proc = subprocess.run( ["git", "show", f"{base_point}:{BUDGET_PATH.name}"], - cwd=REPO_ROOT, capture_output=True, text=True, + cwd=REPO_ROOT, + capture_output=True, + text=True, ) if proc.returncode != 0: return frozenset() @@ -248,17 +247,12 @@ def cmd_update(base_ref: str) -> None: budget = json.loads(BUDGET_PATH.read_text()) base_point = resolve_base_point(base_ref) seeded = frozenset(budget) - _base_budget_rules(base_point) - updated = ratcheted_budget( - budget, count_by_rule(head_violations()), base_counts(base_point), seeded - ) + updated = ratcheted_budget(budget, count_by_rule(head_violations()), base_counts(base_point), seeded) BUDGET_PATH.write_text(json.dumps(updated, indent=2, sort_keys=True) + "\n") cleared = sum(budget[rule]["limit"] - updated[rule]["limit"] for rule in updated) print(f"Ratcheted LIT-rule limits down by {cleared} violations this branch fixed") if seeded: - print( - "Left untouched (seeded on this branch, absent from the base budget): " - + ", ".join(sorted(seeded)) - ) + print("Left untouched (seeded on this branch, absent from the base budget): " + ", ".join(sorted(seeded))) def main() -> None: diff --git a/tests/e2e/lifecycle.py b/tests/e2e/lifecycle.py index eb9704d4dcb..1ccf1bdbefa 100644 --- a/tests/e2e/lifecycle.py +++ b/tests/e2e/lifecycle.py @@ -54,9 +54,7 @@ class ResourceManager: client: ResourceClient strict_cleanup: bool = False - _cleanups: List[Callable[[], object]] = field( - default_factory=list - ) # mutable-ok: append-only teardown registry + _cleanups: List[Callable[[], object]] = field(default_factory=list) def init(self) -> None: """No global setup needed today; present for lifecycle symmetry.""" @@ -85,8 +83,7 @@ class ResourceManager: def teardown(self) -> None: failures: Final = tuple( - failure for cleanup in reversed(self._cleanups) - if (failure := _run_cleanup(cleanup)) is not None + failure for cleanup in reversed(self._cleanups) if (failure := _run_cleanup(cleanup)) is not None ) if failures and self.strict_cleanup: raise ExceptionGroup("Resource cleanup failed", failures) diff --git a/tests/e2e/load/proxy_usage.py b/tests/e2e/load/proxy_usage.py index 83463c078b8..b4e28478cdf 100644 --- a/tests/e2e/load/proxy_usage.py +++ b/tests/e2e/load/proxy_usage.py @@ -160,5 +160,5 @@ class ProxyUsageSampler: """ with self._lock: taken = tuple(self._samples) - self._samples = [taken[-1]] if taken else [] # rebind-ok: drains the buffer under the lock + self._samples = [taken[-1]] if taken else [] return UsageWindow(samples=taken) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 9c321269e38..4986f5ddcc0 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -51,7 +51,6 @@ def _owned(nodeid: str) -> bool: def pytest_collection_modifyitems(config: pytest.Config, items: list[pytest.Item]) -> None: order_seed: Final = config.getoption("integration_order_seed") if order_seed: - # rebind-ok: pytest requires this hook to reorder its shared collection list in place. items.sort(key=lambda item: hashlib.sha256(f"{order_seed}:{item.nodeid}".encode()).digest()) root: Final = Path(__file__).parent owned: Final = tuple( diff --git a/tests/test_litellm/proxy/db/test_exception_handler_reconnect_retry.py b/tests/test_litellm/proxy/db/test_exception_handler_reconnect_retry.py index 4286da23242..26ab6798d8e 100644 --- a/tests/test_litellm/proxy/db/test_exception_handler_reconnect_retry.py +++ b/tests/test_litellm/proxy/db/test_exception_handler_reconnect_retry.py @@ -27,9 +27,7 @@ def _make_client( `call_with_db_reconnect_retry` actually pokes at.""" client = MagicMock() if has_attempt_db_reconnect: - client.attempt_db_reconnect = AsyncMock( - return_value=attempt_db_reconnect_return - ) + client.attempt_db_reconnect = AsyncMock(return_value=attempt_db_reconnect_return) else: # `hasattr(client, "attempt_db_reconnect")` must return False — MagicMock # auto-creates attributes, so we wipe it out via `spec`. @@ -127,9 +125,7 @@ async def test_call_with_db_reconnect_retry_propagates_after_second_transport_er raise httpx.ReadError("still failing") with pytest.raises(httpx.ReadError): - await call_with_db_reconnect_retry( - client, _factory, reason="second_transport_error" - ) + await call_with_db_reconnect_retry(client, _factory, reason="second_transport_error") assert len(invocations) == 2 client.attempt_db_reconnect.assert_awaited_once() @@ -166,9 +162,7 @@ async def test_call_with_db_reconnect_retry_invokes_factory_twice_not_same_coro( raise httpx.ReadError("transport blip") return "ok" - result = await call_with_db_reconnect_retry( - client, _factory, reason="fresh_coro_on_retry" - ) + result = await call_with_db_reconnect_retry(client, _factory, reason="fresh_coro_on_retry") assert result == "ok" assert factory_call_count == 2 @@ -243,14 +237,13 @@ async def test_call_with_db_reconnect_retry_preserves_original_error_when_reconn raise original_exc with pytest.raises(httpx.ReadError) as exc_info: - await call_with_db_reconnect_retry( - client, _factory, reason="reconnect_itself_raises" - ) + await call_with_db_reconnect_retry(client, _factory, reason="reconnect_itself_raises") assert exc_info.value is original_exc assert exc_info.value.__cause__ is reconnect_exc client.attempt_db_reconnect.assert_awaited_once() + @pytest.mark.asyncio async def test_call_with_db_reconnect_retry_honors_narrowed_retry_safe_types(): """A non-idempotent write can pass `retry_safe_error_types` to opt out of @@ -259,7 +252,7 @@ async def test_call_with_db_reconnect_retry_honors_narrowed_retry_safe_types(): attempts = 0 async def _factory(): - nonlocal attempts # rebind-ok: attempt counter for a two-call helper + nonlocal attempts attempts += 1 raise httpx.ReadError("ambiguous") @@ -283,7 +276,7 @@ async def test_call_with_db_reconnect_retry_default_covers_every_transport_error attempts = 0 async def _factory(): - nonlocal attempts # rebind-ok: attempt counter for a two-call helper + nonlocal attempts attempts += 1 if attempts == 1: raise ClientNotConnectedError() diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_conduct.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_conduct.py index 323756f8fa0..a7c777248c0 100644 --- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_conduct.py +++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_conduct.py @@ -221,14 +221,10 @@ def test_missing_package_fails_at_config_load_with_install_hint() -> None: def test_plugin_that_swallows_unreachable_fallback_into_kwargs_is_rejected() -> None: class Swallowing: - def __init__( - self, *, fail_mode: str = "fail_closed", **kwargs: object - ) -> None: ... # kwargs-ok: models plugin 0.2.4 + def __init__(self, *, fail_mode: str = "fail_closed", **kwargs: object) -> None: ... class Binding: - def __init__( - self, *, unreachable_fallback: str | None = None, **kwargs: object - ) -> None: ... # kwargs-ok: plugin 0.2.5 + def __init__(self, *, unreachable_fallback: str | None = None, **kwargs: object) -> None: ... assert not binds_unreachable_fallback(Swallowing) assert binds_unreachable_fallback(Binding) diff --git a/tests/test_litellm/test_check_type_discipline.py b/tests/test_litellm/test_check_type_discipline.py index 2d49332e687..b0c8d2d5d56 100644 --- a/tests/test_litellm/test_check_type_discipline.py +++ b/tests/test_litellm/test_check_type_discipline.py @@ -40,17 +40,17 @@ def test_scan_comments_tokenizes_every_comment(): # was tokenized, and the valid cast-ok suppression line must be captured. A crash in the # readline path would leave both empty. source = "x = 1 # noqa\ny = 2 # cast-ok: validated upstream by the caller\n" - comments, violations = checker.scan_comments(Path("snippet.py"), source) + suppressions, violations = checker.scan_comments(Path("snippet.py"), source) assert [v.code for v in violations] == ["LIT003"] - assert comments.cast_ok_lines == frozenset({2}) + assert suppressions["cast-ok"] == frozenset({2}) def test_scan_comments_does_not_crash_on_malformed_source(): # A dedent mismatch makes tokenize raise IndentationError (a SyntaxError subclass); # scan_comments must swallow it, not propagate and crash the whole run. - comments, violations = checker.scan_comments(Path("x.py"), "if True:\n a = 1\n b = 2\n") + suppressions, violations = checker.scan_comments(Path("x.py"), "if True:\n a = 1\n b = 2\n") assert violations == () - assert comments.cast_ok_lines == frozenset() + assert suppressions["cast-ok"] == frozenset() def test_malformed_source_degrades_to_lit000(tmp_path): @@ -107,6 +107,38 @@ def test_ok_suppression_without_reason_is_flagged(tmp_path): assert "LIT002" in codes # and it does not suppress, so the construction still trips +def test_mutable_ok_on_a_real_violation_suppresses_and_is_not_lit013(tmp_path): + codes = _codes(tmp_path, "x: Final = [] # mutable-ok: seed\n") + assert "LIT002" not in codes + assert "LIT013" not in codes + + +def test_mutable_ok_on_a_clean_line_is_lit013(tmp_path): + f = tmp_path / "snippet.py" + f.write_text("x: Final = (1, 2) # mutable-ok: stale\n", encoding="utf-8") + found = checker.check_file(f) + assert [v.code for v in found] == ["LIT013"] + assert "mutable-ok" in found[0].message + + +def test_mutable_ok_does_not_suppress_rebind_codes(tmp_path): + codes = _codes(tmp_path, "x = 1 # mutable-ok: wrong token\n") + assert "LIT010" in codes + assert "LIT013" in codes + + +def test_rebind_ok_on_a_real_param_rebind_is_not_lit013(tmp_path): + codes = _codes(tmp_path, "def f(p: int) -> None:\n p = 2 # rebind-ok: reset\n") + assert "LIT011" not in codes + assert "LIT013" not in codes + + +def test_reasonless_ok_on_a_clean_line_is_lit005_not_lit013(tmp_path): + codes = _codes(tmp_path, "x: Final = (1, 2) # mutable-ok\n") + assert "LIT005" in codes + assert "LIT013" not in codes + + # --------------------------------------------------------------------------- # # Mutable annotations (LIT001) and construction (LIT002) # --------------------------------------------------------------------------- # @@ -213,15 +245,11 @@ def test_typeddict_annotated_dict_literal_is_exempt(tmp_path): def test_wrapped_typeddict_annotations_share_the_exemption(tmp_path): - assert "LIT002" not in _codes( - tmp_path, "from typing import Final, Optional\nx: Final[Optional[MyTD]] = {'a': 1}\n" - ) + assert "LIT002" not in _codes(tmp_path, "from typing import Final, Optional\nx: Final[Optional[MyTD]] = {'a': 1}\n") assert "LIT002" not in _codes( tmp_path, "from typing import Annotated, Final\nx: Final[Annotated[MyTD, 'meta']] = {'a': 1}\n" ) - assert "LIT002" not in _codes( - tmp_path, "from typing import ClassVar\nclass C:\n x: ClassVar[MyTD] = {'a': 1}\n" - ) + assert "LIT002" not in _codes(tmp_path, "from typing import ClassVar\nclass C:\n x: ClassVar[MyTD] = {'a': 1}\n") assert "LIT002" not in _codes(tmp_path, "from typing import Final\nx: Final[MyTD | None] = {'a': 1}\n") assert "LIT002" in _codes(tmp_path, "from typing import Final\nx: Final[dict[str, int] | None] = {'a': 1}\n") @@ -234,7 +262,8 @@ def test_bare_final_dict_literal_still_counts(tmp_path): def test_non_typeddict_annotations_do_not_exempt(tmp_path): assert "LIT002" in _codes(tmp_path, "from typing import Final\nx: Final[dict[str, int]] = {'a': 1}\n") assert "LIT002" in _codes( - tmp_path, "from collections.abc import Mapping\nfrom typing import Final\nx: Final[Mapping[str, int]] = {'a': 1}\n" + tmp_path, + "from collections.abc import Mapping\nfrom typing import Final\nx: Final[Mapping[str, int]] = {'a': 1}\n", ) assert "LIT002" in _codes(tmp_path, "from typing import Any, Final\nx: Final[Any] = {'a': 1}\n") assert "LIT002" in _codes(tmp_path, "from typing import Final\nx: Final[object] = {'a': 1}\n") @@ -372,10 +401,7 @@ def test_walrus_rebinding_is_flagged(tmp_path): def test_unpack_after_global_declaration_is_flagged(tmp_path): src = ( - "count = 0 # rebind-ok: seeded module counter\n" - "def f() -> None:\n" - " global count\n" - " count, other = (1, 2)\n" + "count = 0 # rebind-ok: seeded module counter\ndef f() -> None:\n global count\n count, other = (1, 2)\n" ) assert _codes(tmp_path, src).count("LIT010") == 1 @@ -411,14 +437,7 @@ def test_non_assignment_binding_forms_are_exempt(tmp_path): def test_dunder_underscore_class_body_and_type_alias_are_exempt(tmp_path): - src = ( - "from typing import TypeAlias\n" - "__all__ = ['C']\n" - "_ = 1\n" - "Alias: TypeAlias = str\n" - "class C:\n" - " field = 1\n" - ) + src = "from typing import TypeAlias\n__all__ = ['C']\n_ = 1\nAlias: TypeAlias = str\nclass C:\n field = 1\n" assert "LIT010" not in _codes(tmp_path, src) @@ -428,12 +447,7 @@ def test_comprehension_targets_are_exempt(tmp_path): def test_global_reassignment_inside_function_is_flagged(tmp_path): - src = ( - "count = 0 # rebind-ok: seeded module counter\n" - "def bump() -> None:\n" - " global count\n" - " count = 1\n" - ) + src = "count = 0 # rebind-ok: seeded module counter\ndef bump() -> None:\n global count\n count = 1\n" assert _codes(tmp_path, src).count("LIT010") == 1 @@ -585,11 +599,7 @@ def test_walrus_in_own_defaults_binds_in_enclosing_scope_not_the_parameter(tmp_p def test_walrus_in_nested_defaults_rebinds_the_enclosing_parameter(tmp_path): - src = ( - "def g(p: int) -> None:\n" - " def inner(q: int = (p := 2)) -> None:\n" - " return None\n" - ) + src = "def g(p: int) -> None:\n def inner(q: int = (p := 2)) -> None:\n return None\n" assert "LIT011" in _codes(tmp_path, src) @@ -604,11 +614,7 @@ def test_typeddict_writable_field_is_flagged(tmp_path): def test_typeddict_readonly_field_is_clean(tmp_path): - src = ( - "from typing_extensions import ReadOnly, TypedDict\n" - "class P(TypedDict):\n" - " a: ReadOnly[int]\n" - ) + src = "from typing_extensions import ReadOnly, TypedDict\nclass P(TypedDict):\n a: ReadOnly[int]\n" assert "LIT012" not in _codes(tmp_path, src) @@ -640,11 +646,7 @@ def test_readonly_in_annotated_metadata_position_does_not_qualify(tmp_path): def test_typeddict_subclass_in_same_module_is_flagged(tmp_path): src = ( - "from typing import TypedDict\n" - "class Base(TypedDict):\n" - " pass\n" - "class Child(Base, total=False):\n" - " a: int\n" + "from typing import TypedDict\nclass Base(TypedDict):\n pass\nclass Child(Base, total=False):\n a: int\n" ) assert "LIT012" in _codes(tmp_path, src) @@ -677,11 +679,7 @@ def test_writable_ok_with_reason_suppresses_lit012(tmp_path): def test_writable_ok_without_reason_is_lit005_and_does_not_suppress(tmp_path): - src = ( - "from typing import TypedDict\n" - "class P(TypedDict):\n" - " a: int # writable-ok\n" - ) + src = "from typing import TypedDict\nclass P(TypedDict):\n a: int # writable-ok\n" codes = _codes(tmp_path, src) assert "LIT005" in codes assert "LIT012" in codes @@ -717,7 +715,9 @@ def _corpus(tmp_path: Path, count: int) -> tuple[Path, ...]: def _run_checker(target: Path) -> list[str]: completed = subprocess.run( [sys.executable, str(_MODULE_PATH), str(target)], - capture_output=True, text=True, timeout=300, + capture_output=True, + text=True, + timeout=300, ) return completed.stdout.splitlines() @@ -727,9 +727,7 @@ def test_worker_count_stays_serial_below_the_threshold(): def test_worker_count_fans_out_at_the_threshold(): - assert checker._worker_count(checker.PARALLEL_MIN_PATHS) == max( - 1, min(os.cpu_count() or 1, checker.MAX_WORKERS) - ) + assert checker._worker_count(checker.PARALLEL_MIN_PATHS) == max(1, min(os.cpu_count() or 1, checker.MAX_WORKERS)) def test_worker_count_never_exceeds_the_cap(): diff --git a/tests/test_litellm_rust/support/isolation.py b/tests/test_litellm_rust/support/isolation.py index f98ce4843a8..26c7cd0f875 100644 --- a/tests/test_litellm_rust/support/isolation.py +++ b/tests/test_litellm_rust/support/isolation.py @@ -29,7 +29,7 @@ def _list_attribute(container: ModuleType, attribute: str) -> list[object]: def _isolated_list(container: ModuleType, attribute: str) -> Generator[None]: source: Final = _list_attribute(container, attribute) original: Final = list(source) - source.clear() # mutable-ok: test isolation mutates global registries by design + source.clear() try: yield finally: @@ -54,5 +54,5 @@ def isolated_callback_registries() -> Generator[None]: for attribute in CALLBACK_ATTRIBUTES: stack.enter_context(_isolated_list(litellm, attribute)) stack.enter_context(_isolated_list(litellm_logging, "_in_memory_loggers")) # pyright: ignore[reportPrivateUsage] # no public callback-cache accessor - stack.enter_context(rebound(utils, "callback_list", [])) # rebind-ok: isolate legacy callback registry + stack.enter_context(rebound(utils, "callback_list", [])) yield diff --git a/tests/unit/messages/test_dispatch.py b/tests/unit/messages/test_dispatch.py index 48eb1adbf51..88ef849f0e2 100644 --- a/tests/unit/messages/test_dispatch.py +++ b/tests/unit/messages/test_dispatch.py @@ -216,9 +216,7 @@ def test_binding_errors_delegate_to_python(args: tuple[object, ...], kwargs: Map captured: Final[list[tuple[tuple[object, ...], Mapping[str, object]]]] = [] expected: Final = response() - def python( - *call_args: object, **call_kwargs: object - ) -> AnthropicMessagesResponse: # kwargs-ok: records invalid call + def python(*call_args: object, **call_kwargs: object) -> AnthropicMessagesResponse: captured.append((call_args, call_kwargs)) return expected diff --git a/type-discipline-budget.json b/type-discipline-budget.json index 0c0952289e2..beeb44474da 100644 --- a/type-discipline-budget.json +++ b/type-discipline-budget.json @@ -34,5 +34,8 @@ }, "LIT012": { "limit": 4486 + }, + "LIT013": { + "limit": 0 } }