1313import binascii
1414import sys
1515from dataclasses import dataclass
16- from typing import TYPE_CHECKING
16+ from typing import Any , TYPE_CHECKING
1717
1818from .settings import ChangedSetting , SettingCodes , Settings , _setting_code_from_int
1919
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+
3342class 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 )
261271class 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 (
0 commit comments