Commit 674d759
committed
Add implementation of AsyncSession.run_sync()
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.1 parent 6c0410e commit 674d759
1 file changed
+19
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
3 | 5 | | |
4 | 6 | | |
5 | 7 | | |
| 8 | + | |
6 | 9 | | |
7 | 10 | | |
8 | 11 | | |
| |||
17 | 20 | | |
18 | 21 | | |
19 | 22 | | |
| 23 | + | |
20 | 24 | | |
21 | 25 | | |
22 | 26 | | |
| |||
26 | 30 | | |
27 | 31 | | |
28 | 32 | | |
| 33 | + | |
29 | 34 | | |
30 | 35 | | |
31 | 36 | | |
| |||
148 | 153 | | |
149 | 154 | | |
150 | 155 | | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
0 commit comments