2424import asyncio
2525import sys
2626from types import TracebackType
27- from typing import Any , Callable , Coroutine , Optional , Type , Union
27+ from typing import Any , Callable , Coroutine , Optional , Union
2828
29- from aenum import Enum
29+ from aenum import Enum # type: ignore
3030from aiohttp import ClientResponse , ClientResponseError
3131from aiohttp import ClientSession as AioHttpClientSession
3232from aiohttp import ServerConnectionError
@@ -77,31 +77,33 @@ def __init__(
7777 self .__retry_number = max_retry_number
7878 self .__retry_delay = base_retry_delay
7979
80- async def __nothing (self ):
80+ async def __nothing (self ) -> None :
8181 pass
8282
83- def __sleep (self , retry_num : int , retry_factor : int ) -> Coroutine :
84- if retry_num > 0 : # don't wait at the first retry attempt
83+ def __sleep (self , retry_num : int , retry_factor : Optional [ int ] ) -> Coroutine :
84+ if retry_num > 0 and retry_factor is not None : # don't wait at the first retry attempt
8585 delay = (((retry_factor * self .__retry_delay ) * 1000 ) ** retry_num ) / 1000
8686 return asyncio .sleep (delay )
8787 else :
8888 return self .__nothing ()
8989
90- async def __request (self , method : Callable , url , ** kwargs : Any ) -> ClientResponse :
90+ async def __request (
91+ self , method : Callable [..., Coroutine [Any , Any , ClientResponse ]], url : str , ** kwargs : Any
92+ ) -> ClientResponse :
9193 """Make a request and retry if necessary.
9294
9395 The method retries requests depending on error class and retry number. For no-retry errors, such as
9496 400 Bad Request it just returns result, for cases where it's reasonable to retry it does it in
9597 exponential manner.
9698 """
97- result = None
99+ result : Optional [ ClientResponse ] = None
98100 exceptions = []
99101
100102 for i in range (self .__retry_number + 1 ): # add one for the first attempt, which is not a retry
101- retry_factor = None
103+ retry_factor : Optional [ int ] = None
102104 if result is not None :
103105 # Release previous result to return connection to pool
104- await result .release ()
106+ result .release ()
105107 try :
106108 result = await method (url , ** kwargs )
107109 except Exception as exc :
@@ -136,6 +138,8 @@ async def __request(self, method: Callable, url, **kwargs: Any) -> ClientRespons
136138 raise exceptions [- 1 ]
137139 else :
138140 raise exceptions [0 ]
141+ if result is None :
142+ raise IOError ("Request failed without exceptions" )
139143 return result
140144
141145 def get (self , url : str , * , allow_redirects : bool = True , ** kwargs : Any ) -> Coroutine [Any , Any , ClientResponse ]:
@@ -150,7 +154,7 @@ def put(self, url: str, *, data: Any = None, **kwargs: Any) -> Coroutine[Any, An
150154 """Perform HTTP PUT request."""
151155 return self .__request (self ._client .put , url , data = data , ** kwargs )
152156
153- def close (self ) -> Coroutine :
157+ def close (self ) -> Coroutine [ None , None , None ] :
154158 """Gracefully close internal aiohttp.ClientSession class instance."""
155159 return self ._client .close ()
156160
@@ -160,7 +164,7 @@ async def __aenter__(self) -> "RetryingClientSession":
160164
161165 async def __aexit__ (
162166 self ,
163- exc_type : Optional [Type [BaseException ]],
167+ exc_type : Optional [type [BaseException ]],
164168 exc_val : Optional [BaseException ],
165169 exc_tb : Optional [TracebackType ],
166170 ) -> None :
@@ -241,7 +245,7 @@ async def __aenter__(self) -> "ClientSession":
241245
242246 async def __aexit__ (
243247 self ,
244- exc_type : Optional [Type [BaseException ]],
248+ exc_type : Optional [type [BaseException ]],
245249 exc_val : Optional [BaseException ],
246250 exc_tb : Optional [TracebackType ],
247251 ) -> None :
0 commit comments