From cc7aee45493c181c21a95dc78725558bb562e0d3 Mon Sep 17 00:00:00 2001 From: Gorshkov Nikolay Date: Tue, 10 Mar 2026 13:42:28 +0500 Subject: [PATCH] feat: switch on peewee 4.0 --- peewee_async/__init__.py | 18 ++++++------ peewee_async/databases.py | 58 ++++++++++----------------------------- pyproject.toml | 8 +++--- tests/conftest.py | 3 +- tests/db_config.py | 10 +++---- tests/test_database.py | 29 +------------------- 6 files changed, 32 insertions(+), 94 deletions(-) diff --git a/peewee_async/__init__.py b/peewee_async/__init__.py index 4e00f5b..b9a7f64 100644 --- a/peewee_async/__init__.py +++ b/peewee_async/__init__.py @@ -21,10 +21,9 @@ from peewee_async.aio_model import AioModel, aio_prefetch from peewee_async.connection import connection_context from peewee_async.databases import ( - PooledMySQLDatabase, - PooledPostgresqlDatabase, - PooledPostgresqlExtDatabase, - PsycopgDatabase, + MySQLDatabase, + PostgresqlDatabase, + Psycopg3Database, ) from peewee_async.pool import MysqlPoolBackend, PostgresqlPoolBackend from peewee_async.transactions import Transaction @@ -34,8 +33,8 @@ __all__ = [ "PooledPostgresqlDatabase", - "PooledPostgresqlExtDatabase", - "PooledMySQLDatabase", + "PostgresqlDatabase", + "MySQLDatabase", "Transaction", "AioModel", "aio_prefetch", @@ -44,7 +43,6 @@ "MysqlPoolBackend", ] -register_database(PooledPostgresqlDatabase, "postgres+pool+async", "postgresql+pool+async") -register_database(PooledPostgresqlExtDatabase, "postgresext+pool+async", "postgresqlext+pool+async") -register_database(PsycopgDatabase, "psycopg+pool+async", "psycopg+pool+async") -register_database(PooledMySQLDatabase, "mysql+pool+async") +register_database(PostgresqlDatabase, "postgres+pool+async", "postgresql+pool+async") +register_database(Psycopg3Database, "psycopg+pool+async", "psycopg+pool+async") +register_database(MySQLDatabase, "mysql+pool+async") diff --git a/peewee_async/databases.py b/peewee_async/databases.py index 5b1bc34..114d338 100644 --- a/peewee_async/databases.py +++ b/peewee_async/databases.py @@ -1,12 +1,10 @@ import contextlib -import warnings from collections.abc import AsyncIterator, Iterator from contextlib import AbstractAsyncContextManager from typing import Any import peewee from playhouse import postgres_ext as ext -from playhouse.psycopg3_ext import Psycopg3Database from .connection import ConnectionContextManager, connection_context from .pool import MysqlPoolBackend, PoolBackend, PostgresqlPoolBackend, PsycopgPoolBackend @@ -22,17 +20,16 @@ class AioDatabase(peewee.Database): Example:: - database = PooledPostgresqlExtDatabase( + database = Psycopg3Database( 'database': 'postgres', 'host': '127.0.0.1', - 'port':5432, + 'port': 5432, 'password': 'postgres', 'user': 'postgres', 'pool_params': { - "minsize": 0, - "maxsize": 5, - "timeout": 30, - 'pool_recycle': 1.5 + "min_size": 0, + "max_size": 5, + 'max_lifetime': 15 } ) @@ -54,18 +51,6 @@ def init_pool_params_defaults(self) -> None: def init_pool_params(self) -> None: self.init_pool_params_defaults() - if "min_connections" in self.connect_params or "max_connections" in self.connect_params: - warnings.warn( - "`min_connections` and `max_connections` are deprecated, use `pool_params` instead.", - DeprecationWarning, - stacklevel=2, - ) - self.pool_params.update( - { - "minsize": self.connect_params.pop("min_connections", 1), - "maxsize": self.connect_params.pop("max_connections", 20), - } - ) pool_params = self.connect_params.pop("pool_params", {}) self.pool_params.update(pool_params) self.pool_params.update(self.connect_params) @@ -194,13 +179,13 @@ async def aio_execute(self, query: Any, fetch_results: FetchResults | None = Non return await self.aio_execute_sql(sql, params, fetch_results=fetch_results) -class PsycopgDatabase(AioDatabase, Psycopg3Database): - """Extension for `peewee.PostgresqlDatabase` providing extra methods +class Psycopg3Database(AioDatabase, ext.Psycopg3Database): + """Extension for `playhouse.Psycopg3Database` providing extra methods for managing async connection based on psycopg3 pool backend. Example:: - database = PsycopgDatabase( + database = Psycopg3Database( 'database': 'postgres', 'host': '127.0.0.1', 'port': 5432, @@ -225,14 +210,14 @@ def init(self, database: str | None, **kwargs: Any) -> None: super().init(database, **kwargs) -class PooledPostgresqlDatabase(AioDatabase, peewee.PostgresqlDatabase): - """Extension for `peewee.PostgresqlDatabase` providing extra methods +class PostgresqlDatabase(AioDatabase, ext.PostgresqlExtDatabase): + """Extension for `playhouse.PostgresqlDatabase` providing extra methods for managing async connection based on aiopg pool backend. Example:: - database = PooledPostgresqlExtDatabase( + database = PostgresqlDatabase( 'database': 'postgres', 'host': '127.0.0.1', 'port':5432, @@ -253,7 +238,7 @@ class PooledPostgresqlDatabase(AioDatabase, peewee.PostgresqlDatabase): pool_backend_cls = PostgresqlPoolBackend def init_pool_params_defaults(self) -> None: - self.pool_params.update({"enable_json": False, "enable_hstore": False}) + self.pool_params.update({"enable_json": True, "enable_hstore": self._register_hstore}) def init(self, database: str | None, **kwargs: Any) -> None: if not aiopg: @@ -261,28 +246,13 @@ def init(self, database: str | None, **kwargs: Any) -> None: super().init(database, **kwargs) -class PooledPostgresqlExtDatabase(PooledPostgresqlDatabase, ext.PostgresqlExtDatabase): - """PosgtreSQL database extended driver providing **single drop-in sync** - connection and **async connections pool** interface based on aiopg pool backend. - - JSON fields support is enabled by default, HStore supports is disabled by - default, but can be enabled through pool_params or with ``register_hstore=False`` argument. - - See also: - https://peewee.readthedocs.io/en/latest/peewee/playhouse.html#PostgresqlExtDatabase - """ - - def init_pool_params_defaults(self) -> None: - self.pool_params.update({"enable_json": True, "enable_hstore": self._register_hstore}) - - -class PooledMySQLDatabase(AioDatabase, peewee.MySQLDatabase): +class MySQLDatabase(AioDatabase, peewee.MySQLDatabase): """MySQL database driver providing **single drop-in sync** connection and **async connections pool** interface. Example:: - database = PooledMySQLDatabase( + database = MySQLDatabase( 'database': 'mysql', 'host': '127.0.0.1', 'port': 3306, diff --git a/pyproject.toml b/pyproject.toml index 8a95260..7d3db4a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,7 +9,7 @@ authors = [ requires-python = ">=3.10" readme = "README.md" dependencies = [ - "peewee>=3.15.4,<4", + "peewee>=4,<5", "typing-extensions>=4.12.2" ] @@ -19,12 +19,12 @@ postgresql = [ "aiopg>=1.4.0", ] mysql = [ - "aiomysql>=0.2.0", + "aiomysql>=0.3.2", "cryptography>=46.0.5" ] psycopg = [ - "psycopg>=3.2.0", - "psycopg-pool>=3.2.0" + "psycopg>=3.3.0", + "psycopg-pool>=3.3.0" ] docs = [ "Sphinx>=8.1.3", diff --git a/tests/conftest.py b/tests/conftest.py index fc1fa89..6bc8ffd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -90,8 +90,7 @@ async def db(request: pytest.FixtureRequest) -> AsyncGenerator[AioDatabase, None PG_DBS = [ - "postgres-pool", - "postgres-pool-ext", + "aiopg-pool", "psycopg-pool", ] diff --git a/tests/db_config.py b/tests/db_config.py index c40a1a8..942808d 100644 --- a/tests/db_config.py +++ b/tests/db_config.py @@ -31,15 +31,13 @@ } DB_DEFAULTS = { - "postgres-pool": PG_DEFAULTS, - "postgres-pool-ext": PG_DEFAULTS, + "aiopg-pool": PG_DEFAULTS, "psycopg-pool": PSYCOPG_DEFAULTS, "mysql-pool": MYSQL_DEFAULTS, } DB_CLASSES = { - "postgres-pool": peewee_async.PooledPostgresqlDatabase, - "postgres-pool-ext": peewee_async.PooledPostgresqlExtDatabase, - "psycopg-pool": peewee_async.PsycopgDatabase, - "mysql-pool": peewee_async.PooledMySQLDatabase, + "aiopg-pool": peewee_async.PostgresqlDatabase, + "psycopg-pool": peewee_async.Psycopg3Database, + "mysql-pool": peewee_async.MySQLDatabase, } diff --git a/tests/test_database.py b/tests/test_database.py index ec83780..f1d44bf 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -76,22 +76,6 @@ async def test_deferred_init(db_name: str) -> None: await database.aio_close() -@pytest.mark.parametrize("db_name", ["postgres-pool", "postgres-pool-ext", "mysql-pool"]) -async def test_deprecated_min_max_connections_param(db_name: str) -> None: - default_params = DB_DEFAULTS[db_name].copy() - del default_params["pool_params"] - default_params["min_connections"] = 1 - default_params["max_connections"] = 3 - db_cls = DB_CLASSES[db_name] - database = db_cls(**default_params) - await database.aio_connect() - - assert database.pool_backend.pool.minsize == 1 # type: ignore - assert database.pool_backend.pool.maxsize == 3 # type: ignore - - await database.aio_close() - - @dbs_mysql async def test_mysql_params(db: AioDatabase) -> None: async with db.aio_connection() as connection_1: @@ -101,18 +85,7 @@ async def test_mysql_params(db: AioDatabase) -> None: assert db.pool_backend.pool.maxsize == 5 # type: ignore -@pytest.mark.parametrize("db", ["postgres-pool"], indirect=["db"]) -async def test_pg_json_hstore__params(db: AioDatabase) -> None: - await db.aio_connect() - assert db.pool_backend.pool._enable_json is False # type: ignore - assert db.pool_backend.pool._enable_hstore is False # type: ignore - assert db.pool_backend.pool._timeout == 30 # type: ignore - assert db.pool_backend.pool._recycle == 1.5 # type: ignore - assert db.pool_backend.pool.minsize == 0 # type: ignore - assert db.pool_backend.pool.maxsize == 5 # type: ignore - - -@pytest.mark.parametrize("db", ["postgres-pool-ext"], indirect=["db"]) +@pytest.mark.parametrize("db", ["aiopg-pool"], indirect=["db"]) async def test_pg_ext_json_hstore__params(db: AioDatabase) -> None: await db.aio_connect() assert db.pool_backend.pool._enable_json is True # type: ignore