3
3
from django .core .exceptions import ImproperlyConfigured
4
4
from django .utils .module_loading import import_string
5
5
6
- Pool = TypeVar ("Pool" )
7
6
Base = TypeVar ("Base" )
8
7
9
8
10
- class BaseConnectionFactory (Generic [Base , Pool ]):
9
+ class BaseConnectionFactory (Generic [Base ]):
11
10
# Store connection pool by cache backend options.
12
11
#
13
12
# _pools is a process-global, as otherwise _pools is cleared every time
14
13
# ConnectionFactory is instantiated, as Django creates new cache client
15
14
# (DefaultClient) instance for every request.
16
15
17
- _pools : dict [str , Pool | Any ] = {}
16
+ _pools : dict [str , type ] = {} # dict[str, Pool]
18
17
19
18
def __init__ (self , options : dict ) -> None :
20
- pool_cls_path : str = options .get ("CONNECTION_POOL_CLASS" , self .path_pool_cls )
21
- self .pool_cls : type [ Pool ] = import_string (pool_cls_path )
19
+ pool_cls_path : str = options .get ("CONNECTION_POOL_CLASS" , self .path_pool_cls ) # type: ignore[attr-defined]
20
+ self .pool_cls = import_string (pool_cls_path ) # type[Pool]
22
21
self .pool_cls_kwargs = options .get ("CONNECTION_POOL_KWARGS" , {})
23
22
24
- base_client_cls_path : str = options .get ("BASE_CLIENT_CLASS" , self .path_base_cls )
23
+ base_client_cls_path : str = options .get ("BASE_CLIENT_CLASS" , self .path_base_cls ) # type: ignore[attr-defined]
25
24
self .base_client_cls : type [Base ] = import_string (base_client_cls_path )
26
25
self .base_client_cls_kwargs = options .get ("BASE_CLIENT_KWARGS" , {})
27
26
@@ -42,7 +41,7 @@ def make_connection_params(self, url: str | None) -> dict:
42
41
# TODO: do we need to check for existence?
43
42
if socket_timeout :
44
43
if not isinstance (socket_timeout , (int , float )):
45
- error_message = "Socket timeout should be float or integer"
44
+ error_message = "Socket timeout should be float or integer" # type: ignore[unreachable]
46
45
raise ImproperlyConfigured (error_message )
47
46
kwargs ["socket_timeout" ] = socket_timeout
48
47
@@ -51,7 +50,7 @@ def make_connection_params(self, url: str | None) -> dict:
51
50
)
52
51
if socket_connect_timeout :
53
52
if not isinstance (socket_connect_timeout , (int , float )):
54
- error_message = "Socket connect timeout should be float or integer"
53
+ error_message = "Socket connect timeout should be float or integer" # type: ignore[unreachable]
55
54
raise ImproperlyConfigured (error_message )
56
55
kwargs ["socket_connect_timeout" ] = socket_connect_timeout
57
56
@@ -61,31 +60,34 @@ def make_connection_params(self, url: str | None) -> dict:
61
60
62
61
return kwargs
63
62
64
- def get_connection_pool (self , params : dict ) -> Pool :
63
+ def get_connection_pool (self , params : dict ):
65
64
"""
66
65
Given a connection parameters, return a new
67
66
connection pool for them.
68
67
69
68
Overwrite this method if you want a custom
70
69
behavior on creating connection pool.
70
+
71
+ :returns: the connection pool
71
72
"""
72
- cp_params = params
73
- cp_params .update (self .pool_cls_kwargs )
74
- pool = self .pool_cls .from_url (** cp_params )
73
+ params .update (self .pool_cls_kwargs )
74
+ pool = self .pool_cls .from_url (** params )
75
75
76
76
if pool .connection_kwargs .get ("password" , None ) is None :
77
77
pool .connection_kwargs ["password" ] = params .get ("password" )
78
78
pool .reset ()
79
79
80
80
return pool
81
81
82
- def get_or_create_connection_pool (self , params : dict ) -> Pool :
82
+ def get_or_create_connection_pool (self , params : dict ):
83
83
"""
84
84
Given a connection parameters and return a new
85
85
or cached connection pool for them.
86
86
87
87
Reimplement this method if you want distinct
88
88
connection pool instance caching behavior.
89
+
90
+ :returns: the connection pool
89
91
"""
90
92
key : str = params ["url" ]
91
93
if key not in self ._pools :
0 commit comments