Skip to content

fix: do not suggest FromQuery for path parameter defaults - #5019

Open
Sharawey74 wants to merge 1 commit into
litestar-org:mainfrom
Sharawey74:fix/parameter-default-error-message
Open

fix: do not suggest FromQuery for path parameter defaults#5019
Sharawey74 wants to merge 1 commit into
litestar-org:mainfrom
Sharawey74:fix/parameter-default-error-message

Conversation

@Sharawey74

Copy link
Copy Markdown

Closes #5018

Description

raise_for_kwarg_as_default maps ParameterKwarg.param_type to the replacement syntax it recommends. For a plain Parameter() that mapping is unreliable: param_type defaults to ParamType.QUERY and only changes when header=, cookie= or query= is passed, so a path parameter is told to use FromQuery.

This adds a check for whether param_type was actually chosen, and only uses the alias mapping when it was. Otherwise the message falls back to Annotated[<type>, Parameter(...)], which is valid for every parameter kind.

param_type counts as chosen when either the default is one of the dedicated subclasses (QueryParameter, PathParameter, HeaderParameter, CookieParameter), which set it themselves, or one of header= / cookie= / query= was passed.

The route path is not usable at this point. FieldDefinition.from_annotation inspects one parameter in isolation, and raise_for_kwarg_as_default is also called from DTO field generation where no route exists at all.

Side effect worth noting: the ParamType.PATH: "FromPath" entry was previously unreachable, since nothing could set param_type to PATH on a value reaching this function. It becomes reachable through PathParameter().

Tests

Three tests added to tests/unit/test_typing.py:

  • test_kwarg_definition_as_default_without_explicit_source_suggests_parameter — covers the reported bug, and fails without the fix.
  • test_kwarg_definition_as_default_suggests_dedicated_alias — the four dedicated subclasses still name their alias, including FromPath.
  • test_kwarg_definition_as_default_with_explicit_source_suggests_dedicated_alias — the deprecated header= / cookie= / query= kwargs still name their alias.

608 tests pass across test_typing, test_kwargs, test_signature and test_openapi. ruff, mypy, pyright and slotscheck are clean.

@Sharawey74
Sharawey74 marked this pull request as ready for review August 26, 2026 16:00
@Sharawey74
Sharawey74 requested review from a team as code owners August 26, 2026 16:00
Copilot AI lite review requested due to automatic review settings August 26, 2026 16:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@codecov

codecov Bot commented Aug 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 67.19%. Comparing base (cbc0c51) to head (80c15c9).

Files with missing lines Patch % Lines
litestar/utils/errors.py 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5019      +/-   ##
==========================================
- Coverage   67.19%   67.19%   -0.01%     
==========================================
  Files         293      293              
  Lines       15363    15365       +2     
  Branches     1745     1745              
==========================================
+ Hits        10323    10324       +1     
- Misses       4890     4891       +1     
  Partials      150      150              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Sharawey74

Copy link
Copy Markdown
Author

The two failing checks here are unrelated to this change:

All six required checks pass, and the test matrix is green on 3.11 through 3.14.

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.

Bug: Error message suggests FromQuery for path parameter defaults

2 participants