-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
override http.HTTPMessage
methods to only use str
for the header type
#11114
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
f10dd55
5ad636a
cf667cf
df10900
8973977
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,6 +99,24 @@ responses: dict[int, str] | |
|
||
class HTTPMessage(email.message.Message): | ||
def getallmatchingheaders(self, name: str) -> list[str]: ... # undocumented | ||
# override below all of Message's methods that use `_HeaderType` / `_HeaderTypeParam` with `str` | ||
# `HTTPMessage` breaks the Liskov substitution principle by only intending for `str` headers | ||
# This is easier than making `Message` generic | ||
def __getitem__(self, name: str) -> str | Any: ... # See comment on `Message.__getitem__` | ||
def __setitem__(self, name: str, val: str) -> None: ... | ||
def values(self) -> list[str]: ... | ||
def items(self) -> list[tuple[str, str]]: ... | ||
@overload | ||
def get(self, name: str, failobj: None = None) -> str | None: ... | ||
@overload | ||
def get(self, name: str, failobj: _T) -> str | _T: ... | ||
@overload | ||
def get_all(self, name: str, failobj: None = None) -> list[str] | None: ... | ||
@overload | ||
def get_all(self, name: str, failobj: _T) -> list[str] | _T: ... | ||
Comment on lines
+115
to
+116
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally, we'd be able to have an overload like this, but I don't think it's possible:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what such an overload would be trying to do. Consumer code is the one passing the def foo(message: HttpMessage) -> list[str] | SentinelType:
return message.get_all("foo", sentinel_value) # Ok
def foo(message: HttpMessage) -> list[str]:
return message.get_all("foo", []) # Fails pyright `reportUnknownArgumentType` but by no fault of the definition
def foo(message: HttpMessage) -> list[str]:
return message.get_all("foo", list[str]()) # ok
def foo(message: HttpMessage) -> list[str]:
return message.get_all("foo", sentinel_value) # Fails type-checking If we still really wanna restrict it, we can bind |
||
def replace_header(self, _name: str, _value: str) -> None: ... | ||
def set_raw(self, name: str, value: str) -> None: ... | ||
def raw_items(self) -> Iterator[tuple[str, str]]: ... | ||
|
||
def parse_headers(fp: io.BufferedIOBase, _class: Callable[[], email.message.Message] = ...) -> HTTPMessage: ... | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.