Skip to content

Commit e40bfe7

Browse files
committed
convert events.DataReceived into a dataclass
1 parent 8d64bfc commit e40bfe7

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

src/h2/events.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import binascii
1414
import sys
1515
from dataclasses import dataclass
16-
from typing import TYPE_CHECKING
16+
from typing import Any, TYPE_CHECKING
1717

1818
from .settings import ChangedSetting, SettingCodes, Settings, _setting_code_from_int
1919

@@ -30,6 +30,15 @@
3030
kw_only = {"kw_only": True}
3131

3232

33+
_LAZY_INIT: Any = object()
34+
"""
35+
Some h2 events are instantiated by the state machine, but its attributes are
36+
subsequently populated by H2Stream. To make this work with strict type annotations
37+
on the events, they are temporarily set to this placeholder value.
38+
This value should never be exposed to users.
39+
"""
40+
41+
3342
class Event:
3443
"""
3544
Base class for h2 events.
@@ -258,6 +267,7 @@ def __repr__(self) -> str:
258267
return f"<InformationalResponseReceived stream_id:{self.stream_id}, headers:{self.headers}>"
259268

260269

270+
@dataclass(**kw_only)
261271
class DataReceived(Event):
262272
"""
263273
The DataReceived event is fired whenever data is received on a stream from
@@ -268,25 +278,28 @@ class DataReceived(Event):
268278
Added ``stream_ended`` property.
269279
"""
270280

271-
def __init__(self) -> None:
272-
#: The Stream ID for the stream this data was received on.
273-
self.stream_id: int | None = None
281+
stream_id: int
282+
"""The Stream ID for the stream this data was received on."""
283+
284+
data: bytes = _LAZY_INIT
285+
"""The data itself."""
274286

275-
#: The data itself.
276-
self.data: bytes | None = None
287+
flow_controlled_length: int = _LAZY_INIT
288+
"""
289+
The amount of data received that counts against the flow control
290+
window. Note that padding counts against the flow control window, so
291+
when adjusting flow control you should always use this field rather
292+
than ``len(data)``.
293+
"""
277294

278-
#: The amount of data received that counts against the flow control
279-
#: window. Note that padding counts against the flow control window, so
280-
#: when adjusting flow control you should always use this field rather
281-
#: than ``len(data)``.
282-
self.flow_controlled_length: int | None = None
295+
stream_ended: StreamEnded | None = None
296+
"""
297+
If this data chunk also completed the stream, the associated
298+
:class:`StreamEnded <h2.events.StreamEnded>` event will be available
299+
here.
283300
284-
#: If this data chunk also completed the stream, the associated
285-
#: :class:`StreamEnded <h2.events.StreamEnded>` event will be available
286-
#: here.
287-
#:
288-
#: .. versionadded:: 2.4.0
289-
self.stream_ended: StreamEnded | None = None
301+
.. versionadded:: 2.4.0
302+
"""
290303

291304
def __repr__(self) -> str:
292305
return (

src/h2/stream.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ def data_received(self, previous_state: StreamState) -> list[Event]:
224224
if not self.headers_received:
225225
msg = "cannot receive data before headers"
226226
raise ProtocolError(msg)
227-
event = DataReceived()
228-
event.stream_id = self.stream_id
227+
event = DataReceived(stream_id=self.stream_id)
229228
return [event]
230229

231230
def window_updated(self, previous_state: StreamState) -> list[Event]:

tests/test_events.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,11 @@ def test_datareceived_repr(self) -> None:
172172
"""
173173
DataReceived has a useful debug representation.
174174
"""
175-
e = h2.events.DataReceived()
176-
e.stream_id = 888
177-
e.data = b"abcdefghijklmnopqrstuvwxyz"
178-
e.flow_controlled_length = 88
175+
e = h2.events.DataReceived(
176+
stream_id=888,
177+
data=b"abcdefghijklmnopqrstuvwxyz",
178+
flow_controlled_length=88,
179+
)
179180

180181
assert repr(e) == (
181182
"<DataReceived stream_id:888, flow_controlled_length:88, "

0 commit comments

Comments
 (0)