]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
chore: use TypeAlias to clean up verbose types
authorDan Shick <dan.shick@nydig.com>
Tue, 14 Mar 2023 20:09:04 +0000 (16:09 -0400)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 15 Mar 2023 02:51:53 +0000 (03:51 +0100)
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

index 76d2414583de012deac9f73797f1bb5fe9434ac0..ca641d7946f533e24fe24c6a73bf5d20ad6d566e 100644 (file)
@@ -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__(
index cb1db2d7a4f30ee480fb2808d4b8176473ac155f..83254cf38d4e68861d91a9076ffe93688ccb1508 100644 (file)
@@ -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__(
index 327a2b6d7a37399b6cb50e13827336ef67f852eb..19fd2aa285d135f3fb0b15fed1f71a267196b6c7 100644 (file)
@@ -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()
index 3150da02ab9d7e2d9863d0c749999735477157e9..c9b90e08c157364fec770c20d0a24f6de5e58550 100644 (file)
@@ -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.