@@ -234,6 +234,44 @@ def __init__(
234234 max_field_size : int = 8190 ,
235235 fallback_charset_resolver : _CharsetResolver = lambda r , b : "utf-8" ,
236236 ) -> None :
237+ # We initialise _connector to None immediately, as it's referenced in __del__()
238+ # and could cause issues if an exception occurs during initialisation.
239+ self ._connector : Optional [BaseConnector ] = None
240+ if timeout is sentinel or timeout is None :
241+ self ._timeout = DEFAULT_TIMEOUT
242+ if read_timeout is not sentinel :
243+ warnings .warn (
244+ "read_timeout is deprecated, " "use timeout argument instead" ,
245+ DeprecationWarning ,
246+ stacklevel = 2 ,
247+ )
248+ self ._timeout = attr .evolve (self ._timeout , total = read_timeout )
249+ if conn_timeout is not None :
250+ self ._timeout = attr .evolve (self ._timeout , connect = conn_timeout )
251+ warnings .warn (
252+ "conn_timeout is deprecated, " "use timeout argument instead" ,
253+ DeprecationWarning ,
254+ stacklevel = 2 ,
255+ )
256+ else :
257+ if not isinstance (timeout , ClientTimeout ):
258+ raise ValueError (
259+ f"timeout parameter cannot be of { type (timeout )} type, "
260+ "please use 'timeout=ClientTimeout(...)'" ,
261+ )
262+ self ._timeout = timeout
263+ if read_timeout is not sentinel :
264+ raise ValueError (
265+ "read_timeout and timeout parameters "
266+ "conflict, please setup "
267+ "timeout.read"
268+ )
269+ if conn_timeout is not None :
270+ raise ValueError (
271+ "conn_timeout and timeout parameters "
272+ "conflict, please setup "
273+ "timeout.connect"
274+ )
237275 if loop is None :
238276 if connector is not None :
239277 loop = connector ._loop
@@ -271,41 +309,6 @@ def __init__(
271309 self ._default_auth = auth
272310 self ._version = version
273311 self ._json_serialize = json_serialize
274- if timeout is sentinel or timeout is None :
275- self ._timeout = DEFAULT_TIMEOUT
276- if read_timeout is not sentinel :
277- warnings .warn (
278- "read_timeout is deprecated, " "use timeout argument instead" ,
279- DeprecationWarning ,
280- stacklevel = 2 ,
281- )
282- self ._timeout = attr .evolve (self ._timeout , total = read_timeout )
283- if conn_timeout is not None :
284- self ._timeout = attr .evolve (self ._timeout , connect = conn_timeout )
285- warnings .warn (
286- "conn_timeout is deprecated, " "use timeout argument instead" ,
287- DeprecationWarning ,
288- stacklevel = 2 ,
289- )
290- else :
291- if not isinstance (timeout , ClientTimeout ):
292- raise ValueError (
293- f"timeout parameter cannot be of { type (timeout )} type, "
294- "please use 'timeout=ClientTimeout(...)'" ,
295- )
296- self ._timeout = timeout
297- if read_timeout is not sentinel :
298- raise ValueError (
299- "read_timeout and timeout parameters "
300- "conflict, please setup "
301- "timeout.read"
302- )
303- if conn_timeout is not None :
304- raise ValueError (
305- "conn_timeout and timeout parameters "
306- "conflict, please setup "
307- "timeout.connect"
308- )
309312 self ._raise_for_status = raise_for_status
310313 self ._auto_decompress = auto_decompress
311314 self ._trust_env = trust_env
0 commit comments