question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FEATURE] Headers should be typed as Optional

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

13reactions
Pyczcommented, Jun 2, 2021

@ElPapi42 just use param_name: str = Header(...)

For everyone confused as me ... here is ellipsis operator - I mean, literally ... Or you can as well do this:

from pydantic import Required

param_name: str = Header(Required)

(in pydentic Required = Ellipsis)

7reactions
ycdcommented, Mar 16, 2021

@ElPapi42 just use param_name: str = Header(...)

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found