13
13
import struct
14
14
import time
15
15
import urllib .parse
16
+ import warnings
16
17
17
18
from . import cursor
18
19
from . import exceptions
@@ -216,7 +217,12 @@ async def execute(self, query: str, *args, timeout: float=None) -> str:
216
217
_ , status , _ = await self ._execute (query , args , 0 , timeout , True )
217
218
return status .decode ()
218
219
219
- async def executemany (self , command : str , args , timeout : float = None ):
220
+ async def executemany (self , command : str , args ,
221
+ _timeout : float = None , ** kw ):
222
+ # The real signature of this method is:
223
+ #
224
+ # executemany(self, command: str, args, *, timeout: float=None)
225
+ #
220
226
"""Execute an SQL *command* for each sequence of arguments in *args*.
221
227
222
228
Example:
@@ -234,6 +240,23 @@ async def executemany(self, command: str, args, timeout: float=None):
234
240
235
241
.. versionadded:: 0.7.0
236
242
"""
243
+ if 'timeout' in kw :
244
+ timeout = kw .pop ('timeout' )
245
+ else :
246
+ timeout = _timeout
247
+ if timeout is not None :
248
+ warnings .warn (
249
+ "Passing 'timeout' as a positional argument to "
250
+ "executemany() is deprecated and will be removed in "
251
+ "asyncpg 0.11.0. Pass it as a keyword argument instead: "
252
+ "`executemany(..., timeout=...)`." ,
253
+ DeprecationWarning , stacklevel = 2 )
254
+ if kw :
255
+ first_kwarg = next (iter (kw ))
256
+ raise TypeError (
257
+ 'executemany() got an unexpected keyword argument {!r}' .format (
258
+ first_kwarg ))
259
+
237
260
return await self ._executemany (command , args , timeout )
238
261
239
262
async def _get_statement (self , query , timeout , * , named : bool = False ):
@@ -948,3 +971,21 @@ def _detect_server_capabilities(server_version, connection_settings):
948
971
sql_reset = sql_reset ,
949
972
sql_close_all = sql_close_all
950
973
)
974
+
975
+
976
+ def _patch_executemany_signature ():
977
+ # Patch Connection.executemany() signature to remove '**kw' parameter
978
+ # and change '_timeout' keyword arg to 'timeout' keyword-only arg.
979
+ # TODO Remove in 0.11.0.
980
+ import inspect
981
+ sig = inspect .signature (Connection .executemany )
982
+ params = sig .parameters .copy ()
983
+ params .pop ('kw' )
984
+ timeout = params .pop ('_timeout' )
985
+ timeout = timeout .replace (name = 'timeout' , kind = timeout .KEYWORD_ONLY )
986
+ params ['timeout' ] = timeout
987
+ Connection .executemany .__signature__ = sig .replace (
988
+ parameters = params .values ())
989
+
990
+
991
+ _patch_executemany_signature ()
0 commit comments