From c7ca8f4dcf88610838d1e4005e76f4f93c332350 Mon Sep 17 00:00:00 2001 From: Yucheng Zhu Date: Wed, 19 Aug 2026 18:16:03 -0700 Subject: [PATCH] refactor(ptu): drop a Final rebind and two redundant isinstance guards The basedpyright budget rejected reassigning a Final in the datetime coercion and two isinstance calls the router entry's own type already guarantees. Filtering the built records rather than the raw entries removes both guards and leaves _router_deployment as the single validator. --- litellm/litellm_core_utils/ptu_pricing.py | 19 +++++++++++-------- .../spend_tracking/ptu_flat_cost_rollup.py | 13 ++++++------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/litellm/litellm_core_utils/ptu_pricing.py b/litellm/litellm_core_utils/ptu_pricing.py index fa14d43ea24..a1f8bb36e27 100644 --- a/litellm/litellm_core_utils/ptu_pricing.py +++ b/litellm/litellm_core_utils/ptu_pricing.py @@ -62,18 +62,21 @@ class PTUTerms: effective_to: datetime | None +def _to_utc(parsed: datetime) -> datetime: + """``parsed`` as UTC, reading a naive value as UTC rather than local time.""" + return parsed.replace(tzinfo=timezone.utc) if parsed.tzinfo is None else parsed.astimezone(timezone.utc) + + def _as_utc(value: object) -> datetime | None: """A model_info datetime as UTC, parsing an ISO string, else None.""" if isinstance(value, datetime): - parsed: Final = value - elif isinstance(value, str): - try: - parsed = datetime.fromisoformat(value.replace("Z", "+00:00")) # rebind-ok: one parsed value, two sources - except ValueError: - return None - else: + return _to_utc(value) + if not isinstance(value, str): + return None + try: + return _to_utc(datetime.fromisoformat(value.replace("Z", "+00:00"))) + except ValueError: return None - return parsed.replace(tzinfo=timezone.utc) if parsed.tzinfo is None else parsed.astimezone(timezone.utc) def ptu_terms(model_info: Mapping[str, object]) -> PTUTerms | None: diff --git a/litellm/proxy/spend_tracking/ptu_flat_cost_rollup.py b/litellm/proxy/spend_tracking/ptu_flat_cost_rollup.py index eb5ac72de89..381641be96d 100644 --- a/litellm/proxy/spend_tracking/ptu_flat_cost_rollup.py +++ b/litellm/proxy/spend_tracking/ptu_flat_cost_rollup.py @@ -346,15 +346,14 @@ def _config_deployments(router: object | None, *, owned_by_db: frozenset[str]) - a fresh id, so pricing it would bill one reservation once per distinct client key. """ entries: Final = tuple(getattr(router, "model_list", None) or ()) + records: Final = tuple(_router_deployment(entry) for entry in entries) return tuple( record - for entry in entries - if isinstance(entry, Mapping) - and isinstance(entry.get("model_info"), Mapping) - and entry["model_info"].get("db_model") is not True - and entry["model_info"].get("original_model_id") is None - for record in (_router_deployment(entry),) - if record is not None and record.model_id not in owned_by_db + for record in records + if record is not None + and record.model_info.get("db_model") is not True + and record.model_info.get("original_model_id") is None + and record.model_id not in owned_by_db )