13
13
import binascii
14
14
import sys
15
15
from dataclasses import dataclass
16
- from typing import TYPE_CHECKING
16
+ from typing import Any , TYPE_CHECKING
17
17
18
18
from .settings import ChangedSetting , SettingCodes , Settings , _setting_code_from_int
19
19
30
30
kw_only = {"kw_only" : True }
31
31
32
32
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
+
33
42
class Event :
34
43
"""
35
44
Base class for h2 events.
@@ -258,6 +267,7 @@ def __repr__(self) -> str:
258
267
return f"<InformationalResponseReceived stream_id:{ self .stream_id } , headers:{ self .headers } >"
259
268
260
269
270
+ @dataclass (** kw_only )
261
271
class DataReceived (Event ):
262
272
"""
263
273
The DataReceived event is fired whenever data is received on a stream from
@@ -268,25 +278,28 @@ class DataReceived(Event):
268
278
Added ``stream_ended`` property.
269
279
"""
270
280
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."""
274
286
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
+ """
277
294
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.
283
300
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
+ """
290
303
291
304
def __repr__ (self ) -> str :
292
305
return (
0 commit comments