Skip to content

Fix path param warning for request handlers#1393

Open
ahmadmahmoody wants to merge 2 commits into
sparckles:mainfrom
ahmadmahmoody:am-fix-path-param-warning
Open

Fix path param warning for request handlers#1393
ahmadmahmoody wants to merge 2 commits into
sparckles:mainfrom
ahmadmahmoody:am-fix-path-param-warning

Conversation

@ahmadmahmoody
Copy link
Copy Markdown

@ahmadmahmoody ahmadmahmoody commented May 29, 2026

Description

This PR fixes #1336.

Summary

This PR updates the path-param warning heuristic so it respects Robyn's supported request access patterns. Routes with path params no longer warn when the handler can access those params through Request, PathParams, reserved request aliases, or direct path-param arguments.

The warning still fires when a route declares path params and the handler has no supported way to access them.

Tests cover direct param access, request aliases, typed Request, path_params, typed PathParams, multi-param request access, and the real inaccessible-param warning case.

PR Checklist

Please ensure that:

  • The PR contains a descriptive title
  • The PR contains a descriptive summary of the changes
  • You build and test your changes before submitting a PR.
  • You have added relevant documentation (not needed; this fixes warning behavior for an existing API)
  • You have added relevant tests. We prefer integration tests wherever possible

Pre-Commit Instructions:

Validation run locally:

  • python -m pytest unit_tests
  • pre-commit run --all-files
  • git diff --check

Summary by CodeRabbit

  • Bug Fixes

    • Improved router's detection of whether handlers can access declared path parameters through various access methods, reducing false warnings about unused parameters.
  • Tests

    • Added comprehensive test suite validating router warning behavior for path parameters across different handler configurations and access patterns.

Review Change Stack

@vercel
Copy link
Copy Markdown

vercel Bot commented May 29, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
robyn Ready Ready Preview, Comment May 29, 2026 1:23am

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 29, 2026

📝 Walkthrough

Walkthrough

This PR reduces false-positive path parameter warnings by detecting when handlers can access route parameters through type annotations or reserved parameter names like request or path_params, rather than requiring explicit function arguments matching parameter names.

Changes

Path Parameter Warning Suppression

Layer / File(s) Summary
Path parameter access detection mechanism
robyn/router.py
Adds reserved request/path parameter name constants, a tuple of annotation types (Request, PathParams) that indicate path-param access, and a _handler_can_access_path_params() helper that inspects handler annotations and parameter names to determine if a handler can indirectly access declared path parameters. Also updates imports to include Mapping from collections.abc.
Route registration warning suppression
robyn/router.py
Modifies add_route() to conditionally compute unused_route_params only when the handler is not able to access path parameters via annotations, suppressing the "declared but unused" warning for handlers that can access params indirectly.
Comprehensive test coverage
unit_tests/test_router_path_param_warnings.py
New test module with route-registration and log-filtering helpers, eight handler variants exercising different parameter patterns (bare names, typed annotations, combinations), and three parametrized test cases: verifying warnings are skipped for handlers with Request or PathParams annotations, verifying warnings are skipped when typed request plus a named parameter can access remaining params, and verifying warnings are still logged when handlers truly do not use the route's declared path params.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • sansyrox

Poem

