-
Notifications
You must be signed in to change notification settings - Fork 96
Fix all typing issues #134
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
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 |
|---|---|---|
| @@ -1,6 +1,29 @@ | ||
| from .protocol import HTTPProtocol | ||
| from .parser import * # NoQA | ||
| from .errors import * # NoQA | ||
| from .url_parser import * # NoQA | ||
| from .parser import HttpParser, HttpRequestParser, HttpResponseParser # NoQA | ||
| from .errors import ( | ||
| HttpParserError, | ||
| HttpParserCallbackError, | ||
| HttpParserInvalidStatusError, | ||
| HttpParserInvalidMethodError, | ||
| HttpParserInvalidURLError, | ||
| HttpParserUpgrade, | ||
| ) | ||
| from .url_parser import parse_url | ||
|
|
||
| __all__ = parser.__all__ + errors.__all__ + url_parser.__all__ # NoQA | ||
| __all__ = ( | ||
| # protocol | ||
| "HTTPProtocol", | ||
| # parser | ||
| "HttpParser", | ||
| "HttpRequestParser", | ||
| "HttpResponseParser", | ||
| # errors | ||
| "HttpParserError", | ||
| "HttpParserCallbackError", | ||
| "HttpParserInvalidStatusError", | ||
| "HttpParserInvalidMethodError", | ||
| "HttpParserInvalidURLError", | ||
| "HttpParserUpgrade", | ||
| # url_parser | ||
| "parse_url", | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,57 +1,59 @@ | ||
| from typing import Union, Any | ||
| from typing import Union | ||
| from array import array | ||
| from .protocol import HTTPProtocol | ||
|
|
||
| class HttpParser: | ||
| def __init__(self, protocol: Union[HTTPProtocol, Any]) -> None: | ||
| """ | ||
| protocol -- a Python object with the following methods | ||
| (all optional): | ||
| - on_message_begin() | ||
| - on_url(url: bytes) | ||
| - on_header(name: bytes, value: bytes) | ||
| - on_headers_complete() | ||
| - on_body(body: bytes) | ||
| - on_message_complete() | ||
| - on_chunk_header() | ||
| - on_chunk_complete() | ||
| - on_status(status: bytes) | ||
|
Comment on lines
-11
to
-19
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. This is not needed. Implementers can follow the protocol. |
||
| def __init__(self, protocol: HTTPProtocol | object) -> None: | ||
|
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.
Python still doesn't support "not required class method protocol": https://discuss.python.org/t/discussion-optional-class-and-protocol-fields-and-methods |
||
| """The HTTP parser. | ||
| Args: | ||
| protocol (HTTPProtocol): Callback interface for the parser. | ||
| """ | ||
|
|
||
| def set_dangerous_leniencies( | ||
|
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. This was missing. |
||
| self, | ||
| lenient_headers: bool | None = None, | ||
| lenient_chunked_length: bool | None = None, | ||
| lenient_keep_alive: bool | None = None, | ||
| lenient_transfer_encoding: bool | None = None, | ||
| lenient_version: bool | None = None, | ||
| lenient_data_after_close: bool | None = None, | ||
| lenient_optional_lf_after_cr: bool | None = None, | ||
| lenient_optional_cr_before_lf: bool | None = None, | ||
| lenient_optional_crlf_after_chunk: bool | None = None, | ||
| lenient_spaces_after_chunk_size: bool | None = None, | ||
| ) -> None: | ||
| """Set dangerous leniencies for the parser.""" | ||
|
|
||
| def get_http_version(self) -> str: | ||
| """Return an HTTP protocol version.""" | ||
| ... | ||
| """Retrieve the HTTP protocol version e.g. "1.1".""" | ||
|
|
||
| def should_keep_alive(self) -> bool: | ||
| """Return ``True`` if keep-alive mode is preferred.""" | ||
| ... | ||
| """Return `True` if keep-alive mode is preferred.""" | ||
|
|
||
| def should_upgrade(self) -> bool: | ||
| """Return ``True`` if the parsed request is a valid Upgrade request. | ||
| """Return `True` if the parsed request is a valid Upgrade request. | ||
| The method exposes a flag set just before on_headers_complete. | ||
| Calling this method earlier will only yield `False`.""" | ||
| ... | ||
|
|
||
| def feed_data(self, data: Union[bytes, bytearray, memoryview, array]) -> None: | ||
| def feed_data(self, data: Union[bytes, bytearray, memoryview, array[int]]) -> None: | ||
|
||
| """Feed data to the parser. | ||
| Will eventually trigger callbacks on the ``protocol`` | ||
| object. | ||
| Will eventually trigger callbacks on the ``protocol`` object. | ||
| On HTTP upgrade, this method will raise an | ||
| ``HttpParserUpgrade`` exception, with its sole argument | ||
| set to the offset of the non-HTTP data in ``data``. | ||
| """ | ||
|
|
||
| class HttpRequestParser(HttpParser): | ||
| """Used for parsing http requests from the server's side""" | ||
| """Used for parsing http requests from the server side.""" | ||
|
|
||
| def get_method(self) -> bytes: | ||
| """Return HTTP request method (GET, HEAD, etc)""" | ||
| """Retrieve the HTTP method of the request.""" | ||
|
|
||
| class HttpResponseParser(HttpParser): | ||
| """Used for parsing http requests from the client's side""" | ||
| """Used for parsing http responses from the client side.""" | ||
|
|
||
| def get_status_code(self) -> int: | ||
| """Return the status code of the HTTP response""" | ||
| """Retrieve the status code of the HTTP response.""" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,12 +4,12 @@ | |
| class HTTPProtocol(Protocol): | ||
| """Used for providing static type-checking when parsing through the http protocol""" | ||
|
|
||
| def on_message_begin() -> None: ... | ||
| def on_url(url: bytes) -> None: ... | ||
| def on_header(name: bytes, value: bytes) -> None: ... | ||
| def on_headers_complete() -> None: ... | ||
| def on_body(body: bytes) -> None: ... | ||
| def on_message_complete() -> None: ... | ||
| def on_chunk_header() -> None: ... | ||
| def on_chunk_complete() -> None: ... | ||
| def on_status(status: bytes) -> None: ... | ||
| def on_message_begin(self) -> None: ... | ||
| def on_url(self, url: bytes) -> None: ... | ||
| def on_header(self, name: bytes, value: bytes) -> None: ... | ||
| def on_headers_complete(self) -> None: ... | ||
| def on_body(self, body: bytes) -> None: ... | ||
| def on_message_complete(self) -> None: ... | ||
| def on_chunk_header(self) -> None: ... | ||
| def on_chunk_complete(self) -> None: ... | ||
| def on_status(self, status: bytes) -> None: ... | ||
|
Comment on lines
+7
to
+15
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. Missing |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,18 +10,5 @@ class URL: | |
| fragment: bytes | ||
| userinfo: bytes | ||
|
|
||
| def parse_url(url: Union[bytes, bytearray, memoryview, array]) -> URL: | ||
| """Parse URL strings into a structured Python object. | ||
| Returns an instance of ``httptools.URL`` class with the | ||
| following attributes: | ||
| - schema: bytes | ||
| - host: bytes | ||
| - port: int | ||
| - path: bytes | ||
| - query: bytes | ||
| - fragment: bytes | ||
| - userinfo: bytes | ||
|
Comment on lines
-16
to
-25
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. Irrelevant. The |
||
| """ | ||
| ... | ||
| def parse_url(url: Union[bytes, bytearray, memoryview, array[int]]) -> URL: | ||
| """Parse a URL string into a structured Python object.""" | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Proper definition.