Skip to content

Commit d6f470e

Browse files
authored
Run format and mypy on Python 3.8
1 parent a2c6894 commit d6f470e

10 files changed

+58
-75
lines changed

h11/_connection.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ def _extract_next_receive_event(
428428
# return that event, and then the state will change and we'll
429429
# get called again to generate the actual ConnectionClosed().
430430
if hasattr(self._reader, "read_eof"):
431-
event = self._reader.read_eof() # type: ignore[attr-defined]
431+
event = self._reader.read_eof()
432432
else:
433433
event = ConnectionClosed()
434434
if event is None:
@@ -505,7 +505,13 @@ def send(self, event: ConnectionClosed) -> None:
505505
...
506506

507507
@overload
508-
def send(self, event: Event) -> bytes:
508+
def send(
509+
self, event: Union[Request, InformationalResponse, Response, Data, EndOfMessage]
510+
) -> bytes:
511+
...
512+
513+
@overload
514+
def send(self, event: Event) -> Optional[bytes]:
509515
...
510516

511517
def send(self, event: Event) -> Optional[bytes]:

h11/_events.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
import re
99
from abc import ABC
10-
from dataclasses import dataclass, field
11-
from typing import Any, cast, Dict, List, Tuple, Union
10+
from dataclasses import dataclass
11+
from typing import List, Tuple, Union
1212

1313
from ._abnf import method, request_target
1414
from ._headers import Headers, normalize_and_validate

h11/tests/test_against_stdlib_http.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
@contextmanager
1515
def socket_server(
16-
handler: Callable[..., socketserver.BaseRequestHandler]
16+
handler: Callable[..., socketserver.BaseRequestHandler],
1717
) -> Generator[socketserver.TCPServer, None, None]:
1818
httpd = socketserver.TCPServer(("127.0.0.1", 0), handler)
1919
thread = threading.Thread(
@@ -39,17 +39,17 @@ def translate_path(self, path: str) -> str:
3939

4040
def test_h11_as_client() -> None:
4141
with socket_server(SingleMindedRequestHandler) as httpd:
42-
with closing(socket.create_connection(httpd.server_address)) as s:
42+
with closing(socket.create_connection(httpd.server_address)) as s: # type: ignore[arg-type]
4343
c = h11.Connection(h11.CLIENT)
4444

4545
s.sendall(
46-
c.send( # type: ignore[arg-type]
46+
c.send(
4747
h11.Request(
4848
method="GET", target="/foo", headers=[("Host", "localhost")]
4949
)
5050
)
5151
)
52-
s.sendall(c.send(h11.EndOfMessage())) # type: ignore[arg-type]
52+
s.sendall(c.send(h11.EndOfMessage()))
5353

5454
data = bytearray()
5555
while True:
@@ -96,15 +96,15 @@ def handle(self) -> None:
9696
},
9797
}
9898
)
99-
s.sendall(c.send(h11.Response(status_code=200, headers=[]))) # type: ignore[arg-type]
99+
s.sendall(c.send(h11.Response(status_code=200, headers=[])))
100100
s.sendall(c.send(h11.Data(data=info.encode("ascii"))))
101101
s.sendall(c.send(h11.EndOfMessage()))
102102

103103

104104
def test_h11_as_server() -> None:
105105
with socket_server(H11RequestHandler) as httpd:
106106
host, port = httpd.server_address
107-
url = f"http://{host}:{port}/some-path"
107+
url = f"http://{host}:{port}/some-path" # type: ignore[str-bytes-safe]
108108
with closing(urlopen(url)) as f:
109109
assert f.getcode() == 200
110110
data = f.read()

h11/tests/test_connection.py

+22-24
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
ConnectionClosed,
88
Data,
99
EndOfMessage,
10-
Event,
1110
InformationalResponse,
1211
Request,
1312
Response,
@@ -17,7 +16,6 @@
1716
CLOSED,
1817
DONE,
1918
ERROR,
20-
IDLE,
2119
MIGHT_SWITCH_PROTOCOL,
2220
MUST_CLOSE,
2321
SEND_BODY,
@@ -48,15 +46,15 @@ def test__keep_alive() -> None:
4846
)
4947
)
5048
assert not _keep_alive(
51-
Request(method="GET", target="/", headers=[], http_version="1.0") # type: ignore[arg-type]
49+
Request(method="GET", target="/", headers=[], http_version="1.0")
5250
)
5351

