Skip to content

Score Python route handlers nested in a factory as their own entries - #30

Open
davearlin wants to merge 1 commit into
unclebob:masterfrom
davearlin:python-route-handlers
Open

davearlin wants to merge 1 commit into
unclebob:masterfrom
davearlin:python-route-handlers

Conversation

@davearlin

Copy link
Copy Markdown

Why

FastAPI and Flask apps often register their handlers inside a factory:

def build_router(config):
    router = APIRouter()

    @router.get("/users/{user_id}")
    async def user(user_id: str):
        ...

    @router.post("/users")
    async def add_user(body: NewUser):
        ...

    return router

A nested function stays inside the function that encloses it, so every handler's decisions are charged to build_router. The factory reports a large CC, and the handlers, where the logic and the missing tests are, have no entries. The TypeScript scanner already handles the same shape for inline Express callbacks.

What

  • A function nested in another function and decorated with a route call (.get, .post, .put, .patch, .delete, .head, .options, .trace, .websocket, .route or .api_route) is its own entry in the module's namespace, as an Express callback is.
  • Its name is the route: GET /users/{user_id}. .route and .api_route take their verbs from methods=[...] (GET|POST /sync) and default to GET. A repeated label is GET /users#2.
  • Its decisions are not also charged to the factory. The decorator's arguments still are, because the factory evaluates them.
  • A string path must be empty or start with /, so @mock.patch("pkg.mod.attr") is not a route. An f-string keeps its fields (GET {PREFIX}/items), and a name keeps its source (DELETE PATHS.user).
  • Top-level handlers and methods keep their function names, so uml-viewer joins do not change.
  • The #2 naming moves from typescript.py to treesitter.claim, so both scanners share one copy.

Not included

The factory's coverage range still includes the handler bodies, as it does for a function that encloses Express callbacks.

Verified

  • test_route_handlers_in_a_factory_are_their_own_entries and test_top_level_handlers_keep_their_names_and_lookalikes_stay_nested fail on master and pass here.
  • crapper on its own source: every new function scores CRAP 6 or less, and languages/python.py is at 99% line coverage. The one missed line (ident is None) predates this change.
  • Linux (python:3.12-slim with git and zsh): 147 passed, 1 failed, the bb test that fails on master too. Windows, Python 3.12: 144 passed, with the same 4 Unix-only failures as master.

This touches the same README paragraph, module docstring and test file as #29. Whichever lands second needs a small rebase, and I will do it.

FastAPI and Flask apps often register handlers inside a factory, so
every handler's decisions were charged to the factory and the handlers
had no entries. A nested function decorated with a route call (.get,
.post, .put, .patch, .delete, .head, .options, .trace, .websocket,
.route, .api_route) is now its own entry, named like an Express route
(`GET /users`, `GET|POST /sync`, `GET /users#2`), in the module's
namespace. Its decisions are not also charged to the factory; the
decorator's arguments still are. A string path must be empty or start
with `/`, so `@mock.patch("a.b")` is not a route. Top-level handlers and
methods keep their names. The `#2` naming moves to treesitter.claim so
both scanners share it.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant