]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
refactor: use typing.Self
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 30 Dec 2023 00:30:39 +0000 (01:30 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 30 Dec 2023 02:19:52 +0000 (03:19 +0100)
The object seems available for all the supported Python version and
should avoid problems with PyRight (see #708).

It is not a solution for #308 because we cannot use `Self[Row]`.

docs/news_pool.rst
psycopg_pool/psycopg_pool/_compat.py
psycopg_pool/psycopg_pool/pool.py
psycopg_pool/psycopg_pool/pool_async.py
psycopg_pool/setup.cfg

index 8db413aad4e8d7bbeb473169ea9d8517ba61e2b1..ec758550d28522701631c1502d6d856c36b7b9f9 100644 (file)
@@ -7,6 +7,16 @@
 ``psycopg_pool`` release notes
 ==============================
 
+Future releases
+---------------
+
+psycopg_pool 3.2.1 (unreleased)
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+- Use `typing.Self` as a more correct return value annotation of context
+  managers and other self-returning methods (see :ticket:`708`).
+
+
 Current release
 ---------------
 
@@ -25,8 +35,8 @@ psycopg_pool 3.2.0
   it will become an error. (:ticket:`#659`).
 
 
-psycopg_pool 3.1.9 (unreleased)
-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+psycopg_pool 3.1.9
+^^^^^^^^^^^^^^^^^^
 
 - Fix the return type annotation of `!NullConnectionPool.__enter__()`
   (:ticket:`#540`).
index c3e27f02b85f80231ca9a1d6637cd1239b1646ec..e88cf2b3d15952a2a337b685dc99097a038df222 100644 (file)
@@ -14,9 +14,15 @@ if sys.version_info >= (3, 9):
 else:
     from typing import Counter, Deque
 
+if sys.version_info >= (3, 11):
+    from typing import Self
+else:
+    from typing_extensions import Self
+
 __all__ = [
     "Counter",
     "Deque",
+    "Self",
 ]
 
 # Workaround for psycopg < 3.0.8.
index 288473fc06ba75debe81caf85b6b944fe59dc9ee..1e298518bbd4c873c719895dfaa5d0741cf9306e 100644 (file)
@@ -15,7 +15,7 @@ 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, TypeVar
+from typing import Optional, overload, Sequence, Type
 from weakref import ref
 from contextlib import contextmanager
 
@@ -27,7 +27,7 @@ from psycopg.rows import TupleRow
 from .abc import CT, ConnectionCB, ConnectFailedCB
 from .base import ConnectionAttempt, BasePool
 from .errors import PoolClosed, PoolTimeout, TooManyRequests
-from ._compat import Deque
+from ._compat import Deque, Self
 from ._acompat import Condition, Event, Lock, Queue, Worker, spawn, gather
 from ._acompat import current_thread_name
 from .sched import Scheduler
@@ -37,7 +37,6 @@ logger = logging.getLogger("psycopg.pool")
 
 
 class ConnectionPool(Generic[CT], BasePool):
-    _Self = TypeVar("_Self", bound="ConnectionPool[CT]")
     _pool: Deque[CT]
 
     @overload
@@ -491,7 +490,7 @@ class ConnectionPool(Generic[CT], BasePool):
         sched_runner, self._sched_runner = (self._sched_runner, None)
         gather(sched_runner, *workers, timeout=timeout)
 
-    def __enter__(self: _Self) -> _Self:
+    def __enter__(self) -> Self:
         self._open_implicit = False
         self.open()
         return self
index 25cec3f9544e7a221946271163e03040bdc07a44..d53243ff91187080e0cf0c61fea78262ca6d15a4 100644 (file)
@@ -12,7 +12,7 @@ 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, TypeVar
+from typing import Optional, overload, Sequence, Type
 from weakref import ref
 from contextlib import asynccontextmanager
 
@@ -24,7 +24,7 @@ from psycopg.rows import TupleRow
 from .abc import ACT, AsyncConnectionCB, AsyncConnectFailedCB
 from .base import ConnectionAttempt, BasePool
 from .errors import PoolClosed, PoolTimeout, TooManyRequests
-from ._compat import Deque
+from ._compat import Deque, Self
 from ._acompat import ACondition, AEvent, ALock, AQueue, AWorker, aspawn, agather
 from ._acompat import current_task_name
 from .sched_async import AsyncScheduler
@@ -36,7 +36,6 @@ logger = logging.getLogger("psycopg.pool")
 
 
 class AsyncConnectionPool(Generic[ACT], BasePool):
-    _Self = TypeVar("_Self", bound="AsyncConnectionPool[ACT]")
     _pool: Deque[ACT]
 
     @overload
@@ -519,7 +518,7 @@ class AsyncConnectionPool(Generic[ACT], BasePool):
         sched_runner, self._sched_runner = self._sched_runner, None
         await agather(sched_runner, *workers, timeout=timeout)
 
-    async def __aenter__(self: _Self) -> _Self:
+    async def __aenter__(self) -> Self:
         self._open_implicit = False
         await self.open()
         return self
index f439e8bc230a4bcd8cc5004938cd9a89bf252322..8c3de4ba124ef2a95a0aea9e737869bc9ac54a1a 100644 (file)
@@ -46,7 +46,7 @@ python_requires = >= 3.8
 packages = find:
 zip_safe = False
 install_requires =
-    typing-extensions >= 3.10
+    typing-extensions >= 4.0
 
 [options.package_data]
 psycopg_pool = py.typed