1
1
import asyncio
2
2
import sys
3
3
4
+ from types import TracebackType
5
+ from typing import Optional , Type
4
6
5
- __version__ = '2.0.1'
7
+
8
+ __version__ = '3.0.0'
6
9
7
10
PY_37 = sys .version_info >= (3 , 7 )
8
11
@@ -21,42 +24,48 @@ class timeout:
21
24
timeout - value in seconds or None to disable timeout logic
22
25
loop - asyncio compatible event loop
23
26
"""
24
- def __init__ (self , timeout , * , loop = None ):
27
+ def __init__ (self , timeout : Optional [float ],
28
+ * , loop : asyncio .AbstractEventLoop = None ) -> None :
25
29
self ._timeout = timeout
26
30
if loop is None :
27
31
loop = asyncio .get_event_loop ()
28
32
self ._loop = loop
29
- self ._task = None
33
+ self ._task = None # type: Optional[asyncio.Task]
30
34
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]
33
37
34
- def __enter__ (self ):
38
+ def __enter__ (self ) -> 'timeout' :
35
39
return self ._do_enter ()
36
40
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 ]:
38
45
self ._do_exit (exc_type )
46
+ return None
39
47
40
- @asyncio .coroutine
41
- def __aenter__ (self ):
48
+ async def __aenter__ (self ) -> 'timeout' :
42
49
return self ._do_enter ()
43
50
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 :
46
55
self ._do_exit (exc_type )
47
56
48
57
@property
49
- def expired (self ):
58
+ def expired (self ) -> bool :
50
59
return self ._cancelled
51
60
52
61
@property
53
- def remaining (self ):
62
+ def remaining (self ) -> Optional [ float ] :
54
63
if self ._cancel_at is not None :
55
64
return max (self ._cancel_at - self ._loop .time (), 0.0 )
56
65
else :
57
66
return None
58
67
59
- def _do_enter (self ):
68
+ def _do_enter (self ) -> 'timeout' :
60
69
# Support Tornado 5- without timeout
61
70
# Details: https://github.com/python/asyncio/issues/392
62
71
if self ._timeout is None :
@@ -76,7 +85,7 @@ def _do_enter(self):
76
85
self ._cancel_at , self ._cancel_task )
77
86
return self
78
87
79
- def _do_exit (self , exc_type ) :
88
+ def _do_exit (self , exc_type : Type [ BaseException ]) -> None :
80
89
if exc_type is asyncio .CancelledError and self ._cancelled :
81
90
self ._cancel_handler = None
82
91
self ._task = None
@@ -85,20 +94,22 @@ def _do_exit(self, exc_type):
85
94
self ._cancel_handler .cancel ()
86
95
self ._cancel_handler = None
87
96
self ._task = None
97
+ return None
88
98
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
92
103
93
104
94
- def current_task (loop ) :
105
+ def current_task (loop : asyncio . AbstractEventLoop ) -> asyncio . Task :
95
106
if PY_37 :
96
- task = asyncio .current_task (loop = loop )
107
+ task = asyncio .current_task (loop = loop ) # type: ignore
97
108
else :
98
109
task = asyncio .Task .current_task (loop = loop )
99
110
if task is None :
100
111
# this should be removed, tokio must use register_task and family API
101
112
if hasattr (loop , 'current_task' ):
102
- task = loop .current_task ()
113
+ task = loop .current_task () # type: ignore
103
114
104
115
return task
0 commit comments