🐰 A handler once cried with dismay,
"Why warn me of params I use every day?
Through request.path_params I've peeked,
Yet warnings won't cease till I've tweaked!
But now, with annotations so bright,
The router knows—no false alarms in sight!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: fixing the path parameter warning logic for request handlers, which is the primary objective of this PR.
Description check ✅ Passed The description covers all key sections: it references the linked issue (#1336), provides a clear summary of the fix, explains the warning behavior, mentions tests added, and confirms validation steps were completed.
Linked Issues check ✅ Passed The code changes directly address issue #1336 by introducing path-param access detection that respects Request, PathParams, reserved aliases, and direct arguments, eliminating false-positive warnings as required.
Out of Scope Changes check ✅ Passed All changes are focused on fixing the path-param warning heuristic and adding comprehensive tests; no unrelated modifications were introduced.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@ahmadmahmoody ahmadmahmoody marked this pull request as ready for review May 29, 2026 01:29
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
robyn/router.py (1)

78-83: 💤 Low value

Make path-param access detection robust to postponed (stringified) annotations

_handler_can_access_path_params checks param.annotation in _PATH_PARAM_ACCESS_TYPES (i.e., Request/PathParams). With from __future__ import annotations (PEP 563), inspect.Parameter.annotation is typically the string "Request"/"PathParams", so this check fails and the logic falls back to name-based detection—meaning the “Route … declares path params … but handler … doesn't use them” warning can reappear for handlers that rely on type annotations but don’t use the reserved parameter names. This matches the existing limitation in wrapped_handler’s identity-based checks (e.g., handler_param_type is Request around line 214). Resolve annotations first (e.g., via typing.get_type_hints(handler)) before comparing to Request/PathParams if you want it to be robust.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@robyn/router.py` around lines 78 - 83, _handler_can_access_path_params
currently checks param.annotation against _PATH_PARAM_ACCESS_TYPES but fails for
postponed (string) annotations; change its signature to accept the handler
callable (or obtain the handler where it's called) and use
typing.get_type_hints(handler) to resolve annotations before comparing types to
_PATH_PARAM_ACCESS_TYPES, then fall back to the existing name-based check (using
_REQUEST_PARAM_NAMES | _PATH_PARAMS_PARAM_NAMES) if resolution fails; also catch
and ignore TypeError/NameError from get_type_hints so the function degrades
gracefully and keep behavior consistent with wrapped_handler's identity checks
(e.g., comparisons to Request/PathParams).
unit_tests/test_router_path_param_warnings.py (1)

88-103: ⚡ Quick win

Solid negative case; consider locking the partial-coverage boundary.

The tests cover full suppression and full-miss, but not the in-between: a handler that names one declared param yet has no Request/PathParams access to the remaining one (e.g., def handler(user_id): ... on /users/:user_id/posts/:post_id). Adding that case would pin down whether the warning fires for the uncovered post_id, preventing future regressions in the detection heuristic.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@unit_tests/test_router_path_param_warnings.py` around lines 88 - 103, Add a
new unit test that covers the partial-coverage case: create a handler that
accepts only one of the declared path params (e.g., def handler(user_id): return
"ok"), register it with add_get_route(Router(),
"/users/:user_id/posts/:post_id", handler), capture warnings via caplog (set
WARNING for "robyn.router") and assert a single warning is emitted that
references the route and specifically mentions the uncovered "post_id" (and does
not claim "user_id" is unused), using the same helpers
path_param_warning_messages and naming the test like
test_path_param_warning_when_one_param_unused to locate the behavior in the test
suite.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@robyn/router.py`:
- Around line 78-83: _handler_can_access_path_params currently checks
param.annotation against _PATH_PARAM_ACCESS_TYPES but fails for postponed
(string) annotations; change its signature to accept the handler callable (or
obtain the handler where it's called) and use typing.get_type_hints(handler) to
resolve annotations before comparing types to _PATH_PARAM_ACCESS_TYPES, then
fall back to the existing name-based check (using _REQUEST_PARAM_NAMES |
_PATH_PARAMS_PARAM_NAMES) if resolution fails; also catch and ignore
TypeError/NameError from get_type_hints so the function degrades gracefully and
keep behavior consistent with wrapped_handler's identity checks (e.g.,
comparisons to Request/PathParams).

In `@unit_tests/test_router_path_param_warnings.py`:
- Around line 88-103: Add a new unit test that covers the partial-coverage case:
create a handler that accepts only one of the declared path params (e.g., def
handler(user_id): return "ok"), register it with add_get_route(Router(),
"/users/:user_id/posts/:post_id", handler), capture warnings via caplog (set
WARNING for "robyn.router") and assert a single warning is emitted that
references the route and specifically mentions the uncovered "post_id" (and does
not claim "user_id" is unused), using the same helpers
path_param_warning_messages and naming the test like
test_path_param_warning_when_one_param_unused to locate the behavior in the test
suite.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c1667911-04c8-4d42-8566-717632ebb6f1

📥 Commits

Reviewing files that changed from the base of the PR and between 25d4844 and b09adbf.

📒 Files selected for processing (2)
  • robyn/router.py
  • unit_tests/test_router_path_param_warnings.py

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.

Path params warning fires even when params are accessed via request.path_params

1 participant