]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
refactor: use typing.Self 710/head
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 01:00:25 +0000 (02:00 +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]`.

17 files changed:
docs/news.rst
docs/news_pool.rst
psycopg/psycopg/_compat.py
psycopg/psycopg/_pipeline.py
psycopg/psycopg/connection.py
psycopg/psycopg/connection_async.py
psycopg/psycopg/copy.py
psycopg/psycopg/crdb/connection.py
psycopg/psycopg/cursor.py
psycopg/psycopg/cursor_async.py
psycopg/psycopg/pq/_debug.py
psycopg/psycopg/server_cursor.py
psycopg/psycopg/transaction.py
psycopg_pool/psycopg_pool/_compat.py
psycopg_pool/psycopg_pool/pool.py
psycopg_pool/psycopg_pool/pool_async.py
psycopg_pool/setup.cfg

index e7233696d35d08950d551b7582ea180f1002cf06..5fd7c51287aaaf8b507ae834d18e8db57f3a6e6d 100644 (file)
@@ -7,6 +7,16 @@
 ``psycopg`` release notes
 =========================
 
+Future releases
+---------------
+
+Psycopg 3.1.17 (unreleased)
+^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+- Use `typing.Self` as a more correct return value annotation of context
+  managers and other self-returning methods (see :ticket:`708`).
+
+
 Current release
 ---------------
 
index 3f29ef97a813cf79d28f9bb5f2f843ed67b938fc..b95336e3ddf55d3dd89a8caf5550ecd98c4d809e 100644 (file)
@@ -7,6 +7,16 @@
 ``psycopg_pool`` release notes
 ==============================
 
+Future releases
+---------------
+
+psycopg_pool 3.1.10 (unreleased)
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+- Use `typing.Self` as a more correct return value annotation of context
+  managers and other self-returning methods (see :ticket:`708`).
+
+
 Current release
 ---------------
 
index 7dbae79686d961bbb6992a4482a7cc49d1277e88..994e86f0f9e24ef84836bb9cf2be65a4c96c35a6 100644 (file)
@@ -55,15 +55,16 @@ else:
     from typing_extensions import TypeGuard
 
 if sys.version_info >= (3, 11):
-    from typing import LiteralString
+    from typing import LiteralString, Self
 else:
-    from typing_extensions import LiteralString
+    from typing_extensions import LiteralString, Self
 
 __all__ = [
     "Counter",
     "Deque",
     "LiteralString",
     "Protocol",
+    "Self",
     "TypeGuard",
     "ZoneInfo",
     "cache",
index 3c73f7eeae595e66b70a4f57f30f7f06bc801dec..7064f6038683fbf5b0e7a34891091aefcfa9950d 100644 (file)
@@ -6,13 +6,13 @@ commands pipeline management
 
 import logging
 from types import TracebackType
-from typing import Any, List, Optional, Union, Tuple, Type, TypeVar, TYPE_CHECKING
+from typing import Any, List, Optional, Union, Tuple, Type, TYPE_CHECKING
 from typing_extensions import TypeAlias
 
 from . import pq
 from . import errors as e
 from .abc import PipelineCommand, PQGen
-from ._compat import Deque
+from ._compat import Deque, Self
 from .pq.misc import connection_summary
 from ._encodings import pgconn_encoding
 from ._preparing import Key, Prepare
@@ -218,7 +218,6 @@ class Pipeline(BasePipeline):
 
     __module__ = "psycopg"
     _conn: "Connection[Any]"
-    _Self = TypeVar("_Self", bound="Pipeline")
 
     def __init__(self, conn: "Connection[Any]") -> None:
         super().__init__(conn)
@@ -233,7 +232,7 @@ class Pipeline(BasePipeline):
         except e._NO_TRACEBACK as ex:
             raise ex.with_traceback(None)
 
-    def __enter__(self: _Self) -> _Self:
+    def __enter__(self) -> Self:
         with self._conn.lock:
             self._conn.wait(self._enter_gen())
         return self
@@ -262,7 +261,6 @@ class AsyncPipeline(BasePipeline):
 
     __module__ = "psycopg"
     _conn: "AsyncConnection[Any]"
-    _Self = TypeVar("_Self", bound="AsyncPipeline")
 
     def __init__(self, conn: "AsyncConnection[Any]") -> None:
         super().__init__(conn)
@@ -274,7 +272,7 @@ class AsyncPipeline(BasePipeline):
         except e._NO_TRACEBACK as ex:
             raise ex.with_traceback(None)
 
-    async def __aenter__(self: _Self) -> _Self:
+    async def __aenter__(self) -> Self:
         async with self._conn.lock:
             await self._conn.wait(self._enter_gen())
         return self
index a38a98fc39b39b246f41d44cdb7714a149b4a975..163e9b8651ed3aee65e8aab160fefa089f8f6e9e 100644 (file)
@@ -28,7 +28,7 @@ from .rows import Row, RowFactory, tuple_row, TupleRow, args_row
 from .adapt import AdaptersMap
 from ._enums import IsolationLevel
 from .cursor import Cursor
-from ._compat import LiteralString
+from ._compat import LiteralString, Self
 from .pq.misc import connection_summary
 from .conninfo import make_conninfo, conninfo_to_dict, ConnectionInfo
 from .conninfo import conninfo_attempts, ConnDict, timeout_from_conninfo
@@ -666,7 +666,6 @@ class Connection(BaseConnection[Row]):
     server_cursor_factory: Type[ServerCursor[Row]]
     row_factory: RowFactory[Row]
     _pipeline: Optional[Pipeline]
-    _Self = TypeVar("_Self", bound="Connection[Any]")
 
     def __init__(
         self,
@@ -692,7 +691,9 @@ class Connection(BaseConnection[Row]):
         context: Optional[AdaptContext] = None,
         **kwargs: Union[None, int, str],
     ) -> "Connection[Row]":
-        # TODO: returned type should be _Self. See #308.
+        # TODO: returned type should be Self. See #308.
+        # Unfortunately we cannot use Self[Row] as Self is not parametric.
+        # https://peps.python.org/pep-0673/#use-in-generic-classes
         ...
 
     @overload
@@ -720,7 +721,7 @@ class Connection(BaseConnection[Row]):
         cursor_factory: Optional[Type[Cursor[Row]]] = None,
         context: Optional[AdaptContext] = None,
         **kwargs: Any,
-    ) -> "Connection[Any]":
+    ) -> Self:
         """
         Connect to a database server and return a new `Connection` instance.
         """
@@ -759,7 +760,7 @@ class Connection(BaseConnection[Row]):
         rv.prepare_threshold = prepare_threshold
         return rv
 
-    def __enter__(self: _Self) -> _Self:
+    def __enter__(self) -> Self:
         return self
 
     def __exit__(
index 88544db997e7dc57e60ecafa31763737b9c47ffa..0422159e5db4a3231a59faa3d2e9c27026b45fcd 100644 (file)
@@ -9,7 +9,7 @@ import asyncio
 import logging
 from types import TracebackType
 from typing import Any, AsyncGenerator, AsyncIterator, List, Optional
-from typing import Type, TypeVar, Union, cast, overload, TYPE_CHECKING
+from typing import Type, Union, cast, overload, TYPE_CHECKING
 from contextlib import asynccontextmanager
 
 from . import pq
@@ -20,6 +20,7 @@ from ._tpc import Xid
 from .rows import Row, AsyncRowFactory, tuple_row, TupleRow, args_row
 from .adapt import AdaptersMap
 from ._enums import IsolationLevel
+from ._compat import Self
 from .conninfo import ConnDict, make_conninfo, conninfo_to_dict
 from .conninfo import conninfo_attempts_async, timeout_from_conninfo
 from ._pipeline import AsyncPipeline
@@ -53,7 +54,6 @@ class AsyncConnection(BaseConnection[Row]):
     server_cursor_factory: Type[AsyncServerCursor[Row]]
     row_factory: AsyncRowFactory[Row]
     _pipeline: Optional[AsyncPipeline]
-    _Self = TypeVar("_Self", bound="AsyncConnection[Any]")
 
     def __init__(
         self,
@@ -79,7 +79,9 @@ class AsyncConnection(BaseConnection[Row]):
         context: Optional[AdaptContext] = None,
         **kwargs: Union[None, int, str],
     ) -> "AsyncConnection[Row]":
-        # TODO: returned type should be _Self. See #308.
+        # TODO: returned type should be Self. See #308.
+        # Unfortunately we cannot use Self[Row] as Self is not parametric.
+        # https://peps.python.org/pep-0673/#use-in-generic-classes
         ...
 
     @overload
@@ -107,7 +109,7 @@ class AsyncConnection(BaseConnection[Row]):
         row_factory: Optional[AsyncRowFactory[Row]] = None,
         cursor_factory: Optional[Type[AsyncCursor[Row]]] = None,
         **kwargs: Any,
-    ) -> "AsyncConnection[Any]":
+    ) -> Self:
         if sys.platform == "win32":
             loop = asyncio.get_running_loop()
             if isinstance(loop, asyncio.ProactorEventLoop):
@@ -153,7 +155,7 @@ class AsyncConnection(BaseConnection[Row]):
         rv.prepare_threshold = prepare_threshold
         return rv
 
-    async def __aenter__(self: _Self) -> _Self:
+    async def __aenter__(self) -> Self:
         return self
 
     async def __aexit__(
index 10701e1480ce41df50c0d70103453e655e1c35ff..7b41b288bae6e620761a5debd6a83975dcf64625 100644 (file)
@@ -12,13 +12,13 @@ import threading
 from abc import ABC, abstractmethod
 from types import TracebackType
 from typing import Any, AsyncIterator, Dict, Generic, Iterator, List, Match, IO
-from typing import Optional, Sequence, Tuple, Type, TypeVar, Union, TYPE_CHECKING
+from typing import Optional, Sequence, Tuple, Type, Union, TYPE_CHECKING
 
 from . import pq
 from . import adapt
 from . import errors as e
 from .abc import Buffer, ConnectionType, PQGen, Transformer
-from ._compat import create_task
+from ._compat import create_task, Self
 from .pq.misc import connection_summary
 from ._cmodule import _psycopg
 from ._encodings import pgconn_encoding
@@ -75,8 +75,6 @@ class BaseCopy(Generic[ConnectionType]):
     a file for later use.
     """
 
-    _Self = TypeVar("_Self", bound="BaseCopy[Any]")
-
     formatter: "Formatter"
 
     def __init__(
@@ -237,7 +235,7 @@ class Copy(BaseCopy["Connection[Any]"]):
         self.writer = writer
         self._write = writer.write
 
-    def __enter__(self: BaseCopy._Self) -> BaseCopy._Self:
+    def __enter__(self) -> Self:
         self._enter()
         return self
 
@@ -495,7 +493,7 @@ class AsyncCopy(BaseCopy["AsyncConnection[Any]"]):
         self.writer = writer
         self._write = writer.write
 
-    async def __aenter__(self: BaseCopy._Self) -> BaseCopy._Self:
+    async def __aenter__(self) -> Self:
         self._enter()
         return self
 
index 451474b77ecf9a07bf93174187a2db3789a55c0c..49b7d5ffa3a465feecac4f31f12efebcac9e009c 100644 (file)
@@ -10,6 +10,7 @@ from typing import Any, Optional, Type, Union, overload, TYPE_CHECKING
 from .. import errors as e
 from ..abc import AdaptContext
 from ..rows import Row, RowFactory, AsyncRowFactory, TupleRow
+from .._compat import Self
 from ..conninfo import ConnectionInfo
 from ..connection import Connection
 from .._adapters_map import AdaptersMap
@@ -95,7 +96,7 @@ class CrdbConnection(_CrdbConnectionMixin, Connection[Row]):
         ...
 
     @classmethod
-    def connect(cls, conninfo: str = "", **kwargs: Any) -> "CrdbConnection[Any]":
+    def connect(cls, conninfo: str = "", **kwargs: Any) -> Self:
         """
         Connect to a database server and return a new `CrdbConnection` instance.
         """
@@ -142,10 +143,8 @@ class AsyncCrdbConnection(_CrdbConnectionMixin, AsyncConnection[Row]):
         ...
 
     @classmethod
-    async def connect(
-        cls, conninfo: str = "", **kwargs: Any
-    ) -> "AsyncCrdbConnection[Any]":
-        return await super().connect(conninfo, **kwargs)  # type: ignore [no-any-return]
+    async def connect(cls, conninfo: str = "", **kwargs: Any) -> Self:
+        return await super().connect(conninfo, **kwargs)  # type: ignore[no-any-return]
 
 
 class CrdbConnectionInfo(ConnectionInfo):
index 349f6ef5c7438aadda48b095af9f38a1fa54ee84..4346657cee12525e0c9bc62cc2059e368d7f5adf 100644 (file)
@@ -7,7 +7,7 @@ psycopg cursor objects
 from functools import partial
 from types import TracebackType
 from typing import Any, Generic, Iterable, Iterator, List
-from typing import Optional, NoReturn, Sequence, Tuple, Type, TypeVar
+from typing import Optional, NoReturn, Sequence, Tuple, Type
 from typing import overload, TYPE_CHECKING
 from warnings import warn
 from contextlib import contextmanager
@@ -19,6 +19,7 @@ from .abc import ConnectionType, Query, Params, PQGen
 from .copy import Copy, Writer as CopyWriter
 from .rows import Row, RowMaker, RowFactory
 from ._column import Column
+from ._compat import Self
 from .pq.misc import connection_summary
 from ._queries import PostgresQuery, PostgresClientQuery
 from ._pipeline import Pipeline
@@ -662,7 +663,6 @@ class BaseCursor(Generic[ConnectionType, Row]):
 class Cursor(BaseCursor["Connection[Any]", Row]):
     __module__ = "psycopg"
     __slots__ = ()
-    _Self = TypeVar("_Self", bound="Cursor[Any]")
 
     @overload
     def __init__(self: "Cursor[Row]", connection: "Connection[Row]"):
@@ -686,7 +686,7 @@ class Cursor(BaseCursor["Connection[Any]", Row]):
         super().__init__(connection)
         self._row_factory = row_factory or connection.row_factory
 
-    def __enter__(self: _Self) -> _Self:
+    def __enter__(self) -> Self:
         return self
 
     def __exit__(
@@ -718,13 +718,13 @@ class Cursor(BaseCursor["Connection[Any]", Row]):
         return self._row_factory(self)
 
     def execute(
-        self: _Self,
+        self,
         query: Query,
         params: Optional[Params] = None,
         *,
         prepare: Optional[bool] = None,
         binary: Optional[bool] = None,
-    ) -> _Self:
+    ) -> Self:
         """
         Execute a query or command to the database.
         """
index 58fce6420a7fef0bc1d04d39a60c616c0eea81a4..842de0f70369ee1fe218d6826b039a6573d03c6a 100644 (file)
@@ -6,7 +6,7 @@ psycopg async cursor objects
 
 from types import TracebackType
 from typing import Any, AsyncIterator, Iterable, List
-from typing import Optional, Type, TypeVar, TYPE_CHECKING, overload
+from typing import Optional, Type, TYPE_CHECKING, overload
 from contextlib import asynccontextmanager
 
 from . import pq
@@ -15,6 +15,7 @@ from .abc import Query, Params
 from .copy import AsyncCopy, AsyncWriter as AsyncCopyWriter
 from .rows import Row, RowMaker, AsyncRowFactory
 from .cursor import BaseCursor
+from ._compat import Self
 from ._pipeline import Pipeline
 
 if TYPE_CHECKING:
@@ -26,7 +27,6 @@ ACTIVE = pq.TransactionStatus.ACTIVE
 class AsyncCursor(BaseCursor["AsyncConnection[Any]", Row]):
     __module__ = "psycopg"
     __slots__ = ()
-    _Self = TypeVar("_Self", bound="AsyncCursor[Any]")
 
     @overload
     def __init__(self: "AsyncCursor[Row]", connection: "AsyncConnection[Row]"):
@@ -50,7 +50,7 @@ class AsyncCursor(BaseCursor["AsyncConnection[Any]", Row]):
         super().__init__(connection)
         self._row_factory = row_factory or connection.row_factory
 
-    async def __aenter__(self: _Self) -> _Self:
+    async def __aenter__(self) -> Self:
         return self
 
     async def __aexit__(
@@ -78,13 +78,13 @@ class AsyncCursor(BaseCursor["AsyncConnection[Any]", Row]):
         return self._row_factory(self)
 
     async def execute(
-        self: _Self,
+        self,
         query: Query,
         params: Optional[Params] = None,
         *,
         prepare: Optional[bool] = None,
         binary: Optional[bool] = None,
-    ) -> _Self:
+    ) -> Self:
         try:
             async with self._conn.lock:
                 await self._conn.wait(
index f86f3bdcb1e00cdfcf86446d7d60131a388275b2..1d7c01ac0c8f8237df27c57a6576688be384a7b3 100644 (file)
@@ -30,8 +30,9 @@ Suggested usage::
 
 import inspect
 import logging
-from typing import Any, Callable, Type, TypeVar, TYPE_CHECKING
+from typing import Any, Callable, TypeVar, TYPE_CHECKING
 from functools import wraps
+from .._compat import Self
 
 from . import PGconn
 from .misc import connection_summary
@@ -47,7 +48,6 @@ logger = logging.getLogger("psycopg.debug")
 class PGconnDebug:
     """Wrapper for a PQconn logging all its access."""
 
-    _Self = TypeVar("_Self", bound="PGconnDebug")
     _pgconn: "abc.PGconn"
 
     def __init__(self, pgconn: "abc.PGconn"):
@@ -71,11 +71,11 @@ class PGconnDebug:
         logger.info("PGconn.%s <- %s", attr, value)
 
     @classmethod
-    def connect(cls: Type[_Self], conninfo: bytes) -> _Self:
+    def connect(cls, conninfo: bytes) -> Self:
         return cls(debugging(PGconn.connect)(conninfo))
 
     @classmethod
-    def connect_start(cls: Type[_Self], conninfo: bytes) -> _Self:
+    def connect_start(cls, conninfo: bytes) -> Self:
         return cls(debugging(PGconn.connect_start)(conninfo))
 
     @classmethod
index 7a86e599d45c6bdb225ca311091c36a351b8b3e0..013a10b8d34f032a4d48e2682eb532c2b4867177 100644 (file)
@@ -5,7 +5,7 @@ psycopg server-side cursor objects.
 # Copyright (C) 2020 The Psycopg Team
 
 from typing import Any, AsyncIterator, List, Iterable, Iterator
-from typing import Optional, TypeVar, TYPE_CHECKING, overload
+from typing import Optional, TYPE_CHECKING, overload
 from warnings import warn
 
 from . import pq
@@ -14,6 +14,7 @@ from . import errors as e
 from .abc import ConnectionType, Query, Params, PQGen
 from .rows import Row, RowFactory, AsyncRowFactory
 from .cursor import BaseCursor, Cursor
+from ._compat import Self
 from .generators import execute
 from .cursor_async import AsyncCursor
 
@@ -211,7 +212,6 @@ class ServerCursorMixin(BaseCursor[ConnectionType, Row]):
 class ServerCursor(ServerCursorMixin["Connection[Any]", Row], Cursor[Row]):
     __module__ = "psycopg"
     __slots__ = ()
-    _Self = TypeVar("_Self", bound="ServerCursor[Any]")
 
     @overload
     def __init__(
@@ -270,13 +270,13 @@ class ServerCursor(ServerCursorMixin["Connection[Any]", Row], Cursor[Row]):
             super().close()
 
     def execute(
-        self: _Self,
+        self,
         query: Query,
         params: Optional[Params] = None,
         *,
         binary: Optional[bool] = None,
         **kwargs: Any,
-    ) -> _Self:
+    ) -> Self:
         """
         Open a cursor to execute a query to the database.
         """
@@ -353,7 +353,6 @@ class AsyncServerCursor(
 ):
     __module__ = "psycopg"
     __slots__ = ()
-    _Self = TypeVar("_Self", bound="AsyncServerCursor[Any]")
 
     @overload
     def __init__(
@@ -409,13 +408,13 @@ class AsyncServerCursor(
             await super().close()
 
     async def execute(
-        self: _Self,
+        self,
         query: Query,
         params: Optional[Params] = None,
         *,
         binary: Optional[bool] = None,
         **kwargs: Any,
-    ) -> _Self:
+    ) -> Self:
         if kwargs:
             raise TypeError(f"keyword not supported: {list(kwargs)[0]}")
         if self._pgconn.pipeline_status:
index fae3c2ab557b5d746be71432abd69a53b9b06a20..c6405aa438ea25e6aae9965240ae9713ea59be57 100644 (file)
@@ -7,12 +7,13 @@ Transaction context managers returned by Connection.transaction()
 import logging
 
 from types import TracebackType
-from typing import Generic, Iterator, Optional, Type, Union, TypeVar, TYPE_CHECKING
+from typing import Generic, Iterator, Optional, Type, Union, TYPE_CHECKING
 
 from . import pq
 from . import sql
 from . import errors as e
 from .abc import ConnectionType, PQGen
+from ._compat import Self
 from .pq.misc import connection_summary
 
 if TYPE_CHECKING:
@@ -235,14 +236,12 @@ class Transaction(BaseTransaction["Connection[Any]"]):
 
     __module__ = "psycopg"
 
-    _Self = TypeVar("_Self", bound="Transaction")
-
     @property
     def connection(self) -> "Connection[Any]":
         """The connection the object is managing."""
         return self._conn
 
-    def __enter__(self: _Self) -> _Self:
+    def __enter__(self) -> Self:
         with self._conn.lock:
             self._conn.wait(self._enter_gen())
         return self
@@ -267,13 +266,11 @@ class AsyncTransaction(BaseTransaction["AsyncConnection[Any]"]):
 
     __module__ = "psycopg"
 
-    _Self = TypeVar("_Self", bound="AsyncTransaction")
-
     @property
     def connection(self) -> "AsyncConnection[Any]":
         return self._conn
 
-    async def __aenter__(self: _Self) -> _Self:
+    async def __aenter__(self) -> Self:
         async with self._conn.lock:
             await self._conn.wait(self._enter_gen())
         return self
index 9fb2b9b566a61abbb23a5a24b126f268edf0aa8a..3f94efd611327b2f9942caada57017646a829d3d 100644 (file)
@@ -32,9 +32,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",
     "Task",
     "create_task",
 ]
index 8254c593496f5698bf7740ae44b3dd2330ef56ed..6d0dcbab4d34e817b53e6c34acd359edfd9276c1 100644 (file)
@@ -11,7 +11,7 @@ from time import monotonic
 from queue import Queue, Empty
 from types import TracebackType
 from typing import Any, Callable, Dict, Iterator, List
-from typing import Optional, Sequence, Type, TypeVar
+from typing import Optional, Sequence, Type
 from weakref import ref
 from contextlib import contextmanager
 
@@ -22,14 +22,12 @@ from psycopg.pq import TransactionStatus
 from .base import ConnectionAttempt, BasePool
 from .sched import Scheduler
 from .errors import PoolClosed, PoolTimeout, TooManyRequests
-from ._compat import Deque
+from ._compat import Deque, Self
 
 logger = logging.getLogger("psycopg.pool")
 
 
 class ConnectionPool(BasePool[Connection[Any]]):
-    _Self = TypeVar("_Self", bound="ConnectionPool")
-
     def __init__(
         self,
         conninfo: str = "",
@@ -385,7 +383,7 @@ class ConnectionPool(BasePool[Connection[Any]]):
                         timeout,
                     )
 
-    def __enter__(self: _Self) -> _Self:
+    def __enter__(self) -> Self:
         self.open()
         return self
 
index e08f0c49484f2e9e4562b27ad079a0f51731bc60..6454f47e6bd64be7d419c1912254bad47e0d78ef 100644 (file)
@@ -10,7 +10,7 @@ from abc import ABC, abstractmethod
 from time import monotonic
 from types import TracebackType
 from typing import Any, AsyncIterator, Awaitable, Callable
-from typing import Dict, List, Optional, Sequence, Type, TypeVar
+from typing import Dict, List, Optional, Sequence, Type
 from weakref import ref
 from contextlib import asynccontextmanager
 
@@ -21,14 +21,12 @@ from psycopg.pq import TransactionStatus
 from .base import ConnectionAttempt, BasePool
 from .sched import AsyncScheduler
 from .errors import PoolClosed, PoolTimeout, TooManyRequests
-from ._compat import Task, create_task, Deque
+from ._compat import Task, create_task, Deque, Self
 
 logger = logging.getLogger("psycopg.pool")
 
 
 class AsyncConnectionPool(BasePool[AsyncConnection[Any]]):
-    _Self = TypeVar("_Self", bound="AsyncConnectionPool")
-
     def __init__(
         self,
         conninfo: str = "",
@@ -333,7 +331,7 @@ class AsyncConnectionPool(BasePool[AsyncConnection[Any]]):
                 timeout,
             )
 
-    async def __aenter__(self: _Self) -> _Self:
+    async def __aenter__(self) -> Self:
         await self.open()
         return self
 
index bc04073d638c5d0c975eb7c7bdedb624981db71b..a5344e29471688506e9ad26d2e633d236fe48153 100644 (file)
@@ -44,7 +44,7 @@ python_requires = >= 3.7
 packages = find:
 zip_safe = False
 install_requires =
-    typing-extensions >= 3.10
+    typing-extensions >= 4.0
 
 [options.package_data]
 psycopg_pool = py.typed