Skip to content

Commit 179183c

Browse files
committed
Fix linting issues
1 parent 972ee56 commit 179183c

File tree

5 files changed

+44
-69
lines changed

5 files changed

+44
-69
lines changed

sqlmodel/ext/asyncio/session.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
from typing import Any, Mapping, Optional, Sequence, TypeVar, Union
1+
from typing import Any, Dict, Mapping, Optional, Sequence, Type, TypeVar, Union
22

33
from sqlalchemy import util
44
from sqlalchemy.ext.asyncio import AsyncSession as _AsyncSession
55
from sqlalchemy.ext.asyncio import engine
66
from sqlalchemy.ext.asyncio.engine import AsyncConnection, AsyncEngine
7+
from sqlalchemy.orm import Mapper
8+
from sqlalchemy.sql.expression import TableClause
79
from sqlalchemy.util.concurrency import greenlet_spawn
810
from sqlmodel.sql.base import Executable
911

@@ -14,13 +16,18 @@
1416
_T = TypeVar("_T")
1517

1618

19+
BindsType = Dict[
20+
Union[Type[Any], Mapper[Any], TableClause, str], Union[AsyncEngine, AsyncConnection]
21+
]
22+
23+
1724
class AsyncSession(_AsyncSession):
1825
sync_session: Session
1926

2027
def __init__(
2128
self,
2229
bind: Optional[Union[AsyncConnection, AsyncEngine]] = None,
23-
binds: Optional[Mapping[object, Union[AsyncConnection, AsyncEngine]]] = None,
30+
binds: Optional[BindsType] = None,
2431
**kw: Any,
2532
):
2633
# All the same code of the original AsyncSession

sqlmodel/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ def __dataclass_transform__(
6161

6262

6363
class FieldInfo(PydanticFieldInfo):
64+
65+
nullable: Union[bool, PydanticUndefinedType]
66+
6467
def __init__(self, default: Any = PydanticUndefined, **kwargs: Any) -> None:
6568
primary_key = kwargs.pop("primary_key", False)
6669
nullable = kwargs.pop("nullable", PydanticUndefined)
@@ -587,7 +590,7 @@ def model_validate(
587590

588591

589592
def _is_field_noneable(field: FieldInfo) -> bool:
590-
if getattr(field, "nullable", PydanticUndefined) is not PydanticUndefined:
593+
if not isinstance(field.nullable, PydanticUndefinedType):
591594
return field.nullable
592595
if not field.is_required():
593596
default = getattr(field, "original_default", field.default)

sqlmodel/orm/session.py

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212

1313
from sqlalchemy import util
1414
from sqlalchemy.orm import Mapper as _Mapper
15-
from sqlalchemy.orm import Query as _Query
1615
from sqlalchemy.orm import Session as _Session
17-
from sqlalchemy.sql.base import Executable as _Executable
1816
from sqlalchemy.sql.selectable import ForUpdateArg as _ForUpdateArg
1917
from sqlmodel.sql.expression import Select, SelectOfScalar
2018

@@ -81,56 +79,6 @@ def exec(
8179
return results.scalars() # type: ignore
8280
return results # type: ignore
8381

84-
def execute(
85-
self,
86-
statement: _Executable,
87-
params: Optional[Union[Mapping[str, Any], Sequence[Mapping[str, Any]]]] = None,
88-
execution_options: Mapping[str, Any] = util.EMPTY_DICT,
89-
bind_arguments: Optional[Dict[str, Any]] = None,
90-
_parent_execute_state: Optional[Any] = None,
91-
_add_event: Optional[Any] = None,
92-
**kw: Any,
93-
) -> Result[Any]:
94-
"""
95-
🚨 You probably want to use `session.exec()` instead of `session.execute()`.
96-
97-
This is the original SQLAlchemy `session.execute()` method that returns objects
98-
of type `Row`, and that you have to call `scalars()` to get the model objects.
99-
100-
For example:
101-
102-
```Python
103-
heroes = session.execute(select(Hero)).scalars().all()
104-
```
105-
106-
instead you could use `exec()`:
107-
108-
```Python
109-
heroes = session.exec(select(Hero)).all()
110-
```
111-
"""
112-
return super().execute( # type: ignore
113-
statement,
114-
params=params,
115-
execution_options=execution_options,
116-
bind_arguments=bind_arguments,
117-
_parent_execute_state=_parent_execute_state,
118-
_add_event=_add_event,
119-
**kw,
120-
)
121-
122-
def query(self, *entities: Any, **kwargs: Any) -> "_Query[Any]":
123-
"""
124-
🚨 You probably want to use `session.exec()` instead of `session.query()`.
125-
126-
`session.exec()` is SQLModel's own short version with increased type
127-
annotations.
128-
129-
Or otherwise you might want to use `session.execute()` instead of
130-
`session.query()`.
131-
"""
132-
return super().query(*entities, **kwargs) # type: ignore
133-
13482
def get(
13583
self,
13684
entity: Union[Type[_TSelectParam], "_Mapper[_TSelectParam]"],
@@ -152,3 +100,34 @@ def get(
152100
execution_options=execution_options,
153101
bind_arguments=bind_arguments,
154102
)
103+
104+
105+
Session.query.__doc__ = """
106+
🚨 You probably want to use `session.exec()` instead of `session.query()`.
107+
108+
`session.exec()` is SQLModel's own short version with increased type
109+
annotations.
110+
111+
Or otherwise you might want to use `session.execute()` instead of
112+
`session.query()`.
113+
"""
114+
115+
116+
Session.execute.__doc__ = """
117+
🚨 You probably want to use `session.exec()` instead of `session.execute()`.
118+
119+
This is the original SQLAlchemy `session.execute()` method that returns objects
120+
of type `Row`, and that you have to call `scalars()` to get the model objects.
121+
122+
For example:
123+
124+
```Python
125+
heroes = session.execute(select(Hero)).scalars().all()
126+
```
127+
128+
instead you could use `exec()`:
129+
130+
```Python
131+
heroes = session.exec(select(Hero)).all()
132+
```
133+
"""

sqlmodel/sql/expression.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@ class SelectOfScalar(_Select[Tuple[_TSelect]], Generic[_TSelect]):
3535
inherit_cache = True
3636

3737

38-
# This is not comparable to sqlalchemy.sql.selectable.ScalarSelect, that has a different
39-
# purpose. This is the same as a normal SQLAlchemy Select class where there's only one
40-
# entity, so the result will be converted to a scalar by default. This way writing
41-
# for loops on the results will feel natural.
42-
class SelectOfScalar(_Select, Generic[_TSelect]):
43-
inherit_cache = True
44-
45-
4638
if TYPE_CHECKING: # pragma: no cover
4739
from ..main import SQLModel
4840

sqlmodel/sql/expression.py.jinja2

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ class Select(_Select[Tuple[_TSelect]], Generic[_TSelect]):
3030
class SelectOfScalar(_Select[Tuple[_TSelect]], Generic[_TSelect]):
3131
inherit_cache = True
3232

33-
# This is not comparable to sqlalchemy.sql.selectable.ScalarSelect, that has a different
34-
# purpose. This is the same as a normal SQLAlchemy Select class where there's only one
35-
# entity, so the result will be converted to a scalar by default. This way writing
36-
# for loops on the results will feel natural.
37-
class SelectOfScalar(_Select, Generic[_TSelect]):
38-
inherit_cache = True
3933

4034
if TYPE_CHECKING: # pragma: no cover
4135
from ..main import SQLModel

0 commit comments

Comments
 (0)