fastapi-utils icon indicating copy to clipboard operation
fastapi-utils copied to clipboard

[BUG] Inferring route throws exception when return typehint is set to None.

Open synchronizing opened this issue 3 years ago • 0 comments

While it is implied that a function without a return typehint will return None some might choose to explicitly write it in their code. As of right now the following code will raise an unwanted (and likely unintentioned) exception:

from fastapi import FastAPI, status
from fastapi_utils.inferring_router import InferringRouter

app = FastAPI()
router = InferringRouter()

@router.get("/inferred", status_code=status.HTTP_204_NO_CONTENT)
def get_resource(resource_id: int) -> None:
    pass

app.include_router(router)

Error thrown:

  File "./test.py", line 9, in <module>
    def get_resource(resource_id: int) -> None:
  File "/Users/felipe/.pyenv/versions/3.8.5/lib/python3.8/site-packages/fastapi/routing.py", line 551, in decorator
    self.add_api_route(
  File "/Users/felipe/.pyenv/versions/3.8.5/lib/python3.8/site-packages/fastapi_utils/inferring_router.py", line 18, in add_api_route
    return super().add_api_route(path, endpoint, **kwargs)
  File "/Users/felipe/.pyenv/versions/3.8.5/lib/python3.8/site-packages/fastapi/routing.py", line 496, in add_api_route
    route = route_class(
  File "/Users/felipe/.pyenv/versions/3.8.5/lib/python3.8/site-packages/fastapi/routing.py", line 320, in __init__
    assert (
AssertionError: Status code 204 must not have a response body

synchronizing avatar Mar 04 '21 06:03 synchronizing