New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using PEP 649 annotations with status code 204 No Content
and return type None
raises AssertionError
#9424
Comments
According to the provided link in the issue #9424, you're returning a string in the body " " @app.post("/", status_code=http.HTTPStatus.NO_CONTENT)
async def root() -> str:
logging.info("endpoint called.")
return "" Even if this was edit the code to @app.post("/", status_code=http.HTTPStatus.NO_CONTENT)
async def root() -> None:
logging.info("endpoint called.")
return None The error is thrown because you are still returning something in body as json though it may not be valid @app.post("/", status_code=http.HTTPStatus.NO_CONTENT)
async def root() -> None:
logging.info("endpoint called.") The server does not seem to complain one bit , maybe there's a special case or more information should be provided to resolve this issue . |
I already submitted a PR and it was approved (#9425). You can try the test there (with and without the change). The test fails on my machine as expected. I deleted the repo (it wasn't up-to-date and I don't think it adds anything). The code in the issue already tells the whole story. |
Oh ok I think this issue should be closed then . |
just like NoReturn will have same error. |
Did you test this with my PR? I can add a test later and see if that's also fixed. No idea when or if this will be merged though. |
@hofrob firstly, thanks for #9425 it will be ease for dev experience but ideally that was an indeed an intended behavior. The In your case, you are trying to return a 204 No Content response, which is used to indicate that the request was successful, but there is no content to return. However, you are also using the To fix this error, you need to remove the @app.get("/", status_code = 204)
async def root():
logging.info('endpoint called.') This code will define a route that returns a 204 No Content response without a response body. @Munalula-Sikazwe The server does not have a problem probably because of not using the from __future__ import annotations Maybe it's time for this issue to be closed? |
This is incorrect.
So the conclusion should be:
Please reconsider. |
I don't disagree but in the this scenario, allowing the |
Well, let's see what the FastAPI devs think. |
Yeah, that's cool this way we have a guideline further because you know this is quite controversial and I follow what my team or the project requires me to personally. |
I think None should be a valid return type hint for 204 endpoints. I also think that anything different than None (or lack of return statement, which conceptually is the same, but since we retrieve the type hints at runtime, is a bit different, and that's why not using a type hint works as expected here) should raise an error. EDIT: Also, this is in the "Issues" tab. 😁👍 |
An explicit |
Privileged issue
Issue Content
Reproduce
204 No Content
None
to said routeExpected
The file above should boot up fine when running
uvicorn filename:app --reload
Actual
This raises an error:
AssertionError: Status code 204 must not have a response body
full traceback
Current workarounds
from __future__ import annotations
None
PRs
There was a PR posted in the discussion #6060 (comment)
After stepping through the code, I think it might make sense to return early in
fastapi.dependencies.utils.get_typed_annotation
fastapi.dependencies.utils.get_typed_return_annotation
ifannotation == "None"
(PR incoming).The text was updated successfully, but these errors were encountered: