Skip to content

Commit 55470ce

Browse files
authored
Add one overload to Session.query to type queries of one single item (#151)
Add one overload to `Session.query()` to handle return types for a query of one single item. This helps with the specific case of querying a single model or column. For more than one item in the `query()` it seems that another plugin hook is the only option, as the return is not a plain `Tuple` but a `KeyedTuple`. This is what I found possible continuing the discussion in #149
1 parent 280d810 commit 55470ce

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

sqlalchemy-stubs/orm/session.pyi

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from typing import Any, Optional
1+
from typing import Any, Optional, Type, TypeVar, Union, overload
22

3+
from sqlalchemy import Column
34
from sqlalchemy.orm.query import Query
45

56
class _SessionClassMethods(object):
@@ -26,6 +27,8 @@ class SessionTransaction(object):
2627
def __enter__(self): ...
2728
def __exit__(self, type, value, traceback): ...
2829

30+
_T = TypeVar("_T")
31+
2932
class Session(_SessionClassMethods):
3033
public_methods: Any = ...
3134
identity_map: Any = ...
@@ -62,7 +65,10 @@ class Session(_SessionClassMethods):
6265
def bind_mapper(self, mapper, bind): ...
6366
def bind_table(self, table, bind): ...
6467
def get_bind(self, mapper: Optional[Any] = ..., clause: Optional[Any] = ...): ...
65-
def query(self, *entities, **kwargs) -> Query[Any]: ...
68+
@overload
69+
def query(self, entity: Union[Type[_T], Column[_T]], **kwargs) -> Query[_T]: ...
70+
@overload
71+
def query(self, *entities, **kwargs) -> Query: ...
6672
@property
6773
def no_autoflush(self): ...
6874
def refresh(self, instance, attribute_names: Optional[Any] = ..., lockmode: Optional[Any] = ...): ...

0 commit comments

Comments
 (0)