Skip to content

Commit

Permalink
Release 1.0.post0
Browse files Browse the repository at this point in the history
* ``sessionmaker`` replaced on ``async_sessionmaker``
  • Loading branch information
ri-gilfanov committed Sep 15, 2024
1 parent 891ec68 commit 2deeb0c
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 25 deletions.
3 changes: 2 additions & 1 deletion docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
10 changes: 8 additions & 2 deletions docs/releases.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
========
Releases
========
Version 1.0.post0
-----------------
**Changed**

* ``sessionmaker`` replaced on ``async_sessionmaker``.

Version 1.0.0
-------------
**Added**
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
5 changes: 3 additions & 2 deletions src/aiohttp_sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 3 additions & 4 deletions src/aiohttp_sqlalchemy/typedefs.py
Original file line number Diff line number Diff line change
@@ -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]
5 changes: 2 additions & 3 deletions src/aiohttp_sqlalchemy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/test_bind.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
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
from aiohttp_sqlalchemy import DuplicateAppKeyError


async def test_duplicate_app_key_error(
session_factory: sessionmaker[Session],
session_factory: async_sessionmaker[AsyncSession],
) -> None:
with pytest.raises(DuplicateAppKeyError):
aiohttp_sqlalchemy.setup(
Expand Down
5 changes: 2 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit 2deeb0c

Please sign in to comment.