@@ -243,6 +243,9 @@ def __init__(
243
243
max_field_size : int = 8190 ,
244
244
fallback_charset_resolver : _CharsetResolver = lambda r , b : "utf-8" ,
245
245
) -> None :
246
+ # We initialise _connector to None immediately, as it's referenced in __del__()
247
+ # and could cause issues if an exception occurs during initialisation.
248
+ self ._connector : Optional [BaseConnector ] = None
246
249
if base_url is None or isinstance (base_url , URL ):
247
250
self ._base_url : Optional [URL ] = base_url
248
251
else :
@@ -253,12 +256,20 @@ def __init__(
253
256
254
257
loop = asyncio .get_running_loop ()
255
258
259
+ if timeout is sentinel or timeout is None :
260
+ timeout = DEFAULT_TIMEOUT
261
+ if not isinstance (timeout , ClientTimeout ):
262
+ raise ValueError (
263
+ f"timeout parameter cannot be of { type (timeout )} type, "
264
+ "please use 'timeout=ClientTimeout(...)'" ,
265
+ )
266
+ self ._timeout = timeout
267
+
256
268
if connector is None :
257
269
connector = TCPConnector ()
258
-
259
270
# Initialize these three attrs before raising any exception,
260
271
# they are used in __del__
261
- self ._connector : Optional [ BaseConnector ] = connector
272
+ self ._connector = connector
262
273
self ._loop = loop
263
274
if loop .get_debug ():
264
275
self ._source_traceback : Optional [traceback .StackSummary ] = (
@@ -281,14 +292,6 @@ def __init__(
281
292
self ._default_auth = auth
282
293
self ._version = version
283
294
self ._json_serialize = json_serialize
284
- if timeout is sentinel or timeout is None :
285
- timeout = DEFAULT_TIMEOUT
286
- if not isinstance (timeout , ClientTimeout ):
287
- raise ValueError (
288
- f"timeout parameter cannot be of { type (timeout )} type, "
289
- "please use 'timeout=ClientTimeout(...)'" ,
290
- )
291
- self ._timeout = timeout
292
295
self ._raise_for_status = raise_for_status
293
296
self ._auto_decompress = auto_decompress
294
297
self ._trust_env = trust_env
0 commit comments