1+ from __future__ import annotations
2+
13from abc import ABCMeta , abstractmethod
2- from typing import Generic , TypeVar
4+ from typing import TYPE_CHECKING , Generic , TypeVar
35
46import trio
57
8+ if TYPE_CHECKING :
9+ from types import TracebackType
10+
11+ from typing_extensions import Self
12+
613
714# We use ABCMeta instead of ABC, plus set __slots__=(), so as not to force a
815# __dict__ onto subclasses.
@@ -12,15 +19,15 @@ class Clock(metaclass=ABCMeta):
1219 __slots__ = ()
1320
1421 @abstractmethod
15- def start_clock (self ):
22+ def start_clock (self ) -> None :
1623 """Do any setup this clock might need.
1724
1825 Called at the beginning of the run.
1926
2027 """
2128
2229 @abstractmethod
23- def current_time (self ):
30+ def current_time (self ) -> float :
2431 """Return the current time, according to this clock.
2532
2633 This is used to implement functions like :func:`trio.current_time` and
@@ -32,7 +39,7 @@ def current_time(self):
3239 """
3340
3441 @abstractmethod
35- def deadline_to_sleep_time (self , deadline ) :
42+ def deadline_to_sleep_time (self , deadline : float ) -> float :
3643 """Compute the real time until the given deadline.
3744
3845 This is called before we enter a system-specific wait function like
@@ -225,7 +232,7 @@ class AsyncResource(metaclass=ABCMeta):
225232 __slots__ = ()
226233
227234 @abstractmethod
228- async def aclose (self ):
235+ async def aclose (self ) -> None :
229236 """Close this resource, possibly blocking.
230237
231238 IMPORTANT: This method may block in order to perform a "graceful"
@@ -253,10 +260,15 @@ async def aclose(self):
253260
254261 """
255262
256- async def __aenter__ (self ):
263+ async def __aenter__ (self ) -> Self :
257264 return self
258265
259- async def __aexit__ (self , * args ):
266+ async def __aexit__ (
267+ self ,
268+ exc_type : type [BaseException ] | None ,
269+ exc_value : BaseException | None ,
270+ traceback : TracebackType | None ,
271+ ) -> None :
260272 await self .aclose ()
261273
262274
@@ -279,7 +291,7 @@ class SendStream(AsyncResource):
279291 __slots__ = ()
280292
281293 @abstractmethod
282- async def send_all (self , data ) :
294+ async def send_all (self , data : bytes | bytearray | memoryview ) -> None :
283295 """Sends the given data through the stream, blocking if necessary.
284296
285297 Args:
@@ -305,7 +317,7 @@ async def send_all(self, data):
305317 """
306318
307319 @abstractmethod
308- async def wait_send_all_might_not_block (self ):
320+ async def wait_send_all_might_not_block (self ) -> None :
309321 """Block until it's possible that :meth:`send_all` might not block.
310322
311323 This method may return early: it's possible that after it returns,
@@ -385,7 +397,7 @@ class ReceiveStream(AsyncResource):
385397 __slots__ = ()
386398
387399 @abstractmethod
388- async def receive_some (self , max_bytes = None ):
400+ async def receive_some (self , max_bytes : int | None = None ) -> bytes | bytearray :
389401 """Wait until there is data available on this stream, and then return
390402 some of it.
391403
@@ -413,10 +425,10 @@ async def receive_some(self, max_bytes=None):
413425
414426 """
415427
416- def __aiter__ (self ):
428+ def __aiter__ (self ) -> Self :
417429 return self
418430
419- async def __anext__ (self ):
431+ async def __anext__ (self ) -> bytes | bytearray :
420432 data = await self .receive_some ()
421433 if not data :
422434 raise StopAsyncIteration
@@ -446,7 +458,7 @@ class HalfCloseableStream(Stream):
446458 __slots__ = ()
447459
448460 @abstractmethod
449- async def send_eof (self ):
461+ async def send_eof (self ) -> None :
450462 """Send an end-of-file indication on this stream, if possible.
451463
452464 The difference between :meth:`send_eof` and
@@ -632,7 +644,7 @@ async def receive(self) -> ReceiveType:
632644
633645 """
634646
635- def __aiter__ (self ):
647+ def __aiter__ (self ) -> Self :
636648 return self
637649
638650 async def __anext__ (self ) -> ReceiveType :
0 commit comments