From: Daniele Varrazzo Date: Wed, 3 Jan 2024 01:32:28 +0000 (+0100) Subject: refactor(pool): avoid constructor overloading by using generic default X-Git-Tag: pool-3.2.1~7^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b8dab5495fd3ad0424caec93b57ebb16ee9a084e;p=thirdparty%2Fpsycopg.git refactor(pool): avoid constructor overloading by using generic default --- diff --git a/psycopg_pool/psycopg_pool/abc.py b/psycopg_pool/psycopg_pool/abc.py index 9d76b17fd..6cc85a2c5 100644 --- a/psycopg_pool/psycopg_pool/abc.py +++ b/psycopg_pool/psycopg_pool/abc.py @@ -16,10 +16,11 @@ if TYPE_CHECKING: from .pool import ConnectionPool from .pool_async import AsyncConnectionPool from psycopg import Connection, AsyncConnection # noqa: F401 + from psycopg.rows import TupleRow # noqa: F401 # Connection types to make the pool generic -CT = TypeVar("CT", bound="Connection[Any]") -ACT = TypeVar("ACT", bound="AsyncConnection[Any]") +CT = TypeVar("CT", bound="Connection[Any]", default="Connection[TupleRow]") +ACT = TypeVar("ACT", bound="AsyncConnection[Any]", default="AsyncConnection[TupleRow]") # Callbacks taking a connection from the pool ConnectionCB: TypeAlias = Callable[[CT], None] diff --git a/psycopg_pool/psycopg_pool/null_pool.py b/psycopg_pool/psycopg_pool/null_pool.py index bf062d0fd..3e5651acb 100644 --- a/psycopg_pool/psycopg_pool/null_pool.py +++ b/psycopg_pool/psycopg_pool/null_pool.py @@ -10,11 +10,10 @@ Psycopg null connection pool module (sync version). from __future__ import annotations import logging -from typing import Any, cast, Dict, Optional, overload, Type +from typing import Any, cast, Dict, Optional, Type from psycopg import Connection from psycopg.pq import TransactionStatus -from psycopg.rows import TupleRow from .abc import CT, ConnectionCB, ConnectFailedCB from .errors import PoolTimeout, TooManyRequests @@ -27,53 +26,6 @@ logger = logging.getLogger("psycopg.pool") class NullConnectionPool(_BaseNullConnectionPool, ConnectionPool[CT]): - @overload - def __init__( - self: NullConnectionPool[Connection[TupleRow]], - conninfo: str = "", - *, - kwargs: Optional[Dict[str, Any]] = ..., - min_size: int = ..., - max_size: Optional[int] = ..., - open: bool | None = ..., - configure: Optional[ConnectionCB[CT]] = ..., - check: Optional[ConnectionCB[CT]] = ..., - reset: Optional[ConnectionCB[CT]] = ..., - name: Optional[str] = ..., - timeout: float = ..., - max_waiting: int = ..., - max_lifetime: float = ..., - max_idle: float = ..., - reconnect_timeout: float = ..., - reconnect_failed: Optional[ConnectFailedCB] = ..., - num_workers: int = ..., - ): - ... - - @overload - def __init__( - self: NullConnectionPool[CT], - conninfo: str = "", - *, - connection_class: Type[CT] = ..., - kwargs: Optional[Dict[str, Any]] = ..., - min_size: int = ..., - max_size: Optional[int] = ..., - open: bool | None = ..., - configure: Optional[ConnectionCB[CT]] = ..., - check: Optional[ConnectionCB[CT]] = ..., - reset: Optional[ConnectionCB[CT]] = ..., - name: Optional[str] = ..., - timeout: float = ..., - max_waiting: int = ..., - max_lifetime: float = ..., - max_idle: float = ..., - reconnect_timeout: float = ..., - reconnect_failed: Optional[ConnectFailedCB] = ..., - num_workers: int = ..., - ): - ... - def __init__( self, conninfo: str = "", diff --git a/psycopg_pool/psycopg_pool/null_pool_async.py b/psycopg_pool/psycopg_pool/null_pool_async.py index 2528ff33f..74725e87e 100644 --- a/psycopg_pool/psycopg_pool/null_pool_async.py +++ b/psycopg_pool/psycopg_pool/null_pool_async.py @@ -7,11 +7,10 @@ Psycopg null connection pool module (async version). from __future__ import annotations import logging -from typing import Any, cast, Dict, Optional, overload, Type +from typing import Any, cast, Dict, Optional, Type from psycopg import AsyncConnection from psycopg.pq import TransactionStatus -from psycopg.rows import TupleRow from .abc import ACT, AsyncConnectionCB, AsyncConnectFailedCB from .errors import PoolTimeout, TooManyRequests @@ -24,53 +23,6 @@ logger = logging.getLogger("psycopg.pool") class AsyncNullConnectionPool(_BaseNullConnectionPool, AsyncConnectionPool[ACT]): - @overload - def __init__( - self: AsyncNullConnectionPool[AsyncConnection[TupleRow]], - conninfo: str = "", - *, - kwargs: Optional[Dict[str, Any]] = ..., - min_size: int = ..., - max_size: Optional[int] = ..., - open: bool | None = ..., - configure: Optional[AsyncConnectionCB[ACT]] = ..., - check: Optional[AsyncConnectionCB[ACT]] = ..., - reset: Optional[AsyncConnectionCB[ACT]] = ..., - name: Optional[str] = ..., - timeout: float = ..., - max_waiting: int = ..., - max_lifetime: float = ..., - max_idle: float = ..., - reconnect_timeout: float = ..., - reconnect_failed: Optional[AsyncConnectFailedCB] = ..., - num_workers: int = ..., - ): - ... - - @overload - def __init__( - self: AsyncNullConnectionPool[ACT], - conninfo: str = "", - *, - connection_class: Type[ACT] = ..., - kwargs: Optional[Dict[str, Any]] = ..., - min_size: int = ..., - max_size: Optional[int] = ..., - open: bool | None = ..., - configure: Optional[AsyncConnectionCB[ACT]] = ..., - check: Optional[AsyncConnectionCB[ACT]] = ..., - reset: Optional[AsyncConnectionCB[ACT]] = ..., - name: Optional[str] = ..., - timeout: float = ..., - max_waiting: int = ..., - max_lifetime: float = ..., - max_idle: float = ..., - reconnect_timeout: float = ..., - reconnect_failed: Optional[AsyncConnectFailedCB] = ..., - num_workers: int = ..., - ): - ... - def __init__( self, conninfo: str = "", diff --git a/psycopg_pool/psycopg_pool/pool.py b/psycopg_pool/psycopg_pool/pool.py index 45e218447..85765c96f 100644 --- a/psycopg_pool/psycopg_pool/pool.py +++ b/psycopg_pool/psycopg_pool/pool.py @@ -15,14 +15,13 @@ from abc import ABC, abstractmethod from time import monotonic from types import TracebackType from typing import Any, Iterator, cast, Dict, Generic, List -from typing import Optional, overload, Sequence, Type +from typing import Optional, Sequence, Type from weakref import ref from contextlib import contextmanager from psycopg import errors as e from psycopg import Connection from psycopg.pq import TransactionStatus -from psycopg.rows import TupleRow from .abc import CT, ConnectionCB, ConnectFailedCB from .base import ConnectionAttempt, BasePool @@ -39,53 +38,6 @@ logger = logging.getLogger("psycopg.pool") class ConnectionPool(Generic[CT], BasePool): _pool: Deque[CT] - @overload - def __init__( - self: ConnectionPool[Connection[TupleRow]], - conninfo: str = "", - *, - kwargs: Optional[Dict[str, Any]] = ..., - min_size: int = ..., - max_size: Optional[int] = ..., - open: bool | None = ..., - configure: Optional[ConnectionCB[CT]] = ..., - check: Optional[ConnectionCB[CT]] = ..., - reset: Optional[ConnectionCB[CT]] = ..., - name: Optional[str] = ..., - timeout: float = ..., - max_waiting: int = ..., - max_lifetime: float = ..., - max_idle: float = ..., - reconnect_timeout: float = ..., - reconnect_failed: Optional[ConnectFailedCB] = ..., - num_workers: int = ..., - ): - ... - - @overload - def __init__( - self: ConnectionPool[CT], - conninfo: str = "", - *, - connection_class: Type[CT] = ..., - kwargs: Optional[Dict[str, Any]] = ..., - min_size: int = ..., - max_size: Optional[int] = ..., - open: bool | None = ..., - configure: Optional[ConnectionCB[CT]] = ..., - check: Optional[ConnectionCB[CT]] = ..., - reset: Optional[ConnectionCB[CT]] = ..., - name: Optional[str] = ..., - timeout: float = ..., - max_waiting: int = ..., - max_lifetime: float = ..., - max_idle: float = ..., - reconnect_timeout: float = ..., - reconnect_failed: Optional[ConnectFailedCB] = ..., - num_workers: int = ..., - ): - ... - def __init__( self, conninfo: str = "", diff --git a/psycopg_pool/psycopg_pool/pool_async.py b/psycopg_pool/psycopg_pool/pool_async.py index e3f7e113c..719b7221f 100644 --- a/psycopg_pool/psycopg_pool/pool_async.py +++ b/psycopg_pool/psycopg_pool/pool_async.py @@ -12,14 +12,13 @@ from abc import ABC, abstractmethod from time import monotonic from types import TracebackType from typing import Any, AsyncIterator, cast, Dict, Generic, List -from typing import Optional, overload, Sequence, Type +from typing import Optional, Sequence, Type from weakref import ref from contextlib import asynccontextmanager from psycopg import errors as e from psycopg import AsyncConnection from psycopg.pq import TransactionStatus -from psycopg.rows import TupleRow from .abc import ACT, AsyncConnectionCB, AsyncConnectFailedCB from .base import ConnectionAttempt, BasePool @@ -38,53 +37,6 @@ logger = logging.getLogger("psycopg.pool") class AsyncConnectionPool(Generic[ACT], BasePool): _pool: Deque[ACT] - @overload - def __init__( - self: AsyncConnectionPool[AsyncConnection[TupleRow]], - conninfo: str = "", - *, - kwargs: Optional[Dict[str, Any]] = ..., - min_size: int = ..., - max_size: Optional[int] = ..., - open: bool | None = ..., - configure: Optional[AsyncConnectionCB[ACT]] = ..., - check: Optional[AsyncConnectionCB[ACT]] = ..., - reset: Optional[AsyncConnectionCB[ACT]] = ..., - name: Optional[str] = ..., - timeout: float = ..., - max_waiting: int = ..., - max_lifetime: float = ..., - max_idle: float = ..., - reconnect_timeout: float = ..., - reconnect_failed: Optional[AsyncConnectFailedCB] = ..., - num_workers: int = ..., - ): - ... - - @overload - def __init__( - self: AsyncConnectionPool[ACT], - conninfo: str = "", - *, - connection_class: Type[ACT] = ..., - kwargs: Optional[Dict[str, Any]] = ..., - min_size: int = ..., - max_size: Optional[int] = ..., - open: bool | None = ..., - configure: Optional[AsyncConnectionCB[ACT]] = ..., - check: Optional[AsyncConnectionCB[ACT]] = ..., - reset: Optional[AsyncConnectionCB[ACT]] = ..., - name: Optional[str] = ..., - timeout: float = ..., - max_waiting: int = ..., - max_lifetime: float = ..., - max_idle: float = ..., - reconnect_timeout: float = ..., - reconnect_failed: Optional[AsyncConnectFailedCB] = ..., - num_workers: int = ..., - ): - ... - def __init__( self, conninfo: str = "", diff --git a/tests/pool/test_pool.py b/tests/pool/test_pool.py index 6323420e2..473fe17f5 100644 --- a/tests/pool/test_pool.py +++ b/tests/pool/test_pool.py @@ -177,18 +177,18 @@ def test_configure(dsn): with p.connection() as conn: assert inits == 1 res = conn.execute("show default_transaction_read_only") - assert res.fetchone()[0] == "on" # type: ignore[index] + assert res.fetchone()[0] == "on" with p.connection() as conn: assert inits == 1 res = conn.execute("show default_transaction_read_only") - assert res.fetchone()[0] == "on" # type: ignore[index] + assert res.fetchone()[0] == "on" conn.close() with p.connection() as conn: assert inits == 2 res = conn.execute("show default_transaction_read_only") - assert res.fetchone()[0] == "on" # type: ignore[index] + assert res.fetchone()[0] == "on" def test_reset(dsn): diff --git a/tests/pool/test_pool_async.py b/tests/pool/test_pool_async.py index 7319e00b4..b192bf862 100644 --- a/tests/pool/test_pool_async.py +++ b/tests/pool/test_pool_async.py @@ -181,18 +181,18 @@ async def test_configure(dsn): async with p.connection() as conn: assert inits == 1 res = await conn.execute("show default_transaction_read_only") - assert (await res.fetchone())[0] == "on" # type: ignore[index] + assert (await res.fetchone())[0] == "on" async with p.connection() as conn: assert inits == 1 res = await conn.execute("show default_transaction_read_only") - assert (await res.fetchone())[0] == "on" # type: ignore[index] + assert (await res.fetchone())[0] == "on" await conn.close() async with p.connection() as conn: assert inits == 2 res = await conn.execute("show default_transaction_read_only") - assert (await res.fetchone())[0] == "on" # type: ignore[index] + assert (await res.fetchone())[0] == "on" async def test_reset(dsn):