Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Type annotations for headers in HTTPException declare Any as values but only accept str #9649

Closed
9 tasks done
tiangolo opened this issue Jun 10, 2023 Discussed in #8562 · 1 comment
Closed
9 tasks done
Labels
bug Something isn't working

Comments

@tiangolo
Copy link
Owner

馃悰 Type annotations for headers in HTTPException declare Any as values but only accept str.

Fixed in #9648

Discussed in #8562

Originally posted by DorAbu January 20, 2022

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 fastapi import FastAPI, HTTPException

app = FastAPI()


@app.get("/")
async def root():
    raise HTTPException(
        status_code=503,
        detail=f"The server is not ready to handle the request, due to ...",
        headers={"Retry-After": 20}
    )

Description

In HTTPException object signature (attached below 1.), headers type hint is Optional[Dict[str, Any]].
But for example, when Dict[str, int] is given as in the "example code" section, the implementation in Starlette will raise an exception - AttributeError: 'int' object has no attribute 'encode' (attached below 2.).

Seems that Starlette implementation expects Optional[Dict[str, str]] type hint, FastAPI may resolve it by converting the values of the headers to strings.

  1. image
  2. image

Operating System

Windows

Operating System Details

No response

FastAPI Version

0.70.1

Python Version

3.8.9 (tags/v3.8.9:a743f81, Apr 6 2021, 14:02:34) [MSC v.1928 64 bit (AMD64)]

Additional Context

starlette==0.16.0

@tiangolo tiangolo added the bug Something isn't working label Jun 10, 2023
@tiangolo
Copy link
Owner Author

Fixed in #9648

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant