Skip to content

Commit 6892580

Browse files
committed
convert events.RequestReceived into a dataclass
1 parent f7c4c53 commit 6892580

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

src/h2/events.py

+21-18
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Event:
4545
"""
4646

4747

48-
48+
@dataclass(**kw_only)
4949
class RequestReceived(Event):
5050
"""
5151
The RequestReceived event is fired whenever all of a request's headers
@@ -64,26 +64,29 @@ class RequestReceived(Event):
6464
Added ``stream_ended`` and ``priority_updated`` properties.
6565
"""
6666

67-
def __init__(self) -> None:
68-
#: The Stream ID for the stream this request was made on.
69-
self.stream_id: int | None = None
67+
stream_id: int
68+
"""The Stream ID for the stream this request was made on."""
7069

71-
#: The request headers.
72-
self.headers: list[Header] | None = None
70+
headers: list[Header] = _LAZY_INIT
71+
"""The request headers."""
7372

74-
#: If this request also ended the stream, the associated
75-
#: :class:`StreamEnded <h2.events.StreamEnded>` event will be available
76-
#: here.
77-
#:
78-
#: .. versionadded:: 2.4.0
79-
self.stream_ended: StreamEnded | None = None
73+
stream_ended: StreamEnded | None = None
74+
"""
75+
If this request also ended the stream, the associated
76+
:class:`StreamEnded <h2.events.StreamEnded>` event will be available
77+
here.
8078
81-
#: If this request also had associated priority information, the
82-
#: associated :class:`PriorityUpdated <h2.events.PriorityUpdated>`
83-
#: event will be available here.
84-
#:
85-
#: .. versionadded:: 2.4.0
86-
self.priority_updated: PriorityUpdated | None = None
79+
.. versionadded:: 2.4.0
80+
"""
81+
82+
priority_updated: PriorityUpdated | None = None
83+
"""
84+
If this request also had associated priority information, the
85+
associated :class:`PriorityUpdated <h2.events.PriorityUpdated>`
86+
event will be available here.
87+
88+
.. versionadded:: 2.4.0
89+
"""
8790

8891
def __repr__(self) -> str:
8992
return f"<RequestReceived stream_id:{self.stream_id}, headers:{self.headers}>"

src/h2/stream.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,7 @@ def request_received(self, previous_state: StreamState) -> list[Event]:
195195

196196
self.client = False
197197
self.headers_received = True
198-
event = RequestReceived()
199-
event.stream_id = self.stream_id
198+
event = RequestReceived(stream_id=self.stream_id)
200199
return [event]
201200

202201
def response_received(self, previous_state: StreamState) -> list[Event]:

tests/test_events.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,10 @@ def test_requestreceived_repr(self) -> None:
115115
"""
116116
RequestReceived has a useful debug representation.
117117
"""
118-
e = h2.events.RequestReceived()
119-
e.stream_id = 5
120-
e.headers = self.example_request_headers
118+
e = h2.events.RequestReceived(
119+
stream_id=5,
120+
headers=self.example_request_headers
121+
)
121122

122123
assert repr(e) == (
123124
"<RequestReceived stream_id:5, headers:["

0 commit comments

Comments
 (0)