Skip to content

Commit

Permalink
Make timeout param of executemany a keyword-only kwarg.
Browse files Browse the repository at this point in the history
This commit removes a compatibility layer from the `executemany()`
function allowing to pass `timeout` as a positional argument.
  • Loading branch information
1st1 committed May 4, 2017
1 parent 682c066 commit bb326fc
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 68 deletions.
43 changes: 1 addition & 42 deletions asyncpg/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,7 @@ async def execute(self, query: str, *args, timeout: float=None) -> str:
_, status, _ = await self._execute(query, args, 0, timeout, True)
return status.decode()

async def executemany(self, command: str, args,
_timeout: float=None, **kw):
# The real signature of this method is:
#
# executemany(self, command: str, args, *, timeout: float=None)
#
async def executemany(self, command: str, args, *, timeout: float=None):
"""Execute an SQL *command* for each sequence of arguments in *args*.
Example:
Expand All @@ -242,24 +237,6 @@ async def executemany(self, command: str, args,
.. versionadded:: 0.7.0
"""
self._check_open()

if 'timeout' in kw:
timeout = kw.pop('timeout')
else:
timeout = _timeout
if timeout is not None:
warnings.warn(
"Passing 'timeout' as a positional argument to "
"executemany() is deprecated and will be removed in "
"asyncpg 0.11.0. Pass it as a keyword argument instead: "
"`executemany(..., timeout=...)`.",
DeprecationWarning, stacklevel=2)
if kw:
first_kwarg = next(iter(kw))
raise TypeError(
'executemany() got an unexpected keyword argument {!r}'.format(
first_kwarg))

return await self._executemany(command, args, timeout)

async def _get_statement(self, query, timeout, *, named: bool=False):
Expand Down Expand Up @@ -1290,21 +1267,3 @@ def _detect_server_capabilities(server_version, connection_settings):
sql_reset=sql_reset,
sql_close_all=sql_close_all
)


def _patch_executemany_signature():
# Patch Connection.executemany() signature to remove '**kw' parameter
# and change '_timeout' keyword arg to 'timeout' keyword-only arg.
# TODO Remove in 0.11.0.
import inspect
sig = inspect.signature(Connection.executemany)
params = sig.parameters.copy()
params.pop('kw')
timeout = params.pop('_timeout')
timeout = timeout.replace(name='timeout', kind=timeout.KEYWORD_ONLY)
params['timeout'] = timeout
Connection.executemany.__signature__ = sig.replace(
parameters=params.values())


_patch_executemany_signature()
26 changes: 0 additions & 26 deletions tests/test_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,29 +154,3 @@ async def test_execute_many_2(self):
''', good_data)
finally:
await self.con.execute('DROP TABLE exmany')

async def test_execute_many_3_kwonly_timeout(self):
with self.assertWarnsRegex(DeprecationWarning,
"Passing 'timeout' as a positional"):
await self.con.executemany(
'''SELECT $1::int''',
[(1,), (2,)],
1)

with warnings.catch_warnings():
warnings.simplefilter("error")

# Test that passing timeout as a kwarg doesn't trigger a warning.
await self.con.executemany(
'''SELECT $1::int''',
[(1,), (2,)],
timeout=1)

# Test that not passing timeout is fine too.
await self.con.executemany(
'''SELECT $1::int''',
[(1,), (2,)])

self.assertEqual(
str(inspect.signature(self.con.executemany)),
'(command:str, args, *, timeout:float=None)')

0 comments on commit bb326fc

Please sign in to comment.