16
16
from ..lowlevel import checkpoint
17
17
18
18
T_Item = TypeVar ("T_Item" )
19
+ T_co = TypeVar ("T_co" , covariant = True )
20
+ T_contra = TypeVar ("T_contra" , contravariant = True )
19
21
20
22
21
23
class MemoryObjectStreamStatistics (NamedTuple ):
@@ -55,14 +57,14 @@ def statistics(self) -> MemoryObjectStreamStatistics:
55
57
56
58
57
59
@dataclass (eq = False )
58
- class MemoryObjectReceiveStream (Generic [T_Item ], ObjectReceiveStream [T_Item ]):
59
- _state : MemoryObjectStreamState [T_Item ]
60
+ class MemoryObjectReceiveStream (Generic [T_co ], ObjectReceiveStream [T_co ]):
61
+ _state : MemoryObjectStreamState [T_co ]
60
62
_closed : bool = field (init = False , default = False )
61
63
62
64
def __post_init__ (self ) -> None :
63
65
self ._state .open_receive_channels += 1
64
66
65
- def receive_nowait (self ) -> T_Item :
67
+ def receive_nowait (self ) -> T_co :
66
68
"""
67
69
Receive the next item if it can be done without waiting.
68
70
@@ -90,14 +92,14 @@ def receive_nowait(self) -> T_Item:
90
92
91
93
raise WouldBlock
92
94
93
- async def receive (self ) -> T_Item :
95
+ async def receive (self ) -> T_co :
94
96
await checkpoint ()
95
97
try :
96
98
return self .receive_nowait ()
97
99
except WouldBlock :
98
100
# Add ourselves in the queue
99
101
receive_event = Event ()
100
- container : list [T_Item ] = []
102
+ container : list [T_co ] = []
101
103
self ._state .waiting_receivers [receive_event ] = container
102
104
103
105
try :
@@ -115,7 +117,7 @@ async def receive(self) -> T_Item:
115
117
else :
116
118
raise EndOfStream
117
119
118
- def clone (self ) -> MemoryObjectReceiveStream [T_Item ]:
120
+ def clone (self ) -> MemoryObjectReceiveStream [T_co ]:
119
121
"""
120
122
Create a clone of this receive stream.
121
123
@@ -157,7 +159,7 @@ def statistics(self) -> MemoryObjectStreamStatistics:
157
159
"""
158
160
return self ._state .statistics ()
159
161
160
- def __enter__ (self ) -> MemoryObjectReceiveStream [T_Item ]:
162
+ def __enter__ (self ) -> MemoryObjectReceiveStream [T_co ]:
161
163
return self
162
164
163
165
def __exit__ (
@@ -170,14 +172,14 @@ def __exit__(
170
172
171
173
172
174
@dataclass (eq = False )
173
- class MemoryObjectSendStream (Generic [T_Item ], ObjectSendStream [T_Item ]):
174
- _state : MemoryObjectStreamState [T_Item ]
175
+ class MemoryObjectSendStream (Generic [T_contra ], ObjectSendStream [T_contra ]):
176
+ _state : MemoryObjectStreamState [T_contra ]
175
177
_closed : bool = field (init = False , default = False )
176
178
177
179
def __post_init__ (self ) -> None :
178
180
self ._state .open_send_channels += 1
179
181
180
- def send_nowait (self , item : T_Item ) -> None :
182
+ def send_nowait (self , item : T_contra ) -> None :
181
183
"""
182
184
Send an item immediately if it can be done without waiting.
183
185
@@ -203,7 +205,7 @@ def send_nowait(self, item: T_Item) -> None:
203
205
else :
204
206
raise WouldBlock
205
207
206
- async def send (self , item : T_Item ) -> None :
208
+ async def send (self , item : T_contra ) -> None :
207
209
"""
208
210
Send an item to the stream.
209
211
@@ -236,7 +238,7 @@ async def send(self, item: T_Item) -> None:
236
238
):
237
239
raise BrokenResourceError
238
240
239
- def clone (self ) -> MemoryObjectSendStream [T_Item ]:
241
+ def clone (self ) -> MemoryObjectSendStream [T_contra ]:
240
242
"""
241
243
Create a clone of this send stream.
242
244
@@ -279,7 +281,7 @@ def statistics(self) -> MemoryObjectStreamStatistics:
279
281
"""
280
282
return self ._state .statistics ()
281
283
282
- def __enter__ (self ) -> MemoryObjectSendStream [T_Item ]:
284
+ def __enter__ (self ) -> MemoryObjectSendStream [T_contra ]:
283
285
return self
284
286
285
287
def __exit__ (
0 commit comments