133
133
134
134
import asyncio
135
135
from collections .abc import AsyncIterator
136
- from typing import Any , Generic , TypeGuard , TypeVar
136
+ from typing import Any , Generic , TypeGuard
137
137
138
138
from ._exceptions import Error
139
+ from ._generic import ReceiverMessageT_co
139
140
from ._receiver import Receiver , ReceiverStoppedError
140
141
141
- _T = TypeVar ("_T" )
142
-
143
142
144
143
class _EmptyResult :
145
144
"""A sentinel value to distinguish between None and empty result.
@@ -152,7 +151,7 @@ def __repr__(self) -> str:
152
151
return "<empty>"
153
152
154
153
155
- class Selected (Generic [_T ]):
154
+ class Selected (Generic [ReceiverMessageT_co ]):
156
155
"""A result of a [`select()`][frequenz.channels.select] iteration.
157
156
158
157
The selected receiver is consumed immediately and the received message is stored in
@@ -166,7 +165,7 @@ class Selected(Generic[_T]):
166
165
Please see [`select()`][frequenz.channels.select] for an example.
167
166
"""
168
167
169
- def __init__ (self , receiver : Receiver [_T ], / ) -> None :
168
+ def __init__ (self , receiver : Receiver [ReceiverMessageT_co ], / ) -> None :
170
169
"""Initialize this selected result.
171
170
172
171
The receiver is consumed immediately when creating the instance and the received
@@ -178,10 +177,10 @@ def __init__(self, receiver: Receiver[_T], /) -> None:
178
177
Args:
179
178
receiver: The receiver that was selected.
180
179
"""
181
- self ._recv : Receiver [_T ] = receiver
180
+ self ._recv : Receiver [ReceiverMessageT_co ] = receiver
182
181
"""The receiver that was selected."""
183
182
184
- self ._message : _T | _EmptyResult = _EmptyResult ()
183
+ self ._message : ReceiverMessageT_co | _EmptyResult = _EmptyResult ()
185
184
"""The message that was received.
186
185
187
186
If there was an exception while receiving the message, then this will be `None`.
@@ -198,7 +197,7 @@ def __init__(self, receiver: Receiver[_T], /) -> None:
198
197
"""Flag to indicate if this selected has been handled in the if-chain."""
199
198
200
199
@property
201
- def message (self ) -> _T :
200
+ def message (self ) -> ReceiverMessageT_co :
202
201
"""The message that was received, if any.
203
202
204
203
Returns:
@@ -248,8 +247,8 @@ def __repr__(self) -> str:
248
247
# `TypeGuard`s can't be used as methods. For more information see:
249
248
# https://github.com/microsoft/pyright/discussions/3125
250
249
def selected_from (
251
- selected : Selected [Any ], receiver : Receiver [_T ]
252
- ) -> TypeGuard [Selected [_T ]]:
250
+ selected : Selected [Any ], receiver : Receiver [ReceiverMessageT_co ]
251
+ ) -> TypeGuard [Selected [ReceiverMessageT_co ]]:
253
252
"""Check whether the given receiver was selected by [`select()`][frequenz.channels.select].
254
253
255
254
This function is used in conjunction with the
@@ -283,23 +282,23 @@ class SelectError(Error):
283
282
"""
284
283
285
284
286
- class UnhandledSelectedError (SelectError , Generic [_T ]):
285
+ class UnhandledSelectedError (SelectError , Generic [ReceiverMessageT_co ]):
287
286
"""A receiver was not handled in a [`select()`][frequenz.channels.select] iteration.
288
287
289
288
This exception is raised when a [`select()`][frequenz.channels.select] iteration
290
289
finishes without a call to [`selected_from()`][frequenz.channels.selected_from] for
291
290
the selected receiver.
292
291
"""
293
292
294
- def __init__ (self , selected : Selected [_T ]) -> None :
293
+ def __init__ (self , selected : Selected [ReceiverMessageT_co ]) -> None :
295
294
"""Initialize this error.
296
295
297
296
Args:
298
297
selected: The selected receiver that was not handled.
299
298
"""
300
299
recv = selected ._recv # pylint: disable=protected-access
301
300
super ().__init__ (f"Selected receiver { recv } was not handled in the if-chain" )
302
- self .selected : Selected [_T ] = selected
301
+ self .selected : Selected [ReceiverMessageT_co ] = selected
303
302
"""The selected receiver that was not handled."""
304
303
305
304
0 commit comments