summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2022-05-26 14:35:03 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2022-05-31 15:17:48 -0400
commitd24cd5e96d7f8e47c86b5013a7f989a15e2eec89 (patch)
tree4291dbaeea6b78164e492da183cff5e8e7dfd9d6 /examples
parent5531cec630ee75bfd7f5848cfe622c769be5ae48 (diff)
downloadsqlalchemy-d24cd5e96d7f8e47c86b5013a7f989a15e2eec89.tar.gz
establish sessionmaker and async_sessionmaker as generic
This is so that custom Session and AsyncSession classes can be typed for these factories. Added appropriate typevars to `__call__()`, `__enter__()` and other methods so that a custom Session or AsyncSession subclass is carried through. Fixes: #7656 Change-Id: Ia2b8c1f22b4410db26005c3285f6ba3d13d7f0e0
Diffstat (limited to 'examples')
-rw-r--r--examples/asyncio/gather_orm_statements.py11
1 files changed, 4 insertions, 7 deletions
diff --git a/examples/asyncio/gather_orm_statements.py b/examples/asyncio/gather_orm_statements.py
index edcdc1fe8..a67b5e669 100644
--- a/examples/asyncio/gather_orm_statements.py
+++ b/examples/asyncio/gather_orm_statements.py
@@ -22,12 +22,11 @@ import random
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import String
-from sqlalchemy.ext.asyncio import AsyncSession
+from sqlalchemy.ext.asyncio import async_sessionmaker
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.future import select
from sqlalchemy.orm import merge_frozen_result
-from sqlalchemy.orm import sessionmaker
Base = declarative_base()
@@ -40,14 +39,14 @@ class A(Base):
async def run_out_of_band(
- sessionmaker, session, statement, merge_results=True
+ async_sessionmaker, session, statement, merge_results=True
):
"""run an ORM statement in a distinct session, merging the result
back into the given session.
"""
- async with sessionmaker() as oob_session:
+ async with async_sessionmaker() as oob_session:
# use AUTOCOMMIT for each connection to reduce transaction
# overhead / contention
@@ -94,9 +93,7 @@ async def async_main():
await conn.run_sync(Base.metadata.drop_all)
await conn.run_sync(Base.metadata.create_all)
- async_session = sessionmaker(
- engine, expire_on_commit=False, class_=AsyncSession
- )
+ async_session = async_sessionmaker(engine, expire_on_commit=False)
async with async_session() as session, session.begin():
session.add_all([A(data="a_%d" % i) for i in range(100)])