Skip to content

Commit 1c38bc8

Browse files
committed
Added explicit typing for socketpool and socket with shared interface
1 parent d8f9a72 commit 1c38bc8

File tree

4 files changed

+72
-19
lines changed

4 files changed

+72
-19
lines changed

adafruit_httpserver/interfaces.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,70 @@
88
"""
99

1010
try:
11-
from typing import List, Dict, Union, Any
11+
from typing import List, Tuple, Dict, Union, Any
1212
except ImportError:
1313
pass
1414

1515

16+
class _ISocket: # pylint: disable=missing-function-docstring,no-self-use,unused-argument
17+
"""A class for typing necessary methods for a socket object."""
18+
19+
def accept(self) -> Tuple["_ISocket", Tuple[str, int]]:
20+
...
21+
22+
def bind(self, address: Tuple[str, int]) -> None:
23+
...
24+
25+
def setblocking(self, flag: bool) -> None:
26+
...
27+
28+
def settimeout(self, value: "Union[float, None]") -> None:
29+
...
30+
31+
def setsockopt(self, level: int, optname: int, value: int) -> None:
32+
...
33+
34+
def listen(self, backlog: int) -> None:
35+
...
36+
37+
def send(self, data: bytes) -> int:
38+
...
39+
40+
def recv_into(self, buffer: memoryview, nbytes: int) -> int:
41+
...
42+
43+
def close(self) -> None:
44+
...
45+
46+
47+
class _ISocketPool: # pylint: disable=missing-function-docstring,no-self-use,unused-argument
48+
"""A class to typing necessary methods and properties for a socket pool object."""
49+
50+
AF_INET: int
51+
SO_REUSEADDR: int
52+
SOCK_STREAM: int
53+
SOL_SOCKET: int
54+
55+
def socket( # pylint: disable=redefined-builtin
56+
self,
57+
family: int = ...,
58+
type: int = ...,
59+
proto: int = ...,
60+
) -> _ISocket:
61+
...
62+
63+
def getaddrinfo( # pylint: disable=redefined-builtin,too-many-arguments
64+
self,
65+
host: str,
66+
port: int,
67+
family: int = ...,
68+
type: int = ...,
69+
proto: int = ...,
70+
flags: int = ...,
71+
) -> Tuple[int, int, int, str, Tuple[str, int]]:
72+
...
73+
74+
1675
class _IFieldStorage:
1776
"""Interface with shared methods for QueryParams, FormData and Headers."""
1877

adafruit_httpserver/request.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
try:
1111
from typing import List, Dict, Tuple, Union, Any, TYPE_CHECKING
12-
from socket import socket
13-
from socketpool import SocketPool
1412

1513
if TYPE_CHECKING:
1614
from .server import Server
@@ -20,7 +18,7 @@
2018
import json
2119

2220
from .headers import Headers
23-
from .interfaces import _IFieldStorage, _IXSSSafeFieldStorage
21+
from .interfaces import _ISocket, _IFieldStorage, _IXSSSafeFieldStorage
2422
from .methods import POST, PUT, PATCH, DELETE
2523

2624

@@ -274,7 +272,7 @@ class Request: # pylint: disable=too-many-instance-attributes
274272
Server object that received the request.
275273
"""
276274

277-
connection: Union["SocketPool.Socket", "socket.socket"]
275+
connection: _ISocket
278276
"""
279277
Socket object used to send and receive data on the connection.
280278
"""
@@ -325,7 +323,7 @@ class Request: # pylint: disable=too-many-instance-attributes
325323
def __init__(
326324
self,
327325
server: "Server",
328-
connection: Union["SocketPool.Socket", "socket.socket"],
326+
connection: _ISocket,
329327
client_address: Tuple[str, int],
330328
raw_request: bytes = None,
331329
) -> None:

adafruit_httpserver/response.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
try:
1111
from typing import Optional, Dict, Union, Tuple, Generator, Any
12-
from socket import socket
13-
from socketpool import SocketPool
1412
except ImportError:
1513
pass
1614

@@ -47,6 +45,7 @@
4745
PERMANENT_REDIRECT_308,
4846
)
4947
from .headers import Headers
48+
from .interfaces import _ISocket
5049

5150

5251
class Response: # pylint: disable=too-few-public-methods
@@ -132,7 +131,7 @@ def _send(self) -> None:
132131

133132
def _send_bytes(
134133
self,
135-
conn: Union["SocketPool.Socket", "socket.socket"],
134+
conn: _ISocket,
136135
buffer: Union[bytes, bytearray, memoryview],
137136
):
138137
bytes_sent: int = 0
@@ -708,7 +707,7 @@ def _read_frame(self):
708707
length -= min(payload_length, length)
709708

710709
if has_mask:
711-
payload = bytes(x ^ mask[i % 4] for i, x in enumerate(payload))
710+
payload = bytes(byte ^ mask[idx % 4] for idx, byte in enumerate(payload))
712711

713712
return opcode, payload
714713

adafruit_httpserver/server.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
"""
99

1010
try:
11-
from typing import Callable, Protocol, Union, List, Tuple, Dict, Iterable
12-
from socket import socket
13-
from socketpool import SocketPool
11+
from typing import Callable, Union, List, Tuple, Dict, Iterable
1412
except ImportError:
1513
pass
1614

@@ -28,6 +26,7 @@
2826
ServingFilesDisabledError,
2927
)
3028
from .headers import Headers
29+
from .interfaces import _ISocketPool, _ISocket
3130
from .methods import GET, HEAD
3231
from .request import Request
3332
from .response import Response, FileResponse
@@ -54,7 +53,7 @@ class Server: # pylint: disable=too-many-instance-attributes
5453
"""Root directory to serve files from. ``None`` if serving files is disabled."""
5554

5655
def __init__(
57-
self, socket_source: Protocol, root_path: str = None, *, debug: bool = False
56+
self, socket_source: _ISocketPool, root_path: str = None, *, debug: bool = False
5857
) -> None:
5958
"""Create a server, and get it ready to run.
6059
@@ -244,9 +243,7 @@ def stop(self) -> None:
244243
if self.debug:
245244
_debug_stopped_server(self)
246245

247-
def _receive_header_bytes(
248-
self, sock: Union["SocketPool.Socket", "socket.socket"]
249-
) -> bytes:
246+
def _receive_header_bytes(self, sock: _ISocket) -> bytes:
250247
"""Receive bytes until a empty line is received."""
251248
received_bytes = bytes()
252249
while b"\r\n\r\n" not in received_bytes:
@@ -263,7 +260,7 @@ def _receive_header_bytes(
263260

264261
def _receive_body_bytes(
265262
self,
266-
sock: Union["SocketPool.Socket", "socket.socket"],
263+
sock: _ISocket,
267264
received_body_bytes: bytes,
268265
content_length: int,
269266
) -> bytes:
@@ -282,7 +279,7 @@ def _receive_body_bytes(
282279

283280
def _receive_request(
284281
self,
285-
sock: Union["SocketPool.Socket", "socket.socket"],
282+
sock: _ISocket,
286283
client_address: Tuple[str, int],
287284
) -> Request:
288285
"""Receive bytes from socket until the whole request is received."""

0 commit comments

Comments
 (0)