From: Denis Laxalde Date: Tue, 2 Nov 2021 14:12:43 +0000 (+0100) Subject: Allow Iterable type for cursor.executemany() parameters X-Git-Tag: 3.0.2~1^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7b1a61d5cf05ddd7f304b2a32c4ecd6d76251e83;p=thirdparty%2Fpsycopg.git Allow Iterable type for cursor.executemany() parameters An iterable is enough since we only iterate over the parameters value using a for loop (in _executemany_gen()). There's an usage with a generator expression in tests/scripts/dectest.py. (Spotted by running mypy.) --- diff --git a/psycopg/psycopg/cursor.py b/psycopg/psycopg/cursor.py index 7b3d79ce5..b13efd052 100644 --- a/psycopg/psycopg/cursor.py +++ b/psycopg/psycopg/cursor.py @@ -6,7 +6,7 @@ psycopg cursor objects import sys from types import TracebackType -from typing import Any, Callable, Generic, Iterator, List +from typing import Any, Callable, Generic, Iterable, Iterator, List from typing import Optional, NoReturn, Sequence, Type, TypeVar, TYPE_CHECKING from contextlib import contextmanager @@ -208,7 +208,7 @@ class BaseCursor(Generic[ConnectionType, Row]): self._last_query = query def _executemany_gen( - self, query: Query, params_seq: Sequence[Params] + self, query: Query, params_seq: Iterable[Params] ) -> PQGen[None]: """Generator implementing `Cursor.executemany()`.""" yield from self._start_query(query) @@ -564,7 +564,7 @@ class Cursor(BaseCursor["Connection[Any]", Row]): raise ex.with_traceback(None) return self - def executemany(self, query: Query, params_seq: Sequence[Params]) -> None: + def executemany(self, query: Query, params_seq: Iterable[Params]) -> None: """ Execute the same command with a sequence of input data. """ diff --git a/psycopg/psycopg/cursor_async.py b/psycopg/psycopg/cursor_async.py index db573c0c7..f9d7109db 100644 --- a/psycopg/psycopg/cursor_async.py +++ b/psycopg/psycopg/cursor_async.py @@ -5,8 +5,8 @@ psycopg async cursor objects # Copyright (C) 2020-2021 The Psycopg Team from types import TracebackType -from typing import Any, AsyncIterator, List -from typing import Optional, Sequence, Type, TypeVar, TYPE_CHECKING +from typing import Any, AsyncIterator, Iterable, List +from typing import Optional, Type, TypeVar, TYPE_CHECKING from . import errors as e @@ -82,7 +82,7 @@ class AsyncCursor(BaseCursor["AsyncConnection[Any]", Row]): return self async def executemany( - self, query: Query, params_seq: Sequence[Params] + self, query: Query, params_seq: Iterable[Params] ) -> None: async with self._conn.lock: await self._conn.wait(self._executemany_gen(query, params_seq)) diff --git a/psycopg/psycopg/server_cursor.py b/psycopg/psycopg/server_cursor.py index f89f470fb..6e6653df5 100644 --- a/psycopg/psycopg/server_cursor.py +++ b/psycopg/psycopg/server_cursor.py @@ -5,8 +5,8 @@ psycopg server-side cursor objects. # Copyright (C) 2020-2021 The Psycopg Team import warnings -from typing import Any, AsyncIterator, Generic, List, Iterator, Optional -from typing import Sequence, TypeVar, TYPE_CHECKING +from typing import Any, AsyncIterator, Generic, List, Iterable, Iterator +from typing import Optional, TypeVar, TYPE_CHECKING from . import pq from . import sql @@ -267,7 +267,7 @@ class ServerCursor(Cursor[Row]): return self - def executemany(self, query: Query, params_seq: Sequence[Params]) -> None: + def executemany(self, query: Query, params_seq: Iterable[Params]) -> None: """Method not implemented for server-side cursors.""" raise e.NotSupportedError( "executemany not supported on server-side cursors" @@ -387,7 +387,7 @@ class AsyncServerCursor(AsyncCursor[Row]): return self async def executemany( - self, query: Query, params_seq: Sequence[Params] + self, query: Query, params_seq: Iterable[Params] ) -> None: raise e.NotSupportedError( "executemany not supported on server-side cursors"