✨ Upgrade SQLAlchemy to 2.0, including initial work by farahats9 (#700)
Co-authored-by: Mohamed Farahat <farahats9@yahoo.com> Co-authored-by: Stefan Borer <stefan.borer@gmail.com> Co-authored-by: Peter Landry <peter.landry@gmail.com>
This commit is contained in:
committed by
GitHub
parent
77c6fed305
commit
8ed856d322
@@ -1,16 +1,27 @@
|
||||
from typing import Any, Mapping, Optional, Sequence, Type, TypeVar, Union, overload
|
||||
from typing import (
|
||||
Any,
|
||||
Dict,
|
||||
Mapping,
|
||||
Optional,
|
||||
Sequence,
|
||||
TypeVar,
|
||||
Union,
|
||||
overload,
|
||||
)
|
||||
|
||||
from sqlalchemy import util
|
||||
from sqlalchemy.engine.interfaces import _CoreAnyExecuteParams
|
||||
from sqlalchemy.engine.result import Result, ScalarResult, TupleResult
|
||||
from sqlalchemy.orm import Query as _Query
|
||||
from sqlalchemy.orm import Session as _Session
|
||||
from sqlalchemy.orm._typing import OrmExecuteOptionsParameter
|
||||
from sqlalchemy.sql._typing import _ColumnsClauseArgument
|
||||
from sqlalchemy.sql.base import Executable as _Executable
|
||||
from typing_extensions import Literal
|
||||
from sqlmodel.sql.base import Executable
|
||||
from sqlmodel.sql.expression import Select, SelectOfScalar
|
||||
from typing_extensions import deprecated
|
||||
|
||||
from ..engine.result import Result, ScalarResult
|
||||
from ..sql.base import Executable
|
||||
from ..sql.expression import Select, SelectOfScalar
|
||||
|
||||
_TSelectParam = TypeVar("_TSelectParam")
|
||||
_TSelectParam = TypeVar("_TSelectParam", bound=Any)
|
||||
|
||||
|
||||
class Session(_Session):
|
||||
@@ -21,11 +32,10 @@ class Session(_Session):
|
||||
*,
|
||||
params: Optional[Union[Mapping[str, Any], Sequence[Mapping[str, Any]]]] = None,
|
||||
execution_options: Mapping[str, Any] = util.EMPTY_DICT,
|
||||
bind_arguments: Optional[Mapping[str, Any]] = None,
|
||||
bind_arguments: Optional[Dict[str, Any]] = None,
|
||||
_parent_execute_state: Optional[Any] = None,
|
||||
_add_event: Optional[Any] = None,
|
||||
**kw: Any,
|
||||
) -> Result[_TSelectParam]:
|
||||
) -> TupleResult[_TSelectParam]:
|
||||
...
|
||||
|
||||
@overload
|
||||
@@ -35,10 +45,9 @@ class Session(_Session):
|
||||
*,
|
||||
params: Optional[Union[Mapping[str, Any], Sequence[Mapping[str, Any]]]] = None,
|
||||
execution_options: Mapping[str, Any] = util.EMPTY_DICT,
|
||||
bind_arguments: Optional[Mapping[str, Any]] = None,
|
||||
bind_arguments: Optional[Dict[str, Any]] = None,
|
||||
_parent_execute_state: Optional[Any] = None,
|
||||
_add_event: Optional[Any] = None,
|
||||
**kw: Any,
|
||||
) -> ScalarResult[_TSelectParam]:
|
||||
...
|
||||
|
||||
@@ -52,11 +61,10 @@ class Session(_Session):
|
||||
*,
|
||||
params: Optional[Union[Mapping[str, Any], Sequence[Mapping[str, Any]]]] = None,
|
||||
execution_options: Mapping[str, Any] = util.EMPTY_DICT,
|
||||
bind_arguments: Optional[Mapping[str, Any]] = None,
|
||||
bind_arguments: Optional[Dict[str, Any]] = None,
|
||||
_parent_execute_state: Optional[Any] = None,
|
||||
_add_event: Optional[Any] = None,
|
||||
**kw: Any,
|
||||
) -> Union[Result[_TSelectParam], ScalarResult[_TSelectParam]]:
|
||||
) -> Union[TupleResult[_TSelectParam], ScalarResult[_TSelectParam]]:
|
||||
results = super().execute(
|
||||
statement,
|
||||
params=params,
|
||||
@@ -64,21 +72,40 @@ class Session(_Session):
|
||||
bind_arguments=bind_arguments,
|
||||
_parent_execute_state=_parent_execute_state,
|
||||
_add_event=_add_event,
|
||||
**kw,
|
||||
)
|
||||
if isinstance(statement, SelectOfScalar):
|
||||
return results.scalars() # type: ignore
|
||||
return results.scalars()
|
||||
return results # type: ignore
|
||||
|
||||
def execute(
|
||||
@deprecated(
|
||||
"""
|
||||
🚨 You probably want to use `session.exec()` instead of `session.execute()`.
|
||||
|
||||
This is the original SQLAlchemy `session.execute()` method that returns objects
|
||||
of type `Row`, and that you have to call `scalars()` to get the model objects.
|
||||
|
||||
For example:
|
||||
|
||||
```Python
|
||||
heroes = session.execute(select(Hero)).scalars().all()
|
||||
```
|
||||
|
||||
instead you could use `exec()`:
|
||||
|
||||
```Python
|
||||
heroes = session.exec(select(Hero)).all()
|
||||
```
|
||||
"""
|
||||
)
|
||||
def execute( # type: ignore
|
||||
self,
|
||||
statement: _Executable,
|
||||
params: Optional[Union[Mapping[str, Any], Sequence[Mapping[str, Any]]]] = None,
|
||||
execution_options: Optional[Mapping[str, Any]] = util.EMPTY_DICT,
|
||||
bind_arguments: Optional[Mapping[str, Any]] = None,
|
||||
params: Optional[_CoreAnyExecuteParams] = None,
|
||||
*,
|
||||
execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
|
||||
bind_arguments: Optional[Dict[str, Any]] = None,
|
||||
_parent_execute_state: Optional[Any] = None,
|
||||
_add_event: Optional[Any] = None,
|
||||
**kw: Any,
|
||||
) -> Result[Any]:
|
||||
"""
|
||||
🚨 You probably want to use `session.exec()` instead of `session.execute()`.
|
||||
@@ -98,17 +125,29 @@ class Session(_Session):
|
||||
heroes = session.exec(select(Hero)).all()
|
||||
```
|
||||
"""
|
||||
return super().execute( # type: ignore
|
||||
return super().execute(
|
||||
statement,
|
||||
params=params,
|
||||
execution_options=execution_options,
|
||||
bind_arguments=bind_arguments,
|
||||
_parent_execute_state=_parent_execute_state,
|
||||
_add_event=_add_event,
|
||||
**kw,
|
||||
)
|
||||
|
||||
def query(self, *entities: Any, **kwargs: Any) -> "_Query[Any]":
|
||||
@deprecated(
|
||||
"""
|
||||
🚨 You probably want to use `session.exec()` instead of `session.query()`.
|
||||
|
||||
`session.exec()` is SQLModel's own short version with increased type
|
||||
annotations.
|
||||
|
||||
Or otherwise you might want to use `session.execute()` instead of
|
||||
`session.query()`.
|
||||
"""
|
||||
)
|
||||
def query( # type: ignore
|
||||
self, *entities: _ColumnsClauseArgument[Any], **kwargs: Any
|
||||
) -> _Query[Any]:
|
||||
"""
|
||||
🚨 You probably want to use `session.exec()` instead of `session.query()`.
|
||||
|
||||
@@ -119,23 +158,3 @@ class Session(_Session):
|
||||
`session.query()`.
|
||||
"""
|
||||
return super().query(*entities, **kwargs)
|
||||
|
||||
def get(
|
||||
self,
|
||||
entity: Type[_TSelectParam],
|
||||
ident: Any,
|
||||
options: Optional[Sequence[Any]] = None,
|
||||
populate_existing: bool = False,
|
||||
with_for_update: Optional[Union[Literal[True], Mapping[str, Any]]] = None,
|
||||
identity_token: Optional[Any] = None,
|
||||
execution_options: Optional[Mapping[Any, Any]] = util.EMPTY_DICT,
|
||||
) -> Optional[_TSelectParam]:
|
||||
return super().get(
|
||||
entity,
|
||||
ident,
|
||||
options=options,
|
||||
populate_existing=populate_existing,
|
||||
with_for_update=with_for_update,
|
||||
identity_token=identity_token,
|
||||
execution_options=execution_options,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user