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.
This commit is contained in:
Yucheng Zhu 2026-08-19 18:16:03 -07:00
parent 96080bdd24
commit c7ca8f4dcf
2 changed files with 17 additions and 15 deletions

View file

@ -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:

View file

@ -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
)