diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 6fcff76..60150a8 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -74,10 +74,11 @@ More control in configuration import aiohttp_sqlalchemy as ahsa from sqlalchemy import orm + from sqlalchemy.ext.asyncio import async_sessionmaker url = 'sqlite+aiosqlite:///' engine = create_async_engine(url, echo=True) - Session = orm.sessionmaker(main_engine, class_=AsyncSession, expire_on_commit=False) + Session = orm.async_sessionmaker(main_engine, class_=AsyncSession, expire_on_commit=False) ahsa.setup(app, [ ahsa.bind(Session), diff --git a/docs/releases.rst b/docs/releases.rst index 681bf73..db5b2ec 100644 --- a/docs/releases.rst +++ b/docs/releases.rst @@ -1,6 +1,12 @@ ======== Releases ======== +Version 1.0.post0 +----------------- +**Changed** + +* ``sessionmaker`` replaced on ``async_sessionmaker``. + Version 1.0.0 ------------- **Added** @@ -374,7 +380,7 @@ Version 0.9 **Removed** * Removed support of ``AsyncEngine`` type in ``sa_bind()`` signature. Use - ``sessionmaker(engine, AsyncSession)`` or custom session factory returning + ``async_sessionmaker(engine, AsyncSession)`` or custom session factory returning ``AsyncSession`` instance. Version 0.8 @@ -386,7 +392,7 @@ Version 0.8 **Deprecated** * ``AsyncEngine`` type is deprecated in ``sa_bind()`` signature. Use - ``sessionmaker(engine, AsyncSession)`` or custom session factory returning + ``async_sessionmaker(engine, AsyncSession)`` or custom session factory returning ``AsyncSession`` instance. Version 0.7 diff --git a/pyproject.toml b/pyproject.toml index b61394b..6fb2675 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [tool.poetry] name = "aiohttp-sqlalchemy" packages = [{include = "aiohttp_sqlalchemy", from = "src" }] -version = "1.0.0" +version = "1.0.post0" description = "SQLAlchemy 2.0 support for aiohttp." license = "MIT" diff --git a/src/aiohttp_sqlalchemy/__init__.py b/src/aiohttp_sqlalchemy/__init__.py index 89b78c8..23ab960 100644 --- a/src/aiohttp_sqlalchemy/__init__.py +++ b/src/aiohttp_sqlalchemy/__init__.py @@ -10,9 +10,10 @@ from sqlalchemy.ext.asyncio import ( AsyncEngine, AsyncSession, + async_sessionmaker, create_async_engine, ) -from sqlalchemy.orm import Session, sessionmaker +from sqlalchemy.orm import Session from aiohttp_sqlalchemy.constants import DEFAULT_KEY, SA_DEFAULT_KEY from aiohttp_sqlalchemy.decorators import sa_decorator @@ -110,7 +111,7 @@ def bind( target = create_async_engine(target) if isinstance(target, AsyncEngine): - target = sessionmaker( # type: ignore + target = async_sessionmaker( bind=target, class_=AsyncSession, expire_on_commit=False, diff --git a/src/aiohttp_sqlalchemy/typedefs.py b/src/aiohttp_sqlalchemy/typedefs.py index 1e50fcc..a66dbed 100644 --- a/src/aiohttp_sqlalchemy/typedefs.py +++ b/src/aiohttp_sqlalchemy/typedefs.py @@ -1,12 +1,11 @@ from typing import Awaitable, Callable, Iterable, Tuple, Union from aiohttp.web import StreamResponse -from sqlalchemy.ext.asyncio import AsyncEngine -from sqlalchemy.orm import Session, sessionmaker +from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker THandler = Callable[..., Awaitable[StreamResponse]] THandlerWrapper = Callable[..., THandler] -TTarget = Union[str, AsyncEngine, sessionmaker[Session]] -TBind = Tuple[sessionmaker[Session], str, bool] +TTarget = Union[str, AsyncEngine, async_sessionmaker[AsyncSession]] +TBind = Tuple[async_sessionmaker[AsyncSession], str, bool] TBinds = Iterable[TBind] diff --git a/src/aiohttp_sqlalchemy/utils.py b/src/aiohttp_sqlalchemy/utils.py index b2a476c..c9aa6f3 100644 --- a/src/aiohttp_sqlalchemy/utils.py +++ b/src/aiohttp_sqlalchemy/utils.py @@ -3,13 +3,12 @@ from typing import TYPE_CHECKING from aiohttp.web import Application, Request -from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession +from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker from aiohttp_sqlalchemy.constants import SA_DEFAULT_KEY if TYPE_CHECKING: # pragma: no cover from sqlalchemy import MetaData - from sqlalchemy.orm import Session, sessionmaker async def init_db( @@ -64,7 +63,7 @@ def get_session( def get_session_factory( source: Request | Application, key: str = SA_DEFAULT_KEY, -) -> sessionmaker[Session]: +) -> async_sessionmaker[AsyncSession]: """Return callable object which returns an `AsyncSession` instance. :param source: AIOHTTP request object or your AIOHTTP application. diff --git a/tests/conftest.py b/tests/conftest.py index d9146f7..84eb195 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -12,9 +12,9 @@ from sqlalchemy.ext.asyncio import ( AsyncEngine, AsyncSession, + async_sessionmaker, create_async_engine, ) -from sqlalchemy.orm import Session, sessionmaker import aiohttp_sqlalchemy from aiohttp_sqlalchemy import SA_DEFAULT_KEY, sa_bind, sa_middleware @@ -45,12 +45,12 @@ def orm_async_engine() -> AsyncEngine: @pytest.fixture -def session_factory(orm_async_engine: AsyncEngine) -> sessionmaker[Session]: - return sessionmaker(orm_async_engine, class_=AsyncSession) # type: ignore +def session_factory(orm_async_engine: AsyncEngine) -> async_sessionmaker[AsyncSession]: + return async_sessionmaker(orm_async_engine, class_=AsyncSession) @pytest.fixture -def session(session_factory: sessionmaker[Session]) -> AsyncSession: +def session(session_factory: async_sessionmaker[AsyncSession]) -> AsyncSession: session = session_factory() if not isinstance(session, AsyncSession): raise TypeError @@ -63,7 +63,7 @@ def main_middleware() -> THandler: @pytest.fixture -def middlewared_app(session_factory: sessionmaker[Session]) -> Application: +def middlewared_app(session_factory: async_sessionmaker[AsyncSession]) -> Application: app = web.Application() aiohttp_sqlalchemy.setup(app, [sa_bind(session_factory)]) return app diff --git a/tests/test_bind.py b/tests/test_bind.py index 5634b8b..72c9369 100644 --- a/tests/test_bind.py +++ b/tests/test_bind.py @@ -2,7 +2,7 @@ import pytest from sqlalchemy import create_engine -from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession +from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker from sqlalchemy.orm import sessionmaker import aiohttp_sqlalchemy @@ -43,7 +43,7 @@ def test_bind_with_sync_session() -> None: def test_bind_to_async_session_maker(orm_async_engine: AsyncEngine) -> None: - Session = sessionmaker(orm_async_engine, class_=AsyncSession) # type: ignore + Session = async_sessionmaker(orm_async_engine, class_=AsyncSession) binding = aiohttp_sqlalchemy.bind(Session) Session = binding[0] session = Session() diff --git a/tests/test_setup.py b/tests/test_setup.py index 91fe20b..5518e87 100644 --- a/tests/test_setup.py +++ b/tests/test_setup.py @@ -6,7 +6,7 @@ from aiohttp import web if TYPE_CHECKING: # pragma: no cover - from sqlalchemy.orm import Session, sessionmaker + from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker import aiohttp_sqlalchemy @@ -14,7 +14,7 @@ async def test_duplicate_app_key_error( - session_factory: sessionmaker[Session], + session_factory: async_sessionmaker[AsyncSession], ) -> None: with pytest.raises(DuplicateAppKeyError): aiohttp_sqlalchemy.setup( diff --git a/tests/test_utils.py b/tests/test_utils.py index a50aadc..8a539df 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -4,13 +4,12 @@ import pytest import sqlalchemy as sa -from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession +from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker import aiohttp_sqlalchemy as ahsa if TYPE_CHECKING: # pragma: no cover from aiohttp.web import Application, Request - from sqlalchemy.orm import Session, sessionmaker async def test_db_init(middlewared_app: Application) -> None: @@ -37,7 +36,7 @@ def test_get_session(mocked_request: Request, session: AsyncSession) -> None: def test_get_session_factory( mocked_request: Request, middlewared_app: Application, - session_factory: sessionmaker[Session], + session_factory: async_sessionmaker[AsyncSession], ) -> None: assert ahsa.get_session_factory(mocked_request) is session_factory assert ahsa.get_session_factory(middlewared_app) is session_factory