From: Daniele Varrazzo Date: Mon, 8 Nov 2021 15:00:18 +0000 (+0100) Subject: Add Generic Counter and Deque to compat module X-Git-Tag: 3.0.3~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1a5b09b2533fc4000c01699d4572066e602eb0d2;p=thirdparty%2Fpsycopg.git Add Generic Counter and Deque to compat module --- diff --git a/psycopg/psycopg/_compat.py b/psycopg/psycopg/_compat.py index 7dd011370..0576b71ad 100644 --- a/psycopg/psycopg/_compat.py +++ b/psycopg/psycopg/_compat.py @@ -51,10 +51,14 @@ else: if sys.version_info >= (3, 9): from zoneinfo import ZoneInfo + from collections import Counter, deque as Deque else: from backports.zoneinfo import ZoneInfo + from typing import Counter, Deque __all__ = [ + "Counter", + "Deque", "Protocol", "ZoneInfo", "asynccontextmanager", diff --git a/psycopg_pool/psycopg_pool/base.py b/psycopg_pool/psycopg_pool/base.py index 0227b2e0d..afb4bc533 100644 --- a/psycopg_pool/psycopg_pool/base.py +++ b/psycopg_pool/psycopg_pool/base.py @@ -5,14 +5,10 @@ psycopg connection pool base class and functionalities. # Copyright (C) 2021 The Psycopg Team from random import random -from typing import Any, Callable, Deque, Dict, Generic, Optional -from typing import TYPE_CHECKING -from collections import Counter, deque +from typing import Any, Callable, Dict, Generic, Optional from psycopg.abc import ConnectionType - -if TYPE_CHECKING: - from typing import Counter as TCounter +from psycopg._compat import Counter, Deque class BasePool(Generic[ConnectionType]): @@ -81,8 +77,8 @@ class BasePool(Generic[ConnectionType]): self.num_workers = num_workers self._nconns = min_size # currently in the pool, out, being prepared - self._pool: Deque[ConnectionType] = deque() - self._stats: "TCounter[str]" = Counter() + self._pool = Deque[ConnectionType]() + self._stats = Counter[str]() # Min number of connections in the pool in a max_idle unit of time. # It is reset periodically by the ShrinkPool scheduled task. diff --git a/psycopg_pool/psycopg_pool/pool.py b/psycopg_pool/psycopg_pool/pool.py index e52b5e047..27cc35fb7 100644 --- a/psycopg_pool/psycopg_pool/pool.py +++ b/psycopg_pool/psycopg_pool/pool.py @@ -10,15 +10,15 @@ from abc import ABC, abstractmethod from time import monotonic from queue import Queue, Empty from types import TracebackType -from typing import Any, Callable, Deque, Dict, Iterator, List +from typing import Any, Callable, Dict, Iterator, List from typing import Optional, Type from weakref import ref from contextlib import contextmanager -from collections import deque from psycopg import errors as e from psycopg import Connection from psycopg.pq import TransactionStatus +from psycopg._compat import Deque from .base import ConnectionAttempt, BasePool from .sched import Scheduler @@ -42,7 +42,7 @@ class ConnectionPool(BasePool[Connection[Any]]): self._reset = reset self._lock = threading.RLock() - self._waiting: Deque["WaitingClient"] = deque() + self._waiting = Deque["WaitingClient"]() # to notify that the pool is full self._pool_full_event: Optional[threading.Event] = None @@ -534,8 +534,6 @@ class ConnectionPool(BasePool[Connection[Any]]): # Also disable the warning for open connection in conn.__del__ conn._pool = None - pos: Optional[WaitingClient] = None - # Critical section: if there is a client waiting give it the connection # otherwise put it back into the pool. with self._lock: diff --git a/psycopg_pool/psycopg_pool/pool_async.py b/psycopg_pool/psycopg_pool/pool_async.py index 965e930b2..ae1612978 100644 --- a/psycopg_pool/psycopg_pool/pool_async.py +++ b/psycopg_pool/psycopg_pool/pool_async.py @@ -10,14 +10,13 @@ import logging from abc import ABC, abstractmethod from time import monotonic from types import TracebackType -from typing import Any, AsyncIterator, Awaitable, Callable, Deque +from typing import Any, AsyncIterator, Awaitable, Callable from typing import Dict, List, Optional, Type from weakref import ref -from collections import deque from psycopg import errors as e from psycopg.pq import TransactionStatus -from psycopg._compat import Task, asynccontextmanager, create_task +from psycopg._compat import Task, asynccontextmanager, create_task, Deque from psycopg.connection_async import AsyncConnection from .base import ConnectionAttempt, BasePool @@ -52,7 +51,7 @@ class AsyncConnectionPool(BasePool[AsyncConnection[Any]]): self._reset = reset self._lock = asyncio.Lock() - self._waiting: Deque["AsyncClient"] = deque() + self._waiting = Deque["AsyncClient"]() # to notify that the pool is full self._pool_full_event: Optional[asyncio.Event] = None @@ -464,8 +463,6 @@ class AsyncConnectionPool(BasePool[AsyncConnection[Any]]): # Also disable the warning for open connection in conn.__del__ conn._pool = None - pos: Optional[AsyncClient] = None - # Critical section: if there is a client waiting give it the connection # otherwise put it back into the pool. async with self._lock: diff --git a/tests/fix_faker.py b/tests/fix_faker.py index 8a9a3529d..1885e161a 100644 --- a/tests/fix_faker.py +++ b/tests/fix_faker.py @@ -6,14 +6,13 @@ from uuid import UUID from random import choice, random, randrange from decimal import Decimal from contextlib import contextmanager -from collections import deque import pytest import psycopg from psycopg import sql from psycopg.adapt import PyFormat -from psycopg._compat import asynccontextmanager +from psycopg._compat import asynccontextmanager, Deque from psycopg.types.range import Range from psycopg.types.numeric import Int4, Int8 from psycopg.types.multirange import Multirange @@ -833,7 +832,7 @@ class JsonFloat: def deep_import(name): - parts = deque(name.split(".")) + parts = Deque(name.split(".")) seen = [] if not parts: raise ValueError("name must be a dot-separated name") diff --git a/tests/pool/test_pool.py b/tests/pool/test_pool.py index 22353d750..dfeca1d81 100644 --- a/tests/pool/test_pool.py +++ b/tests/pool/test_pool.py @@ -3,13 +3,13 @@ import logging import weakref from time import sleep, time from threading import Thread, Event -from collections import Counter from typing import Any, List, Tuple import pytest import psycopg from psycopg.pq import TransactionStatus +from psycopg._compat import Counter pytestmark = [] @@ -817,7 +817,7 @@ def test_uniform_use(dsn, retries): for retry in retries: with retry: with pool.ConnectionPool(dsn, min_size=4) as p: - counts = Counter() # type: Counter[int] + counts = Counter[int]() for i in range(8): with p.connection() as conn: sleep(0.1) diff --git a/tests/pool/test_pool_async.py b/tests/pool/test_pool_async.py index 6bc68f25c..a05fe8283 100644 --- a/tests/pool/test_pool_async.py +++ b/tests/pool/test_pool_async.py @@ -2,14 +2,13 @@ import sys import asyncio import logging from time import time -from collections import Counter from typing import Any, List, Tuple import pytest import psycopg from psycopg.pq import TransactionStatus -from psycopg._compat import create_task +from psycopg._compat import create_task, Counter pytestmark = [ pytest.mark.asyncio, @@ -812,7 +811,7 @@ async def test_uniform_use(dsn, retries): async for retry in retries: with retry: async with pool.AsyncConnectionPool(dsn, min_size=4) as p: - counts = Counter() # type: Counter[int] + counts = Counter[int]() for i in range(8): async with p.connection() as conn: await asyncio.sleep(0.1)