Does FastAPI's own code still pass the type checker it adopted?

2026-12-02来源 dev.toEnglish

Astral's ty went 1.0 this week, and it was all over my feeds: the front page of Hacker News, near the top of Lobsters, the usual round of "fastest Python type checker" posts. I'd read the pitch before — a Rust type checker built to replace mypy and Pyright, fast enough to run on every keystroke — but I'd never pointed it at real code. FastAPI seemed like the obvious target, because the 1.0 announcement names it as an adopter, and I had a clone sitting on disk already.

So I cloned FastAPI, installed its actual runtime dependencies into a clean virtualenv, and ran ty check fastapi/ the same way FastAPI's own scripts/lint.sh does. Then I ran mypy fastapi/ with the project's own pyproject.toml settings, which set strict = true. Same 54 files, same machine, three runs each.

The obvious number: speed

Tool Fastest of 3 runs Range
ty 1.0.0 (cold, no cache) 0.21s 0.21–0.25s
mypy 2.4.1 (cold, no cache) 3.42s 3.42–3.49s

That's roughly 16 times faster on this codebase, on this sandbox's 4-core VM (nproc says 4), not Astral's M3 Max. I also measured peak memory with resource.getrusage (no /usr/bin/time on this box): ty's child process peaked around 116MB resident, mypy's around 220MB.

Part of the gap is parallelism, not just language. I compared CPU time against wall time for each tool's run:

ty: wall=0.224s cpu_time=0.654s ratio=2.93 (out of 4 cores available) mypy: wall=3.408s cpu_time=3.536s ratio=1.04 

mypy barely uses a second core. ty used closer to three of the four available. So some of that 16x is ty spreading the same work across cores mypy leaves idle, on top of whatever Rust buys it over mypy's compiled-but-still-Python-shaped architecture. I didn't isolate those two factors from each other — I can't force ty onto a single core from its CLI — so I'm reporting both facts rather than claiming an exact split between them.

The incrementality claim, tested two ways

Astral's pitch leans hard on "incrementality": fast live updates in an editor, not just a fast one-off run. I tried this two ways.

First, the boring way: run ty check three times in a row with its on-disk cache intact, no file changes. No meaningful difference from a cold run — still around 0.2s. At this project size, a fresh CLI invocation pays enough fixed cost that disk-cache reuse barely registers.

Second, the way the blog post actually means it: ty check fastapi/ --watch, which keeps a process running and rechecks on file changes, the way a language server would. I appended a blank line to fastapi/applications.py — the file fastapi/__init__.py imports directly to expose the FastAPI class itself — and timed how long until the watcher printed its next "Found N diagnostics":

Mode Latency
Fresh CLI invocation (cold) ~210–250ms
Watch mode, after editing a core file 32–38ms across 3 trials

That's the real incrementality story, and it held up: roughly 5.5 to 6x faster to re-surface the same 17 diagnostics once ty is already running as a long-lived process, depending which cold run you compare against. It's a meaningfully different claim than "a fresh run is fast," and it only shows up if you actually run it the way an editor would.

The number I didn't expect

Here's the one that changed what I was going to write about. FastAPI's uv.lock pins ty==0.0.78, a beta from early September. The 1.0 release I'd just installed is a different thing entirely. So I built a second virtualenv with 0.0.78 and ran the identical check, same dependencies, same files:

ty version Diagnostics found
0.0.78 (what FastAPI's CI actually runs) 1
1.0.0 (released this week) 17

The single diagnostic in both cases is the same unresolved import (fastapi_cli.cli, a companion package I hadn't installed). Everything else is new: 3 incompatible-mutable-override errors on real code, 4 warnings about a renamed rule in existing ignore comments, 4 deprecated-overload warnings (three on @asynccontextmanager, one on @contextmanager), and 5 redundant-condition warnings. None of this is in FastAPI's lockfile-pinned CI today, so none of it is a live problem for them. But it means the gap between "pinned in CI" and "what just shipped" isn't cosmetic. The three override errors are genuine: a subclass narrows an inherited field's declared type (scheme: str narrowed to scheme: Literal["bearer"], for instance), which is unsound for a mutable attribute, because code holding a reference typed as the base class could still assign a plain str into a field the subclass promises is always "bearer". mypy's strict mode, run against the same code with the same config, doesn't flag any of them.

I don't know whether that's because 0.0.78 didn't implement this particular check yet, or because something else about the rule's behaviour changed between September and 1.0 — I didn't go through ty's changelog to find out, so I can't attribute the cause more precisely than that. What I can say is that the two outputs, on identical code, in an identical environment, differ by sixteen diagnostics. If you're pinning ty the way FastAPI pins it, with a lower-bound version specifier and a lockfile that happens to resolve to a beta, bumping that pin here was not the no-op I expected going from "beta FastAPI tested against" to "first stable release." I only checked this one tool, so I can't say how typical that is of beta-to-1.0 jumps generally.

Checking one specific claim from the announcement

The ty 1.0 post includes a worked example: a TypedDict with a field named seconds, and a sort key that typos it as runner["second"]. The claim is that ty both catches the typo and accepts the function once it's fixed. I typed the example in myself and ran it past ty, mypy --strict, and Pyright 1.1.414, which happened to already be on this machine.

ty caught the typo and, after I fixed it, reported "All checks passed!" exactly as described. Pyright also caught it — "second" is not a defined key in "Runner" — which surprised me, since the post frames this as something only ty manages among the eight tools Astral tested. Where Pyright differs is after the fix: it still refused the corrected function, because the merged list's inferred type (Runner | dict[str, Unknown]) isn't assignable to the declared list[Runner] return type, since list is invariant. mypy never engaged with the actual bug at all — it reported the same three unrelated argument and return-type errors on both the broken and the fixed version, which told me it wasn't reasoning about the TypedDict key at that point, just about the merged list's general shape.

So the claim holds, but more narrowly than a first read suggests: it's not that other tools miss the typo, it's that ty is the only one of the three I tried that both catches it and then gets out of the way once it's fixed.

What I got wrong on the way

The first time I ran ty check fastapi/, it reported 35 diagnostics, and for about ten minutes I was fairly pleased with myself — FastAPI's own strict mypy config versus ty, 35 to nothing. Then I actually read the output instead of counting lines. Nineteen of those 35 were import-not-found, for things FastAPI only imports behind optional feature flags: opentelemetry, annotated_doc, the separate fastapi_cli package. I simply hadn't installed any of them. Fixing that dropped the count to 17, and that's where the comparison that actually mattered — 0.0.78 against 1.0.0 — became visible. Before I installed those packages, I was one bad environment away from publishing a number that measured nothing but my own setup.

Run it yourself

git clone --depth 1 https://github.com/fastapi/fastapi.git python3 -m venv venv && source venv/bin/activate pip install ty mypy starlette pydantic pydantic-extra-types \ typing-extensions typing-inspection python-multipart email-validator \ httpx opentelemetry-sdk opentelemetry-api annotated-doc cd fastapi ty check fastapi/ --python ../venv # cold run mypy fastapi/ --cache-dir=/dev/null # uses pyproject.toml's strict=true ty check fastapi/ --python ../venv --watch # edit a file in another terminal, watch it recheck pip install ty==0.0.78 --force-reinstall --no-deps ty check fastapi/ --python ../venv # compare against FastAPI's actual pinned version 

Where I land

I'll probably switch to ty on whatever I start next, mostly for that 35ms editor feedback. I never got dmypy, mypy's own daemon mode, running here, so I genuinely don't know if it would have closed that gap — take the comparison for what it's worth, not more. The bigger change is smaller than a tool switch: I stop reading "FastAPI uses ty" as a fact about the ty I'm installing. FastAPI uses ty 0.0.78, specifically, pinned in a lockfile, and confusing that with "whatever shipped this week" is what sent me down this whole path in the first place. Lockfile before badge, from now on.

原文:https://www.a8q9.cn/en/article/does-fastapi-s-own-code-still-pass-the-type-checker-it-adopted-6e7b0a.html

相关文章