54-
assert _keep_alive(Response(status_code=200, headers=[])) # type: ignore[arg-type]
52+
assert _keep_alive(Response(status_code=200, headers=[]))
5553
assert not _keep_alive(Response(status_code=200, headers=[("Connection", "close")]))
5654
assert not _keep_alive(
5755
Response(status_code=200, headers=[("Connection", "a, b, cLOse, foo")])
5856
)
59-
assert not _keep_alive(Response(status_code=200, headers=[], http_version="1.0")) # type: ignore[arg-type]
57+
assert not _keep_alive(Response(status_code=200, headers=[], http_version="1.0"))
6058

6159

6260
def test__body_framing() -> None:
@@ -135,7 +133,7 @@ def test_Connection_basics_and_content_length() -> None:
135133
assert p.conn[CLIENT].their_http_version is None
136134
assert p.conn[SERVER].their_http_version == b"1.1"
137135

138-
data = p.send(SERVER, InformationalResponse(status_code=100, headers=[])) # type: ignore[arg-type]
136+
data = p.send(SERVER, InformationalResponse(status_code=100, headers=[]))
139137
assert data == b"HTTP/1.1 100 \r\n\r\n"
140138

141139
data = p.send(SERVER, Response(status_code=200, headers=[("Content-Length", "11")]))
@@ -247,7 +245,7 @@ def test_client_talking_to_http10_server() -> None:
247245
assert c.our_state is DONE
248246
# No content-length, so Http10 framing for body
249247
assert receive_and_get(c, b"HTTP/1.0 200 OK\r\n\r\n") == [
250-
Response(status_code=200, headers=[], http_version="1.0", reason=b"OK") # type: ignore[arg-type]
248+
Response(status_code=200, headers=[], http_version="1.0", reason=b"OK")
251249
]
252250
assert c.our_state is MUST_CLOSE
253251
assert receive_and_get(c, b"12345") == [Data(data=b"12345")]
@@ -261,14 +259,14 @@ def test_server_talking_to_http10_client() -> None:
261259
# No content-length, so no body
262260
# NB: no host header
263261
assert receive_and_get(c, b"GET / HTTP/1.0\r\n\r\n") == [
264-
Request(method="GET", target="/", headers=[], http_version="1.0"), # type: ignore[arg-type]
262+
Request(method="GET", target="/", headers=[], http_version="1.0"),
265263
EndOfMessage(),
266264
]
267265
assert c.their_state is MUST_CLOSE
268266

269267
# We automatically Connection: close back at them
270268
assert (
271-
c.send(Response(status_code=200, headers=[])) # type: ignore[arg-type]
269+
c.send(Response(status_code=200, headers=[]))
272270
== b"HTTP/1.1 200 \r\nConnection: close\r\n\r\n"
273271
)
274272

