You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adding requires('scope') to a function that has a request param that is not of type Request currently throws an AssertionError:
File ".venv/lib/python3.14/site-packages/anyio/_backends/_asyncio.py", line 1002, in run
result = context.run(func, *args)
File ".venv/lib/python3.14/site-packages/starlette/authentication.py", line 79, in sync_wrapper
assert isinstance(request, Request)
~~~~~~~~~~^^^^^^^^^^^^^^^^^^
AssertionError
Which isn't much of a problem specifically in starlette since all functions that handle post requests will do so from accessing request.json(). However, in fastapi, body parameters are pydantic models that can be any name and type, including request.
This change is to add a message to the assert to better communicate what the issue might be for the case where the request parameter is not of type Request:
File ".venv/lib/python3.14/site-packages/anyio/_backends/_asyncio.py", line 1002, in run
result = context.run(func, *args)
File ".venv/lib/python3.14/site-packages/starlette/authentication.py", line 84, in sync_wrapper
assert isinstance(request, Request), (
~~~~~~~~~~^^^^^^^^^^^^^^^^^^
AssertionError: Parameter with name 'request' is required to be of type 'Request' not 'ExampleRequest'
We should probably use raise TypeError instead, and have tests here, but this seems impossible (?) using pure Starlette.
Agreed, I don't think its possible with pure Starlette and rather a consequence of how FastAPI handles params. I did look into if there's a way to improve that handling inside of FastAPI and somehow inject the Request but unfortunately since @requires is a decorator its difficult to get the object when its not a param to the function without changing the API for both the requires functionality and how FastAPI (or any other lib built on top of starlette) resolves / injects. I'll have a think on this and open a discussion if I can think of a better way.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adding
requires('scope')to a function that has arequestparam that is not of typeRequestcurrently throws anAssertionError:Which isn't much of a problem specifically in
starlettesince all functions that handlepostrequests will do so from accessingrequest.json(). However, infastapi, body parameters are pydantic models that can be any name and type, includingrequest.This change is to add a message to the assert to better communicate what the issue might be for the case where the request parameter is not of type
Request:MRE:
Changing
request: ExampleRequesttobody: ExampleRequestand adding arequest: Requestto the function params above works fine.Checklist
I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.non functional changeI've updated the documentation accordingly.