Add implementation of AsyncSession.run_sync() #1347
+17
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently,
sqlmodel.ext.asyncio.session.AsyncSession
doesn't implementrun_sync()
, which means that any call torun_sync()
on a sqlmodelAsyncSession
will be dispatched to the parentsqlalchemy.ext.asyncio.AsyncSession
.The first argument to sqlalchemy's
AsyncSession.run_sync()
is a callable whose first argument is asqlalchemy.orm.Session
object. If we're using this in a repo that uses sqlmodel, we'll actually be passing a callable whose first argument is asqlmodel.orm.session.Session
.In practice this works fine - because
sqlmodel.orm.session.Session
is derived fromsqlalchemy.orm.Session
, the implementation ofsqlalchemy.ext.asyncio.AsyncSession.run_sync()
can use the sqlmodelSession
object in place of the sqlalchemySession
object. However, static analysers will complain that the argument torun_sync()
is of the wrong type. For example, here's a warning from pyright:This commit implements a
run_sync()
method onsqlmodel.ext.asyncio.session.AsyncSession
, which casts the callable to the correct type before dispatching it to the base class. This satisfies the static type checks.