@@ -356,7 +354,7 @@ def test_automagic_connection_close_handling() -> None:
356354
p.send(
357355
SERVER,
358356
# no header here...
359-
[Response(status_code=204, headers=[]), EndOfMessage()], # type: ignore[arg-type]
357+
[Response(status_code=204, headers=[]), EndOfMessage()],
360358
# ...but oh look, it arrived anyway
361359
expect=[
362360
Response(status_code=204, headers=[("connection", "close")]),
@@ -390,7 +388,7 @@ def setup() -> ConnectionPair:
390388

391389
# Disabled by 100 Continue
392390
p = setup()
393-
p.send(SERVER, InformationalResponse(status_code=100, headers=[])) # type: ignore[arg-type]
391+
p.send(SERVER, InformationalResponse(status_code=100, headers=[]))
394392
for conn in p.conns:
395393
assert not conn.client_is_waiting_for_100_continue
396394
assert not conn.they_are_waiting_for_100_continue
@@ -471,7 +469,7 @@ def test_max_incomplete_event_size_countermeasure() -> None:
471469
# Even more data comes in, still no problem
472470
c.receive_data(b"X" * 1000)
473471
# We can respond and reuse to get the second pipelined request
474-
c.send(Response(status_code=200, headers=[])) # type: ignore[arg-type]
472+
c.send(Response(status_code=200, headers=[]))
475473
c.send(EndOfMessage())
476474
c.start_next_cycle()
477475
assert get_all_events(c) == [
@@ -481,7 +479,7 @@ def test_max_incomplete_event_size_countermeasure() -> None:
481479
# But once we unpause and try to read the next message, and find that it's
482480
# incomplete and the buffer is *still* way too large, then *that's* a
483481
# problem:
484-
c.send(Response(status_code=200, headers=[])) # type: ignore[arg-type]
482+
c.send(Response(status_code=200, headers=[]))
485483
c.send(EndOfMessage())
486484
c.start_next_cycle()
487485
with pytest.raises(RemoteProtocolError):
@@ -547,7 +545,7 @@ def test_pipelining() -> None:
547545

548546
assert c.next_event() is PAUSED
549547

550-
c.send(Response(status_code=200, headers=[])) # type: ignore[arg-type]
548+
c.send(Response(status_code=200, headers=[]))
551549
c.send(EndOfMessage())
552550
assert c.their_state is DONE
553551
assert c.our_state is DONE
@@ -564,7 +562,7 @@ def test_pipelining() -> None:
564562
EndOfMessage(),
565563
]
566564
assert c.next_event() is PAUSED
567-
c.send(Response(status_code=200, headers=[])) # type: ignore[arg-type]
565+
c.send(Response(status_code=200, headers=[]))
568566
c.send(EndOfMessage())
569567
c.start_next_cycle()
570568

@@ -574,7 +572,7 @@ def test_pipelining() -> None:
574572
]
575573
# Doesn't pause this time, no trailing data
576574
assert c.next_event() is NEED_DATA
577-
c.send(Response(status_code=200, headers=[])) # type: ignore[arg-type]
575+
c.send(Response(status_code=200, headers=[]))
578576
c.send(EndOfMessage())
579577

580578
# Arrival of more data triggers pause
@@ -683,7 +681,7 @@ def setup() -> ConnectionPair:
683681
sc.send(EndOfMessage())
684682
sc.start_next_cycle()
685683
assert get_all_events(sc) == [
686-
Request(method="GET", target="/", headers=[], http_version="1.0"), # type: ignore[arg-type]
684+
Request(method="GET", target="/", headers=[], http_version="1.0"),
687685
EndOfMessage(),
688686
]
689687

@@ -845,7 +843,7 @@ def test_pipelined_close() -> None:
845843
EndOfMessage(),
846844
]
847845
assert c.states[CLIENT] is DONE
848-
c.send(Response(status_code=200, headers=[])) # type: ignore[arg-type]
846+
c.send(Response(status_code=200, headers=[]))
849847
c.send(EndOfMessage())
850848
assert c.states[SERVER] is DONE
851849
c.start_next_cycle()
@@ -860,7 +858,7 @@ def test_pipelined_close() -> None:
860858
ConnectionClosed(),
861859
]
862860
assert c.states == {CLIENT: CLOSED, SERVER: SEND_RESPONSE}
863-
c.send(Response(status_code=200, headers=[])) # type: ignore[arg-type]
861+
c.send(Response(status_code=200, headers=[]))
864862
c.send(EndOfMessage())
865863
assert c.states == {CLIENT: CLOSED, SERVER: MUST_CLOSE}
866864
c.send(ConnectionClosed())
@@ -919,7 +917,7 @@ def test_errors() -> None:
919917
# But we can still yell at the client for sending us gibberish
920918
if role is SERVER:
921919
assert (
922-
c.send(Response(status_code=400, headers=[])) # type: ignore[arg-type]
920+
c.send(Response(status_code=400, headers=[]))
923921
== b"HTTP/1.1 400 \r\nConnection: close\r\n\r\n"
924922
)
925923

@@ -946,8 +944,8 @@ def conn(role: Type[Sentinel]) -> Connection:
946944
http_version="1.0",
947945
)
948946
elif role is SERVER:
949-
good = Response(status_code=200, headers=[]) # type: ignore[arg-type,assignment]
950-
bad = Response(status_code=200, headers=[], http_version="1.0") # type: ignore[arg-type,assignment]
947+
good = Response(status_code=200, headers=[]) # type: ignore[assignment]
948+
bad = Response(status_code=200, headers=[], http_version="1.0") # type: ignore[assignment]
951949
# Make sure 'good' actually is good
952950
c = conn(role)
953951
c.send(good)
@@ -1063,14 +1061,14 @@ def setup(method: bytes, http_version: bytes) -> Connection:
10631061
# No Content-Length, HTTP/1.1 peer, should use chunked
10641062
c = setup(method, b"1.1")
10651063
assert (
1066-
c.send(Response(status_code=200, headers=[])) == b"HTTP/1.1 200 \r\n" # type: ignore[arg-type]
1064+
c.send(Response(status_code=200, headers=[])) == b"HTTP/1.1 200 \r\n"
10671065
b"Transfer-Encoding: chunked\r\n\r\n"
10681066
)
10691067

10701068
# No Content-Length, HTTP/1.0 peer, frame with connection: close
10711069
c = setup(method, b"1.0")
10721070
assert (
1073-
c.send(Response(status_code=200, headers=[])) == b"HTTP/1.1 200 \r\n" # type: ignore[arg-type]
1071+
c.send(Response(status_code=200, headers=[])) == b"HTTP/1.1 200 \r\n"
10741072
b"Connection: close\r\n\r\n"
10751073
)
10761074

h11/tests/test_events.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
import pytest
44

5-
from .. import _events
65
from .._events import (
76
ConnectionClosed,
87
Data,
98
EndOfMessage,
10-
Event,
119
InformationalResponse,
1210
Request,
1311
Response,
@@ -101,13 +99,13 @@ def test_events() -> None:
10199
with pytest.raises(LocalProtocolError):
102100
InformationalResponse(status_code=200, headers=[("Host", "a")])
103101

104-
resp = Response(status_code=204, headers=[], http_version="1.0") # type: ignore[arg-type]
102+
resp = Response(status_code=204, headers=[], http_version="1.0")
105103
assert resp.status_code == 204
106104
assert resp.headers == []
107105
assert resp.http_version == b"1.0"
108106

109107
with pytest.raises(LocalProtocolError):
110-
resp = Response(status_code=100, headers=[], http_version="1.0") # type: ignore[arg-type]
108+
resp = Response(status_code=100, headers=[], http_version="1.0")
111109

112110
with pytest.raises(LocalProtocolError):
113111
Response(status_code="100", headers=[], http_version="1.0") # type: ignore[arg-type]
@@ -128,7 +126,7 @@ def test_events() -> None:
128126
def test_intenum_status_code() -> None:
129127
# https://github.com/python-hyper/h11/issues/72
130128

131-
r = Response(status_code=HTTPStatus.OK, headers=[], http_version="1.0") # type: ignore[arg-type]
129+
r = Response(status_code=HTTPStatus.OK, headers=[], http_version="1.0")
132130
assert r.status_code == HTTPStatus.OK
133131
assert type(r.status_code) is not type(HTTPStatus.OK)
134132
assert type(r.status_code) is int

h11/tests/test_helpers.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
from .._events import (
2-
ConnectionClosed,
3-
Data,
4-
EndOfMessage,
5-
Event,
6-
InformationalResponse,
7-
Request,
8-
Response,
9-
)
1+
from .._events import Data, EndOfMessage, Response
102
from .helpers import normalize_data_events
113

124

@@ -15,7 +7,7 @@ def test_normalize_data_events() -> None:
157
[
168
Data(data=bytearray(b"1")),
179
Data(data=b"2"),
18-
Response(status_code=200, headers=[]), # type: ignore[arg-type]
10+
Response(status_code=200, headers=[]),
1911
Data(data=b"3"),
2012
Data(data=b"4"),
2113
EndOfMessage(),
@@ -25,7 +17,7 @@ def test_normalize_data_events() -> None:
2517
]
2618
) == [
2719
Data(data=b"12"),
28-
Response(status_code=200, headers=[]), # type: ignore[arg-type]
20+
Response(status_code=200, headers=[]),
2921
Data(data=b"34"),
3022
EndOfMessage(),
3123
Data(data=b"567"),

0 commit comments

Comments
 (0)