fix autocomplete endpoints in web framework #2224
Summary
Part of #2224.
This adds LSP support for Python web framework endpoints. Pyrefly now recognizes decorator-based route definitions, offers endpoint path completions inside HTTP client calls, and lets go-to-definition jump from a client URL literal to the route handler.
What changed
-
Added endpoint discovery
- Scans Python ASTs for route decorators such as
@app.get(...),@app.post(...),@app.route(...),@app.api_route(...), and@app.websocket(...). - Extracts the route path, HTTP method, handler name range, and docstring range.
- Supports explicit methods from
methods=[...]/method=.... - Defaults
route/api_routetoGETwhen no method argument is present.
- Scans Python ASTs for route decorators such as
-
Added endpoint completions for client URL literals.
- In calls like
client.get(""), Pyrefly suggests known matching endpoint paths. - Completion results are filtered by HTTP method, so
client.get(...)andclient.post(...)surface the right handlers. - Insert text handles both inside-string and outside-string completion positions.
- In calls like
-
Added go-to-definition for endpoint URL literals.
- From a call like
client.get("/users"), go-to-definition jumps to the matching route handler. - Path matching understands route parameters, including FastAPI-style
{id}and Flask-style<id>segments.
- From a call like
-
Added guardrails to avoid false positives.
- Endpoint completions/definitions only trigger on recognized HTTP client receivers, such as variables constructed from
TestClient(...),httpx.Client(...),httpx.AsyncClient(...), orrequests.Session(...). - This avoids treating unrelated calls like
dict.get("key")or arbitrary custom.get(...)methods as endpoint requests.
- Endpoint completions/definitions only trigger on recognized HTTP client receivers, such as variables constructed from
-
Added a lightweight performance filter.
- Before loading and walking a module AST for endpoint discovery, Pyrefly first checks whether the source text contains route-decorator-shaped strings such as
.get(,.route(, or.api_route(. - This keeps the first implementation simple while avoiding a full AST scan for modules that clearly cannot define endpoints.
- Before loading and walking a module AST for endpoint discovery, Pyrefly first checks whether the source text contains route-decorator-shaped strings such as
Tests
Added LSP tests covering:
- Endpoint completions from
TestClientcalls. - Endpoint go-to-definition from URL literals.
- Method-specific endpoint matching.
- Path parameter matching.
- Ignoring non-HTTP
.get(...)calls such asdict.get(...).
Verified locally with:
cargo buildcargo test -p pyrefly endpoint_completion_from_client -- --nocapturecargo test -p pyrefly endpoint_completion_ignores_non_http_get_calls -- --nocapturecargo test -p pyrefly endpoint_definition_from_client_literal -- --nocapturecargo test -p pyrefly endpoint_definition_ignores_non_http_get_calls -- --nocapture
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅
i'm sorry - it's hard to figure out what this PR does from the linked issue. would you be able to explain it in the PR with examples?
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅
Hi @asukaminato0721 the PR apparently has failing tests could you address them. Also could you explain what the different changes in this PR do somewhere as it is quite a big change, and we would like to have a brief understanding before diving in.
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