-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Closed
Labels
Description
Is your feature request related to a problem? Please describe.
Setting headers for routes requires adding invalid type information because while headers are optional, they cannot be typed as optional.
Describe the solution you'd like
Headers that are typed as str should error if they are not provided. Otherwise, headers should be typed as Optional if they are so.
Describe alternatives you've considered
N/A
Additional context
x_custom_header will not be str if the request doesn't include it.
async def example_route(
payload: dict, *, x_custom_header: str = Header(None)
):
assert x_custom_header is not None
return {"msg": "ok"}Changing the type hint to typing.Optional[str] raises an assertion error: AssertionError('Parameters for Query and Header must be of type str, int, float, bool, list, tuple or set...
Victor-Savu, haizaar and kkirsche