Skip to content

Commit 6805798

Browse files
committed
Fix all typing issues
1 parent 4cfdabd commit 6805798

File tree

5 files changed

+65
-47
lines changed

5 files changed

+65
-47
lines changed

httptools/parser/__init__.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
from .protocol import HTTPProtocol
2-
from .parser import * # NoQA
3-
from .errors import * # NoQA
4-
from .url_parser import * # NoQA
2+
from .parser import HttpParser, HttpRequestParser, HttpResponseParser # NoQA
3+
from .errors import (
4+
HttpParserError,
5+
HttpParserCallbackError,
6+
HttpParserInvalidStatusError,
7+
HttpParserInvalidMethodError,
8+
HttpParserInvalidURLError,
9+
HttpParserUpgrade,
10+
)
11+
from .url_parser import parse_url
512

6-
__all__ = parser.__all__ + errors.__all__ + url_parser.__all__ # NoQA
13+
__all__ = (
14+
# protocol
15+
"HTTPProtocol",
16+
# parser
17+
"HttpParser",
18+
"HttpRequestParser",
19+
"HttpResponseParser",
20+
# errors
21+
"HttpParserError",
22+
"HttpParserCallbackError",
23+
"HttpParserInvalidStatusError",
24+
"HttpParserInvalidMethodError",
25+
"HttpParserInvalidURLError",
26+
"HttpParserUpgrade",
27+
# url_parser
28+
"parse_url",
29+
)

httptools/parser/parser.pyi

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,53 @@
1-
from typing import Union, Any
1+
from typing import Union
22
from array import array
33
from .protocol import HTTPProtocol
44

55
class HttpParser:
6-
def __init__(self, protocol: Union[HTTPProtocol, Any]) -> None:
7-
"""
8-
protocol -- a Python object with the following methods
9-
(all optional):
10-
11-
- on_message_begin()
12-
- on_url(url: bytes)
13-
- on_header(name: bytes, value: bytes)
14-
- on_headers_complete()
15-
- on_body(body: bytes)
16-
- on_message_complete()
17-
- on_chunk_header()
18-
- on_chunk_complete()
19-
- on_status(status: bytes)
6+
def __init__(self, protocol: HTTPProtocol) -> None:
7+
"""The HTTP parser.
8+
9+
Args:
10+
protocol(HTTPProtocol): a Python object with the following methods (all optional):
11+
- on_message_begin(self): ...
12+
- on_url(self, url: bytes): ...
13+
- on_header(self, name: bytes, value: bytes): ...
14+
- on_headers_complete(self): ...
15+
- on_body(self, body: bytes): ...
16+
- on_message_complete(self): ...
17+
- on_chunk_header(self): ...
18+
- on_chunk_complete(self): ...
19+
- on_status(self, status: bytes): ...
2020
"""
2121

2222
def get_http_version(self) -> str:
2323
"""Return an HTTP protocol version."""
24-
...
2524

2625
def should_keep_alive(self) -> bool:
2726
"""Return ``True`` if keep-alive mode is preferred."""
28-
...
2927

3028
def should_upgrade(self) -> bool:
3129
"""Return ``True`` if the parsed request is a valid Upgrade request.
3230
The method exposes a flag set just before on_headers_complete.
3331
Calling this method earlier will only yield `False`."""
34-
...
3532

36-
def feed_data(self, data: Union[bytes, bytearray, memoryview, array]) -> None:
33+
def feed_data(self, data: Union[bytes, bytearray, memoryview, array[int]]) -> None:
3734
"""Feed data to the parser.
3835
39-
Will eventually trigger callbacks on the ``protocol``
40-
object.
36+
Will eventually trigger callbacks on the ``protocol`` object.
4137
4238
On HTTP upgrade, this method will raise an
4339
``HttpParserUpgrade`` exception, with its sole argument
4440
set to the offset of the non-HTTP data in ``data``.
4541
"""
4642

4743
class HttpRequestParser(HttpParser):
48-
"""Used for parsing http requests from the server's side"""
44+
"""Used for parsing http requests from the server side."""
4945

5046
def get_method(self) -> bytes:
51-
"""Return HTTP request method (GET, HEAD, etc)"""
47+
"""Retrieve the HTTP method of the request."""
5248

5349
class HttpResponseParser(HttpParser):
54-
"""Used for parsing http requests from the client's side"""
50+
"""Used for parsing http responses from the client side."""
5551

5652
def get_status_code(self) -> int:
57-
"""Return the status code of the HTTP response"""
53+
"""Retrieve the status code of the HTTP response."""

httptools/parser/protocol.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
class HTTPProtocol(Protocol):
55
"""Used for providing static type-checking when parsing through the http protocol"""
66

7-
def on_message_begin() -> None: ...
8-
def on_url(url: bytes) -> None: ...
9-
def on_header(name: bytes, value: bytes) -> None: ...
10-
def on_headers_complete() -> None: ...
11-
def on_body(body: bytes) -> None: ...
12-
def on_message_complete() -> None: ...
13-
def on_chunk_header() -> None: ...
14-
def on_chunk_complete() -> None: ...
15-
def on_status(status: bytes) -> None: ...
7+
def on_message_begin(self) -> None: ...
8+
def on_url(self, url: bytes) -> None: ...
9+
def on_header(self, name: bytes, value: bytes) -> None: ...
10+
def on_headers_complete(self) -> None: ...
11+
def on_body(self, body: bytes) -> None: ...
12+
def on_message_complete(self) -> None: ...
13+
def on_chunk_header(self) -> None: ...
14+
def on_chunk_complete(self) -> None: ...
15+
def on_status(self, status: bytes) -> None: ...

httptools/parser/url_parser.pyi

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@ class URL:
1010
fragment: bytes
1111
userinfo: bytes
1212

13-
def parse_url(url: Union[bytes, bytearray, memoryview, array]) -> URL:
13+
def parse_url(url: Union[bytes, bytearray, memoryview, array[int]]) -> URL:
1414
"""Parse URL strings into a structured Python object.
1515
1616
Returns an instance of ``httptools.URL`` class with the
1717
following attributes:
1818
19-
- schema: bytes
20-
- host: bytes
19+
- schema(bytes): The schema of the URL.
20+
- host(bytes): The host of the URL.
2121
- port: int
22-
- path: bytes
23-
- query: bytes
24-
- fragment: bytes
25-
- userinfo: bytes
22+
- path(bytes): The path of the URL.
23+
- query(bytes): The query of the URL.
24+
- fragment(bytes): The fragment of the URL.
25+
- userinfo(bytes): The userinfo of the URL.
2626
"""
27-
...

0 commit comments

Comments
 (0)