Skip to content

Headers should be typed as Optional #179

@chdsbd

Description

@chdsbd

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...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions