Skip to content

Add implementation of AsyncSession.run_sync() #1347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jnewbery
Copy link

Currently, sqlmodel.ext.asyncio.session.AsyncSession doesn't implement run_sync(), which means that any call to run_sync() on a sqlmodel AsyncSession will be dispatched to the parent
sqlalchemy.ext.asyncio.AsyncSession.

The first argument to sqlalchemy's AsyncSession.run_sync() is a callable whose first argument is a sqlalchemy.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 a
sqlmodel.orm.session.Session.

In practice this works fine - because sqlmodel.orm.session.Session is derived from sqlalchemy.orm.Session, the implementation of sqlalchemy.ext.asyncio.AsyncSession.run_sync() can use the sqlmodel Session object in place of the sqlalchemy Session object. However, static analysers will complain that the argument to run_sync() is of the wrong type. For example, here's a warning from pyright:

Pyright: Error: Argument of type "(session: Session, id: UUID) -> int" cannot be assigned to parameter "fn" of type "(Session, **_P@run_sync) -> _T@run_sync" in function "run_sync"
  Type "(session: Session, id: UUID) -> int" is not assignable to type "(Session, id: UUID) -> int"
    Parameter 1: type "Session" is incompatible with type "Session"
      "sqlalchemy.orm.session.Session" is not assignable to "sqlmodel.orm.session.Session" [reportArgumentType]

This commit implements a run_sync() method on
sqlmodel.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.

jnewbery and others added 2 commits April 24, 2025 17:19
Currently, `sqlmodel.ext.asyncio.session.AsyncSession` doesn't implement
`run_sync()`, which means that any call to `run_sync()` on a sqlmodel
`AsyncSession` will be dispatched to the parent
`sqlalchemy.ext.asyncio.AsyncSession`.

The first argument to sqlalchemy's `AsyncSession.run_sync()` is a
callable whose first argument is a `sqlalchemy.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 a
`sqlmodel.orm.session.Session`.

In practice this works fine - because `sqlmodel.orm.session.Session` is
derived from `sqlalchemy.orm.Session`, the implementation of
`sqlalchemy.ext.asyncio.AsyncSession.run_sync()` can use the sqlmodel
`Session` object in place of the sqlalchemy `Session` object. However,
static analysers will complain that the argument to `run_sync()` is of
the wrong type. For example, here's a warning from pyright:

```
Pyright: Error: Argument of type "(session: Session, id: UUID) -> int" cannot be assigned to parameter "fn" of type "(Session, **_P@run_sync) -> _T@run_sync" in function "run_sync"
  Type "(session: Session, id: UUID) -> int" is not assignable to type "(Session, id: UUID) -> int"
    Parameter 1: type "Session" is incompatible with type "Session"
      "sqlalchemy.orm.session.Session" is not assignable to "sqlmodel.orm.session.Session" [reportArgumentType]
```

This commit implements a `run_sync()` method on
`sqlmodel.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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant