refactor(auto-router compression): resolve the policy without a loop-local rebind

The marker walk rebound a loop-local on each iteration, which is the mutation the
repository's convention exists to discourage, but a `: Final` cannot express that
inside a loop body: basedpyright rejects it outright with 'A Final variable cannot
be assigned within a loop'. A lazy generator binds the name once per item and never
rebinds it, so the first marker carrying a policy still wins and the rest are never
read.
This commit is contained in:
moe-berri 2026-09-05 11:19:39 -07:00
parent ff942c3a74
commit 723bc2140f

View file

@ -117,11 +117,11 @@ def policy_for_model(
# request does not carry describes a different slice of traffic, so falling back
# to it would apply, say, an "eu" policy to a "us" request purely on config order.
untagged: Final = tuple(params for params in markers if not params.get("tags"))
for params in (*tag_matched, *untagged):
policy = policy_from_litellm_params(params)
if policy is not None:
return policy
return None
# Lazily, so the first marker carrying a policy still wins and the rest are never
# read. A generator rather than a loop-local: the name is bound once per item and
# never rebound, which `: Final` cannot express inside a loop body.
candidates: Final = (policy_from_litellm_params(params) for params in (*tag_matched, *untagged))
return next((policy for policy in candidates if policy is not None), None)
def team_id_from_request(request_kwargs: Mapping[str, object]) -> str | None: