]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
refactor(pool): avoid constructor overloading by using generic default
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 3 Jan 2024 01:32:28 +0000 (02:32 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 3 Jan 2024 02:52:10 +0000 (03:52 +0100)
psycopg_pool/psycopg_pool/abc.py
psycopg_pool/psycopg_pool/null_pool.py
psycopg_pool/psycopg_pool/null_pool_async.py
psycopg_pool/psycopg_pool/pool.py
psycopg_pool/psycopg_pool/pool_async.py
tests/pool/test_pool.py
tests/pool/test_pool_async.py

index 9d76b17fd84b81900638246eb2708ec61cd79547..6cc85a2c569e6ffd7c9abc637daf647da344cd15 100644 (file)
@@ -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]
index bf062d0fd14d71fbc3b6c018afc86cf36f82dd6e..3e5651acb0cd09c8f3189afdcf26626b8a83356e 100644 (file)
@@ -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 = "",
index 2528ff33fa659712ae1331a840783bd6219fd1e7..74725e87e01cccffee051ce63e89b55ecd5ef627 100644 (file)
@@ -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 = "",
index 45e2184474fc089f85f9a74c3b8fc5d69956b83c..85765c96f170b655a70d9f6be69c059bde721443 100644 (file)
@@ -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 = "",
index e3f7e113cfb98155459428e555d3589ae206e4a7..719b7221f34730a1c7af22f3497bb173f810da3c 100644 (file)
@@ -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 = "",
index 6323420e2dd8b4d96832d162b70c9abc22029caa..473fe17f545d2bad72d9fe3d6a9c4e8092baa2d9 100644 (file)
@@ -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):
index 7319e00b444bc216f0e3de5d5e1c30b7991453e9..b192bf86236a3d2f7d3294351d37f38bd595c5c7 100644 (file)
@@ -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):