Merge pull request #39518 from BerriAI/litellm_techdebt_20260903

refactor: clear fresh tech debt from the last 24 hours (2026-09-03, 2026-09-04)
This commit is contained in:
Mateo Wang 2026-09-04 20:56:42 -07:00 committed by GitHub
commit 9c05c158cb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 31 additions and 74 deletions

View file

@ -1,6 +1,6 @@
{
"reportAny": {
"limit": 14074
"limit": 14072
},
"reportArgumentType": {
"limit": 2206
@ -24,7 +24,7 @@
"limit": 19
},
"reportExplicitAny": {
"limit": 4124
"limit": 4121
},
"reportFunctionMemberAccess": {
"limit": 7
@ -108,7 +108,7 @@
"limit": 38309
},
"reportUnknownParameterType": {
"limit": 19621
"limit": 19620
},
"reportUnknownVariableType": {
"limit": 29844

View file

@ -257,7 +257,7 @@ class DualCache(BaseCache):
self,
current_time: float,
keys: list[str],
result: Sequence[Any],
result: Sequence[object],
) -> tuple[list[str], dict[str, float | None]]:
"""
Atomically choose keys to fetch from Redis and reserve their access time.

View file

@ -3821,7 +3821,7 @@ class Logging(LiteLLMLoggingBaseClass):
def record_streamed_anthropic_message_id(self, message_id: str) -> None:
self.streamed_anthropic_message_id = message_id
def _anthropic_messages_logged_response(self, result: Any) -> ModelResponse:
def _anthropic_messages_logged_response(self, result: object) -> ModelResponse:
"""
The ModelResponse a /v1/messages spend_logs row is built from.

View file

@ -1095,10 +1095,7 @@ class ModelArmorGuardrail(CustomGuardrail, VertexBase):
add_guardrail_to_applied_guardrails_header,
)
# Collect all chunks
all_chunks: Final[list[Any]] = []
async for chunk in response:
all_chunks.append(chunk)
all_chunks: Final[Sequence[object]] = tuple([chunk async for chunk in response])
if not all_chunks or self._is_terminal_error_stream(all_chunks):
for chunk in all_chunks:

View file

@ -32,9 +32,6 @@ class AdmissionControlSettings:
queue_timeout_seconds: float
AdmissionControlSettingsGetter: TypeAlias = Callable[[], AdmissionControlSettings | None] # mutable-ok: Callable params
@dataclass(frozen=True, slots=True)
class AdmissionControlStats:
admitted: int
@ -66,13 +63,10 @@ class AdmissionControlMetrics:
rejected_counter: _Counter
AdmissionControlMetricsFactory: TypeAlias = Callable[[], AdmissionControlMetrics | None] # mutable-ok: Callable params
class AdmissionControlState:
"""Per-process admission counters and the in-flight semaphore shared by one worker's requests."""
def __init__(self, metrics_factory: AdmissionControlMetricsFactory) -> None:
def __init__(self, metrics_factory: Callable[[], AdmissionControlMetrics | None]) -> None:
self._metrics_factory = metrics_factory
self._metrics: AdmissionControlMetrics | None = None
self._metrics_init_attempted = False
@ -140,7 +134,7 @@ class AdmissionControlMiddleware:
def __init__(
self,
app: ASGIApp,
get_settings: AdmissionControlSettingsGetter,
get_settings: Callable[[], AdmissionControlSettings | None],
state: AdmissionControlState,
) -> None:
self.app = app

View file

