Stop `pyright` solving a `TypeVar` in the deprecated `fallback_model` helper

Merged
AD
3mo ago
+29-30

Fixes the Main CI duration regression alert that fired on 2026-09-07 (median run on main went from ~325s to ~513s).

What caused the regression

#8077 added a tiny helper, resolve_fallback_subagent_model, to map the deprecated fallback_model argument onto fallback_subagent_model. It typed both arguments and its return with a single bare TypeVar. Its two callers, ImageGeneration.__init__ and XSearch.__init__, pass it two arguments of the large Model | KnownModelName | str | Callable[...] | None union, and pyright takes roughly 100 seconds per call site to solve that type variable.

That's the whole regression. Nothing else changed: same runner image, same pyright 1.1.411, same command. Every test job, coverage, mypy and every other pre-commit hook stayed flat.

before #8077after #8077
pyright check phase (local)36s360s
typecheck pre-commit hook in CI76s330–490s
quality checks job in CI~220s~500s
whole CI run on main~310s~550s

The quality checks job is the last thing the final check gate waits on, so its growth lands 1:1 on the total.

Two notes for context:

  • The slowdown was already visible on #8077's own PR run on 2026-09-05 (pre-commit step: 472s vs 124s on main that day), so a per-hook time budget on the typecheck hook would have caught it before merge.
  • #8075 measured its --threads numbers on the evening of 2026-09-06, on top of this regression, so its 475–636s figures reflect #8077 rather than the threads flag.

The fix

The helper is now check_deprecated_fallback_model. It takes only whether each argument was passed (two booleans) and decides whether to warn or raise; it never sees the values. Each caller keeps the value with a plain fallback_model if fallback_model is not None else fallback_subagent_model, so the attribute assignment is typed directly from the __init__ parameters and there is nothing for pyright to solve.

Runtime behavior is unchanged: the deprecation warning, the both-arguments UserError, and the stacklevel attribution all work as before.

Verified locally on this branch: full pyright back to 35s with 0 errors, the four test files #8077 touched pass (392 tests), pre-commit clean.

I bisected the mechanism before settling on this: typing the helper with Any also fixes it (0.1s), removing the KnownModelName literals from the callers does not (94s), and removing the Callable member only partly (63s). So it's the type-variable solve against the union, not the size of the literal union.

Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
  • PR title is fit for the release changelog.

šŸ¤– Generated with Claude Code

https://claude.ai/code/session_013KYQXPNPDYPczakELNju21