openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

Explore using ty instead of mypy for the FastAPI code

Open RayBB opened this issue 1 month ago • 4 comments

Feature Request

Problem / Opportunity

We're about to migrate a lot of pages to FastAPI, so fast feedback from our tools matters more than usual. ty is a type checker and language server from Astral (the makers of Ruff and uv). It's much faster than mypy and gives quick feedback in the editor.

The FastAPI code is already in good shape for ty. With ty 0.0.87:

Folder Files ty diagnostics
openlibrary/fastapi/ + openlibrary/asgi_app.py 30 ~12
openlibrary/tests/fastapi/ 43 21

Timings for the FastAPI code, empty cache (mypy as configured in pre-commit):

mypy ty
FastAPI code only ~3.7s ~0.1s
Whole repo ~5.0s ~0.5s

A test run already turned up one real bug, a 500 in /authors/merge.json, fixed in #13932. Most of the remaining diagnostics are noise or small cleanups.

This is a low-priority exploration: find out what it would take to run ty in pre-commit for the FastAPI folders, and stop running mypy on those same folders.

This is best suited to someone who has already contributed to the codebase. Much of the work is deciding which diagnostics are real problems and which can be ignored, and that takes familiarity with the code.

Success: a clear answer on whether we should switch, and if so, a PR that does it with ty passing on those folders.

Proposal

  1. Add a ty hook to pre-commit that only runs on openlibrary/fastapi/, openlibrary/tests/fastapi/ and openlibrary/asgi_app.py. Pin the ty version, since ty is still in beta and its diagnostics can change between releases.
  2. Exclude those same paths from the mypy hook.
  3. Go through the current diagnostics and decide for each one: fix it, ignore it, or configure it away.

What's known so far:

  • ty doesn't honor mypy's coded ignores like # type: ignore[attr-defined]. Only plain # type: ignore or # ty: ignore[...] work. Several current diagnostics are on lines we already silence for mypy.
  • infogami.config attributes are set at runtime, which causes most of the unresolved-attribute errors. A partial stub (typings/infogami-stubs/config.pyi with a module __getattr__, plus py.typed containing partial) clears all of them without changing infogami. Tested locally.
  • ty needs the project's dependencies installed to resolve imports. The hook needs an environment with FastAPI, Pydantic, etc., like the mypy hook's additional_dependencies.
  • Small fixes found so far:
    • @computed_field should be followed by @property in search.py. This also removes a mypy ignore.
    • SearchResponse.facet_counts should be typed | None.
    • _stream_events should be typed AsyncGenerator.
    • The tests need some assert x is not None lines.

Breakdown

Implementation Details (for maintainers)

Related files

Refer to this map of common Endpoints:

  • .pre-commit-config.yaml: add the ty hook, exclude the FastAPI paths from the mypy hook
  • pyproject.toml: [tool.ty] config (environment, extra paths, rule settings)
  • openlibrary/fastapi/, openlibrary/tests/fastapi/, openlibrary/asgi_app.py
  • openlibrary/plugins/worksearch/code.py: SearchResponse
  • openlibrary/fastapi/status.py: _stream_events

Requirements Checklist

Checklist of requirements that need to be satisfied in order for this issue to be closed:

  • [ ] Write up the current ty diagnostics for the FastAPI folders: which are real issues, which are noise, and how each should be handled
  • [ ] Decide how to handle ignore comments shared between mypy and ty
  • [ ] Add a pinned ty pre-commit hook scoped to the FastAPI folders
  • [ ] Exclude the same folders from the mypy hook
  • [ ] ty passes on those folders in pre-commit and pre-commit.ci
  • [ ] Document how to run ty locally and how to use it as an editor language server

Stakeholders

  • @RayBB

Instructions for Contributors

  • Before creating a new branch or pushing up changes to a PR, please first run these commands to ensure your repository is up to date, as the pre-commit bot may add commits to your PRs upstream.
  • This issue is meant for contributors who have already had PRs merged here. It needs judgment calls about types, ignore comments and tooling config, so please share your findings in a comment before opening a large PR.

RayBB avatar Oct 15 '26 01:10 RayBB

Hi @RayBB can i work on this

Bhaskar-Guthula-137 avatar Oct 15 '26 03:10 Bhaskar-Guthula-137

This issue is meant for contributors who have already had PRs merged here. Please follow our contributing guide to see appropriate issues.

RayBB avatar Oct 15 '26 18:10 RayBB

Hi @RayBB, can I take this one?

Prabh0608 avatar Oct 15 '26 23:10 Prabh0608

Yes go for it!

RayBB avatar Oct 16 '26 01:10 RayBB