11import asyncio
22import sys
33
4+ from types import TracebackType
5+ from typing import Optional , Type
46
5- __version__ = '2.0.1'
7+
8+ __version__ = '3.0.0'
69
710PY_37 = sys .version_info >= (3 , 7 )
811
@@ -21,42 +24,48 @@ class timeout:
2124 timeout - value in seconds or None to disable timeout logic
2225 loop - asyncio compatible event loop
2326 """
24- def __init__ (self , timeout , * , loop = None ):
27+ def __init__ (self , timeout : Optional [float ],
28+ * , loop : asyncio .AbstractEventLoop = None ) -> None :
2529 self ._timeout = timeout
2630 if loop is None :
2731 loop = asyncio .get_event_loop ()
2832 self ._loop = loop
29- self ._task = None
33+ self ._task = None # type: Optional[asyncio.Task]
3034 self ._cancelled = False
31- self ._cancel_handler = None
32- self ._cancel_at = None
35+ self ._cancel_handler = None # type: Optional[asyncio.Handle]
36+ self ._cancel_at = None # type: Optional[float]
3337
34- def __enter__ (self ):
38+ def __enter__ (self ) -> 'timeout' :
3539 return self ._do_enter ()
3640
37- def __exit__ (self , exc_type , exc_val , exc_tb ):
41+ def __exit__ (self ,
42+ exc_type : Type [BaseException ],
43+ exc_val : BaseException ,
44+ exc_tb : TracebackType ) -> Optional [bool ]:
3845 self ._do_exit (exc_type )
46+ return None
3947
40- @asyncio .coroutine
41- def __aenter__ (self ):
48+ async def __aenter__ (self ) -> 'timeout' :
4249 return self ._do_enter ()
4350
44- @asyncio .coroutine
45- def __aexit__ (self , exc_type , exc_val , exc_tb ):
51+ async def __aexit__ (self ,
52+ exc_type : Type [BaseException ],
53+ exc_val : BaseException ,
54+ exc_tb : TracebackType ) -> None :
4655 self ._do_exit (exc_type )
4756
4857 @property
49- def expired (self ):
58+ def expired (self ) -> bool :
5059 return self ._cancelled
5160
5261 @property
53- def remaining (self ):
62+ def remaining (self ) -> Optional [ float ] :
5463 if self ._cancel_at is not None :
5564 return max (self ._cancel_at - self ._loop .time (), 0.0 )
5665 else :
5766 return None
5867
59- def _do_enter (self ):
68+ def _do_enter (self ) -> 'timeout' :
6069 # Support Tornado 5- without timeout
6170 # Details: https://github.com/python/asyncio/issues/392
6271 if self ._timeout is None :
@@ -76,7 +85,7 @@ def _do_enter(self):
7685 self ._cancel_at , self ._cancel_task )
7786 return self
7887
79- def _do_exit (self , exc_type ) :
88+ def _do_exit (self , exc_type : Type [ BaseException ]) -> None :
8089 if exc_type is asyncio .CancelledError and self ._cancelled :
8190 self ._cancel_handler = None
8291 self ._task = None
@@ -85,20 +94,22 @@ def _do_exit(self, exc_type):
8594 self ._cancel_handler .cancel ()
8695 self ._cancel_handler = None
8796 self ._task = None
97+ return None
8898
89- def _cancel_task (self ):
90- self ._task .cancel ()
91- self ._cancelled = True
99+ def _cancel_task (self ) -> None :
100+ if self ._task is not None :
101+ self ._task .cancel ()
102+ self ._cancelled = True
92103
93104
94- def current_task (loop ) :
105+ def current_task (loop : asyncio . AbstractEventLoop ) -> asyncio . Task :
95106 if PY_37 :
96- task = asyncio .current_task (loop = loop )
107+ task = asyncio .current_task (loop = loop ) # type: ignore
97108 else :
98109 task = asyncio .Task .current_task (loop = loop )
99110 if task is None :
100111 # this should be removed, tokio must use register_task and family API
101112 if hasattr (loop , 'current_task' ):
102- task = loop .current_task ()
113+ task = loop .current_task () # type: ignore
103114
104115 return task
0 commit comments