[FEATURE] Headers should be typed as Optional
See original GitHub issueIs 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...
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Standard library header <optional> (C++17) - cppreference.com
Standard library header <optional> (C++17) Note: a slash '/' in a revision mark means that the header was deprecated and/or removed.
Read more >TypeScript optional object key not behaving as expected
Type 'undefined' is not assignable to type 'string'. The only way I could get this to work is type RequestHeaders = { [key:...
Read more >Headers and footers in a worksheet - Microsoft Support
You can add headers or footers at the top or bottom of a printed worksheet in Excel. ... On the Insert tab, in...
Read more >HTTP/1.1: Header Field Definitions
SHOULD be interpreted as "I prefer audio/basic, but send me any audio type if it is the best available after an 80% mark-down...
Read more >HTTP headers - MDN Web Docs - Mozilla
Governs which referrer information sent in the Referer header should be included with requests made. User-Agent. Contains a characteristic ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For everyone confused as me
...
here is ellipsis operator - I mean, literally...
Or you can as well do this:(in pydentic
Required = Ellipsis
)@ElPapi42 just use
param_name: str = Header(...)