From: Dan Shick Date: Tue, 14 Mar 2023 20:09:04 +0000 (-0400) Subject: chore: use TypeAlias to clean up verbose types X-Git-Tag: pool-3.2.0~118^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4c44a5b627dc270f3c8749d039eb940cd3288d17;p=thirdparty%2Fpsycopg.git chore: use TypeAlias to clean up verbose types --- diff --git a/psycopg_pool/psycopg_pool/null_pool.py b/psycopg_pool/psycopg_pool/null_pool.py index 76d241458..ca641d794 100644 --- a/psycopg_pool/psycopg_pool/null_pool.py +++ b/psycopg_pool/psycopg_pool/null_pool.py @@ -11,7 +11,7 @@ from typing import Any, Callable, Dict, Optional, Tuple, Type from psycopg import Connection from psycopg.pq import TransactionStatus -from .pool import ConnectionPool, AddConnection +from .pool import ConnectionPool, AddConnection, SyncConnectFailedCB from .errors import PoolTimeout, TooManyRequests from ._compat import ConnectionTimeout @@ -59,7 +59,7 @@ class NullConnectionPool(_BaseNullConnectionPool, ConnectionPool): max_lifetime: float = 60 * 60.0, max_idle: float = 10 * 60.0, reconnect_timeout: float = 5 * 60.0, - reconnect_failed: Optional[Callable[["NullConnectionPool"], None]] = None, + reconnect_failed: Optional[SyncConnectFailedCB] = None, num_workers: int = 3, ): super().__init__( diff --git a/psycopg_pool/psycopg_pool/null_pool_async.py b/psycopg_pool/psycopg_pool/null_pool_async.py index cb1db2d7a..83254cf38 100644 --- a/psycopg_pool/psycopg_pool/null_pool_async.py +++ b/psycopg_pool/psycopg_pool/null_pool_async.py @@ -6,7 +6,7 @@ psycopg asynchronous null connection pool import asyncio import logging -from typing import Any, Awaitable, Callable, Dict, Optional, Type, Union +from typing import Any, Awaitable, Callable, Dict, Optional, Type from psycopg import AsyncConnection from psycopg.pq import TransactionStatus @@ -14,7 +14,7 @@ from psycopg.pq import TransactionStatus from .errors import PoolTimeout, TooManyRequests from ._compat import ConnectionTimeout from .null_pool import _BaseNullConnectionPool -from .pool_async import AsyncConnectionPool, AddConnection +from .pool_async import AsyncConnectionPool, AddConnection, AsyncConnectFailedCB logger = logging.getLogger("psycopg.pool") @@ -38,12 +38,7 @@ class AsyncNullConnectionPool(_BaseNullConnectionPool, AsyncConnectionPool): max_lifetime: float = 60 * 60.0, max_idle: float = 10 * 60.0, reconnect_timeout: float = 5 * 60.0, - reconnect_failed: Optional[ - Union[ - Callable[["AsyncNullConnectionPool"], None], - Callable[["AsyncNullConnectionPool"], Awaitable[None]], - ] - ] = None, + reconnect_failed: Optional[AsyncConnectFailedCB] = None, num_workers: int = 3, ): super().__init__( diff --git a/psycopg_pool/psycopg_pool/pool.py b/psycopg_pool/psycopg_pool/pool.py index 327a2b6d7..19fd2aa28 100644 --- a/psycopg_pool/psycopg_pool/pool.py +++ b/psycopg_pool/psycopg_pool/pool.py @@ -12,6 +12,7 @@ from queue import Queue, Empty from types import TracebackType from typing import Any, Callable, Dict, Iterator, List from typing import Optional, Sequence, Type +from typing_extensions import TypeAlias from weakref import ref from contextlib import contextmanager @@ -26,6 +27,8 @@ from ._compat import Deque logger = logging.getLogger("psycopg.pool") +SyncConnectFailedCB: TypeAlias = Callable[["ConnectionPool"], None] + class ConnectionPool(BasePool[Connection[Any]]): def __init__( @@ -45,14 +48,14 @@ class ConnectionPool(BasePool[Connection[Any]]): max_lifetime: float = 60 * 60.0, max_idle: float = 10 * 60.0, reconnect_timeout: float = 5 * 60.0, - reconnect_failed: Optional[Callable[["ConnectionPool"], None]] = None, + reconnect_failed: Optional[SyncConnectFailedCB] = None, num_workers: int = 3, ): self.connection_class = connection_class self._configure = configure self._reset = reset - self._reconnect_failed: Callable[["ConnectionPool"], None] + self._reconnect_failed: SyncConnectFailedCB self._reconnect_failed = reconnect_failed or (lambda pool: None) self._lock = threading.RLock() diff --git a/psycopg_pool/psycopg_pool/pool_async.py b/psycopg_pool/psycopg_pool/pool_async.py index 3150da02a..c9b90e08c 100644 --- a/psycopg_pool/psycopg_pool/pool_async.py +++ b/psycopg_pool/psycopg_pool/pool_async.py @@ -11,6 +11,7 @@ from time import monotonic from types import TracebackType from typing import Any, AsyncIterator, Awaitable, Callable from typing import Dict, List, Optional, Sequence, Type, Union +from typing_extensions import TypeAlias from weakref import ref from contextlib import asynccontextmanager @@ -25,6 +26,11 @@ from ._compat import Task, create_task, Deque logger = logging.getLogger("psycopg.pool") +AsyncConnectFailedCB: TypeAlias = Union[ + Callable[["AsyncConnectionPool"], None], + Callable[["AsyncConnectionPool"], Awaitable[None]], +] + class AsyncConnectionPool(BasePool[AsyncConnection[Any]]): def __init__( @@ -44,22 +50,14 @@ class AsyncConnectionPool(BasePool[AsyncConnection[Any]]): max_lifetime: float = 60 * 60.0, max_idle: float = 10 * 60.0, reconnect_timeout: float = 5 * 60.0, - reconnect_failed: Optional[ - Union[ - Callable[["AsyncConnectionPool"], None], - Callable[["AsyncConnectionPool"], Awaitable[None]], - ] - ] = None, + reconnect_failed: Optional[AsyncConnectFailedCB] = None, num_workers: int = 3, ): self.connection_class = connection_class self._configure = configure self._reset = reset - self._reconnect_failed: Union[ - Callable[["AsyncConnectionPool"], None], - Callable[["AsyncConnectionPool"], Awaitable[None]], - ] + self._reconnect_failed: AsyncConnectFailedCB self._reconnect_failed = reconnect_failed or (lambda pool: None) # asyncio objects, created on open to attach them to the right loop.