Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

FastAPI==0.89.0 Cannot use None as a return type when status_code is set to 204 with from __future__ import annotations #5859

Closed
9 tasks done
matiuszka opened this issue Jan 9, 2023 · 2 comments · May be fixed by #5860
Closed
9 tasks done
Labels
question Question or problem question-migrate

Comments

@matiuszka
Copy link
Contributor

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from __future__ import annotations 

from fastapi import FastAPI

app = FastAPI()


@app.get("/", status_code=204)
def read_root() -> None:
    return {"Hello": "World"}

Description

If we add:
from __future__ import annotations

It changes the annotations structure and the response model is NoneType instead of None, which causes validation of the statuc_code vs response_model and raises an exception.

  ...
  File ".../site-packages/fastapi/routing.py", line 635, in decorator
    self.add_api_route(
  File ".../site-packages/fastapi/routing.py", line 574, in add_api_route
    route = route_class(
  File ".../site-packages/fastapi/routing.py", line 398, in __init__
    assert is_body_allowed_for_status_code(
AssertionError: Status code 204 must not have a response body

I am working on a fix for it right now.

Operating System

macOS

Operating System Details

No response

FastAPI Version

0.89.0

Python Version

3.10

Additional Context

No response

@matiuszka matiuszka added the question Question or problem label Jan 9, 2023
@matiuszka matiuszka changed the title FastAPI==0.89.0 Cannot use None as a return type when status_code is set to 204 FastAPI==0.89.0 Cannot use None as a return type when status_code is set to 204 with from __future__ import annotations Jan 9, 2023
@matiuszka matiuszka changed the title FastAPI==0.89.0 Cannot use None as a return type when status_code is set to 204 with from __future__ import annotations FastAPI==0.89.0 Cannot useNone as a return type when status_code is set to 204 with from __future__ import annotations Jan 9, 2023
@matiuszka matiuszka changed the title FastAPI==0.89.0 Cannot useNone as a return type when status_code is set to 204 with from __future__ import annotations FastAPI==0.89.0 Cannot use None as a return type when status_code is set to 204 with from __future__ import annotations Jan 9, 2023
@tiangolo
Copy link
Owner

Thanks for the report! ☕

This is because the return type is being used to generate the response model, it's a new "feature" 😅

You can solve it with response_model=None.

There's a lot of new docs about it here: https://fastapi.tiangolo.com/tutorial/response-model/#other-return-type-annotations

from __future__ import annotations 

from fastapi import FastAPI

app = FastAPI()


@app.get("/", status_code=204, response_model=None)
def read_root() -> None:
    return {"Hello": "World"}

@matiuszka
Copy link
Contributor Author

@tiangolo thanks, it helped :)

Although I think this is not cool if there is an error or not depending on if you will add from __future__ import annotations or not.

Are there any plans to improve it? I did proposal here how it can be solved: #5860

@github-actions github-actions bot removed the answered label Jan 12, 2023
Repository owner locked and limited conversation to collaborators Feb 24, 2023
@tiangolo tiangolo converted this issue into discussion #6060 Feb 24, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants