From: Daniele Varrazzo Date: Sat, 30 Dec 2023 00:30:39 +0000 (+0100) Subject: refactor: use typing.Self X-Git-Tag: pool-3.2.1~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e4adfe6be4cf220f8d4708837234a222d27393e0;p=thirdparty%2Fpsycopg.git refactor: use typing.Self 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]`. --- diff --git a/docs/news_pool.rst b/docs/news_pool.rst index 8db413aad..ec758550d 100644 --- a/docs/news_pool.rst +++ b/docs/news_pool.rst @@ -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`). diff --git a/psycopg_pool/psycopg_pool/_compat.py b/psycopg_pool/psycopg_pool/_compat.py index c3e27f02b..e88cf2b3d 100644 --- a/psycopg_pool/psycopg_pool/_compat.py +++ b/psycopg_pool/psycopg_pool/_compat.py @@ -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. diff --git a/psycopg_pool/psycopg_pool/pool.py b/psycopg_pool/psycopg_pool/pool.py index 288473fc0..1e298518b 100644 --- a/psycopg_pool/psycopg_pool/pool.py +++ b/psycopg_pool/psycopg_pool/pool.py @@ -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 diff --git a/psycopg_pool/psycopg_pool/pool_async.py b/psycopg_pool/psycopg_pool/pool_async.py index 25cec3f95..d53243ff9 100644 --- a/psycopg_pool/psycopg_pool/pool_async.py +++ b/psycopg_pool/psycopg_pool/pool_async.py @@ -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 diff --git a/psycopg_pool/setup.cfg b/psycopg_pool/setup.cfg index f439e8bc2..8c3de4ba1 100644 --- a/psycopg_pool/setup.cfg +++ b/psycopg_pool/setup.cfg @@ -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