pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

fix autocomplete endpoints in web framework #2224

Open asukaminato0721 opened this issue 9 months ago • 8 comments

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_route to GET when no method argument is present.
  • 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(...) and client.post(...) surface the right handlers.
    • Insert text handles both inside-string and outside-string completion positions.
  • 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.
  • 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(...), or requests.Session(...).
    • This avoids treating unrelated calls like dict.get("key") or arbitrary custom .get(...) methods as endpoint requests.
  • 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.

Tests

Added LSP tests covering:

  • Endpoint completions from TestClient calls.
  • Endpoint go-to-definition from URL literals.
  • Method-specific endpoint matching.
  • Path parameter matching.
  • Ignoring non-HTTP .get(...) calls such as dict.get(...).

Verified locally with:

  • cargo build
  • cargo test -p pyrefly endpoint_completion_from_client -- --nocapture
  • cargo test -p pyrefly endpoint_completion_ignores_non_http_get_calls -- --nocapture
  • cargo test -p pyrefly endpoint_definition_from_client_literal -- --nocapture
  • cargo test -p pyrefly endpoint_definition_ignores_non_http_get_calls -- --nocapture

asukaminato0721 avatar Feb 04 '26 06:02 asukaminato0721

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Feb 04 '26 06:02 github-actions[bot]

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Mar 22 '26 01:03 github-actions[bot]

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Mar 25 '26 01:03 github-actions[bot]

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?

kinto0 avatar Mar 25 '26 16:03 kinto0

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Mar 26 '26 01:03 github-actions[bot]

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.

NathanTempest avatar Apr 29 '26 17:04 NathanTempest

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Apr 30 '26 04:04 github-actions[bot]

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar May 20 '26 06:05 github-actions[bot]