Skip to content

Commit 0b98b24

Browse files
committed
tests: linting++
1 parent 9bfb877 commit 0b98b24

29 files changed

+1701
-1712
lines changed

src/h2/events.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class RequestReceived(Event):
3434
The RequestReceived event is fired whenever all of a request's headers
3535
are received. This event carries the HTTP headers for the given request
3636
and the stream ID of the new stream.
37-
37+
3838
In HTTP/2, headers may be sent as a HEADERS frame followed by zero or more
3939
CONTINUATION frames with the final frame setting the END_HEADERS flag.
4040
This event is fired after the entire sequence is received.

src/h2/utilities.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
from string import whitespace
1212
from typing import TYPE_CHECKING, Any, NamedTuple
1313

14-
from hpack.struct import Header, HeaderTuple, HeaderWeaklyTyped, NeverIndexedHeaderTuple
14+
from hpack.struct import HeaderTuple, NeverIndexedHeaderTuple
1515

1616
from .exceptions import FlowControlError, ProtocolError
1717

1818
if TYPE_CHECKING: # pragma: no cover
1919
from collections.abc import Generator, Iterable
2020

21+
from hpack.struct import Header, HeaderWeaklyTyped
22+
2123
UPPER_RE = re.compile(b"[A-Z]")
2224
SIGIL = ord(b":")
2325
INFORMATIONAL_START = ord(b"1")

tests/conftest.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import pytest
24

35
from . import helpers

tests/coroutine_tests.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
"""
2-
coroutine_tests
3-
~~~~~~~~~~~~~~~
4-
52
This file gives access to a coroutine-based test class. This allows each test
63
case to be defined as a pair of interacting coroutines, sending data to each
74
other by yielding the flow of control.
@@ -12,13 +9,15 @@
129
makes them behave identically on all platforms, as well as ensuring they both
1310
succeed and fail quickly.
1411
"""
15-
import itertools
12+
from __future__ import annotations
13+
1614
import functools
15+
import itertools
1716

1817
import pytest
1918

2019

21-
class CoroutineTestCase(object):
20+
class CoroutineTestCase:
2221
"""
2322
A base class for tests that use interacting coroutines.
2423
@@ -28,7 +27,8 @@ class CoroutineTestCase(object):
2827
its first action is to receive data), the calling code should prime it by
2928
using the 'server' decorator on this class.
3029
"""
31-
def run_until_complete(self, *coroutines):
30+
31+
def run_until_complete(self, *coroutines) -> None:
3232
"""
3333
Executes a set of coroutines that communicate between each other. Each
3434
one is, in order, passed the output of the previous coroutine until
@@ -55,7 +55,7 @@ def run_until_complete(self, *coroutines):
5555
except StopIteration:
5656
continue
5757
else:
58-
pytest.fail("Coroutine %s not exhausted" % coro)
58+
pytest.fail(f"Coroutine {coro} not exhausted")
5959

6060
def server(self, func):
6161
"""

tests/h2spectest.sh

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env bash
2+
23
# A test script that runs the example Python Twisted server and then runs
34
# h2spec against it. Prints the output of h2spec. This script does not expect
45
# to be run directly, but instead via `tox -e h2spec`.

tests/helpers.py

+36-23
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
"""
2-
helpers
3-
~~~~~~~
4-
5-
This module contains helpers for the h2 tests.
2+
Helper module for the h2 tests.
63
"""
4+
from __future__ import annotations
5+
6+
from hpack.hpack import Encoder
77
from hyperframe.frame import (
8-
HeadersFrame, DataFrame, SettingsFrame, WindowUpdateFrame, PingFrame,
9-
GoAwayFrame, RstStreamFrame, PushPromiseFrame, PriorityFrame,
10-
ContinuationFrame, AltSvcFrame
8+
AltSvcFrame,
9+
ContinuationFrame,
10+
DataFrame,
11+
GoAwayFrame,
12+
HeadersFrame,
13+
PingFrame,
14+
PriorityFrame,
15+
PushPromiseFrame,
16+
RstStreamFrame,
17+
SettingsFrame,
18+
WindowUpdateFrame,
1119
)
12-
from hpack.hpack import Encoder
13-
1420

1521
SAMPLE_SETTINGS = {
1622
SettingsFrame.HEADER_TABLE_SIZE: 4096,
@@ -19,32 +25,35 @@
1925
}
2026

2127

22-
class FrameFactory(object):
28+
class FrameFactory:
2329
"""
2430
A class containing lots of helper methods and state to build frames. This
2531
allows test cases to easily build correct HTTP/2 frames to feed to
2632
hyper-h2.
2733
"""
28-
def __init__(self):
34+
35+
def __init__(self) -> None:
2936
self.encoder = Encoder()
3037

31-
def refresh_encoder(self):
38+
def refresh_encoder(self) -> None:
3239
self.encoder = Encoder()
3340

34-
def preamble(self):
35-
return b'PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n'
41+
def preamble(self) -> bytes:
42+
return b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
3643

3744
def build_headers_frame(self,
3845
headers,
39-
flags=[],
46+
flags=None,
4047
stream_id=1,
4148
**priority_kwargs):
4249
"""
4350
Builds a single valid headers frame out of the contained headers.
4451
"""
52+
if flags is None:
53+
flags = []
4554
f = HeadersFrame(stream_id)
4655
f.data = self.encoder.encode(headers)
47-
f.flags.add('END_HEADERS')
56+
f.flags.add("END_HEADERS")
4857
for flag in flags:
4958
f.flags.add(flag)
5059

@@ -53,10 +62,12 @@ def build_headers_frame(self,
5362

5463
return f
5564

56-
def build_continuation_frame(self, header_block, flags=[], stream_id=1):
65+
def build_continuation_frame(self, header_block, flags=None, stream_id=1):
5766
"""
5867
Builds a single continuation frame out of the binary header block.
5968
"""
69+
if flags is None:
70+
flags = []
6071
f = ContinuationFrame(stream_id)
6172
f.data = header_block
6273
f.flags = set(flags)
@@ -73,7 +84,7 @@ def build_data_frame(self, data, flags=None, stream_id=1, padding_len=0):
7384
f.flags = flags
7485

7586
if padding_len:
76-
flags.add('PADDED')
87+
flags.add("PADDED")
7788
f.pad_length = padding_len
7889

7990
return f
@@ -84,7 +95,7 @@ def build_settings_frame(self, settings, ack=False):
8495
"""
8596
f = SettingsFrame(0)
8697
if ack:
87-
f.flags.add('ACK')
98+
f.flags.add("ACK")
8899

89100
f.settings = settings
90101
return f
@@ -111,7 +122,7 @@ def build_ping_frame(self, ping_data, flags=None):
111122
def build_goaway_frame(self,
112123
last_stream_id,
113124
error_code=0,
114-
additional_data=b''):
125+
additional_data=b""):
115126
"""
116127
Builds a single GOAWAY frame.
117128
"""
@@ -133,15 +144,17 @@ def build_push_promise_frame(self,
133144
stream_id,
134145
promised_stream_id,
135146
headers,
136-
flags=[]):
147+
flags=None):
137148
"""
138149
Builds a single PUSH_PROMISE frame.
139150
"""
151+
if flags is None:
152+
flags = []
140153
f = PushPromiseFrame(stream_id)
141154
f.promised_stream_id = promised_stream_id
142155
f.data = self.encoder.encode(headers)
143156
f.flags = set(flags)
144-
f.flags.add('END_HEADERS')
157+
f.flags.add("END_HEADERS")
145158
return f
146159

147160
def build_priority_frame(self,
@@ -167,7 +180,7 @@ def build_alt_svc_frame(self, stream_id, origin, field):
167180
f.field = field
168181
return f
169182

170-
def change_table_size(self, new_size):
183+
def change_table_size(self, new_size) -> None:
171184
"""
172185
Causes the encoder to send a dynamic size update in the next header
173186
block it sends.

0 commit comments

Comments
 (0)