@ -3365,23 +3365,24 @@ async def view_spend_logs(
)
sql_query, params = summary_sql_and_params
rows: Final[Sequence[_SpendDailySummaryRow]] = await _query_raw(prisma_client, sql_query, *params)
if len(rows) == 0:
return [] # pyright: ignore[reportUnknownVariableType] # empty summary has no element type
summary_items: Final = tuple(
_daily_summary_item(date.fromisoformat(day), tuple(day_rows))
for day, day_rows in groupby(rows, key=lambda row: row["day"])
)
final_date: Final = date.fromisoformat(rows[-1]["day"])
final_date: Final = date.fromisoformat(rows[-1]["day"]) if len(rows) > 0 else None
end_date_date: Final = end_date_obj.date()
padding: Final[tuple[Mapping[str, object], ...]] = tuple(
{
"startTime": final_date + timedelta(days=offset),
"spend": 0,
"users": {},
"models": {},
}
for offset in range(1, (end_date_date - final_date).days + 1)
padding: Final[tuple[Mapping[str, object], ...]] = (
()
if final_date is None
else tuple(
{
"startTime": final_date + timedelta(days=offset),
"spend": 0,
"users": {},
"models": {},
}
for offset in range(1, (end_date_date - final_date).days + 1)
)
)
return [*summary_items, *padding]

View file

@ -496,10 +496,9 @@ class BaseResponsesAPIStreamingIterator:
if logging_response is self.completed_response:
return
target: Final[object] = getattr(logging_response, "response", None)
existing_hidden: Final[object] = getattr(target, "_hidden_params", None)
if not isinstance(existing_hidden, Mapping):
if not isinstance(target, ResponsesAPIResponse):
return
existing: Final[Mapping[str, object]] = existing_hidden
existing: Final[Mapping[str, object]] = target._hidden_params
source_hidden: Final[object] = getattr(
getattr(self.completed_response, "response", None), "_hidden_params", None
)
@ -510,15 +509,11 @@ class BaseResponsesAPIStreamingIterator:
raw_headers: Final[Mapping[str, object]] = raw if isinstance(raw, Mapping) else EMPTY_MAPPING
# rebuild by value and let existing keys win: sharing the source dicts would alias what the proxy
# splats into the client's HTTP headers, and copying non-header keys would carry response_cost
setattr( # noqa: B010 # target is typed object here, so a plain attribute store does not type check
target,
"_hidden_params",
{ # mutable-ok: the cost calculator writes optional_params into _hidden_params
"additional_headers": {**headers}, # mutable-ok: fresh copy, logging callbacks may mutate it
"headers": {**raw_headers}, # mutable-ok: fresh copy, logging callbacks may mutate it
**existing,
},
)
target._hidden_params = { # mutable-ok: the cost calculator writes optional_params into _hidden_params
"additional_headers": {**headers}, # mutable-ok: fresh copy, logging callbacks may mutate it
"headers": {**raw_headers}, # mutable-ok: fresh copy, logging callbacks may mutate it
**existing,
}
def _handle_logging_completed_response(self):
"""Base implementation - should be overridden by subclasses"""

View file

@ -116,28 +116,6 @@ async def aattempt(
return RustHandled(adapt(value))
def call(operation: Callable[[], ResultT], context: BridgeErrorContext) -> ResultT:
exceptions: Final = native_exception_types()
if exceptions is None:
return operation()
upstream: Final = exceptions[1]
try:
return operation()
except upstream as error:
_raise_upstream(error, context)
async def acall(operation: Callable[[], Awaitable[ResultT]], context: BridgeErrorContext) -> ResultT:
exceptions: Final = native_exception_types()
if exceptions is None:
return await operation()
upstream: Final = exceptions[1]
try:
return await operation()
except upstream as error:
_raise_upstream(error, context)
def _decline_reason(error: BaseException) -> str:
reason: Final[object] = error.args[0] if error.args else str(error)
return reason if isinstance(reason, str) else str(reason)
@ -170,11 +148,3 @@ def _raise_upstream(error: BaseException, context: BridgeErrorContext) -> NoRetu
llm_provider=context.provider,
model=context.model,
) from error
def identity(value: ResultT) -> ResultT:
return value
async def async_none() -> None:
return None

View file

@ -78,7 +78,7 @@
"limit": 1
},
"C901": {
"limit": 311
"limit": 306
},
"D419": {
"limit": 6

View file

@ -1,9 +1,9 @@
{
"LIT001": {
"limit": 22326
"limit": 22325
},
"LIT002": {
"limit": 26748
"limit": 26746
},
"LIT003": {
"limit": 261