4
4
import copy
5
5
import functools
6
6
import logging
7
- import operator
8
7
import typing as t
9
8
10
9
from .config import conf
15
14
KEY_SPECIAL_PROPERTY ,
16
15
QueryDefinition ,
17
16
SortOrder ,
17
+ TFilters ,
18
+ TOrders ,
18
19
)
19
20
from .utils import IsInTransaction
20
21
21
22
if t .TYPE_CHECKING :
22
23
from viur .core .skeleton import SkeletonInstance , SkelList
23
24
25
+ TOrderHook = t .TypeVar ("TOrderHook" , bound = t .Callable [["Query" , TOrders ], TOrders ])
26
+ TFilterHook = t .TypeVar ("TFilterHook" , bound = t .Callable [
27
+ ["Query" , str , DATASTORE_BASE_TYPES | list [DATASTORE_BASE_TYPES ]], TFilters
28
+ ])
29
+
24
30
25
31
def _entryMatchesQuery (entry : Entity , singleFilter : dict ) -> bool :
26
32
"""
@@ -71,26 +77,19 @@ def __init__(self, kind: str, srcSkelClass: t.Union["SkeletonInstance", None] =
71
77
:param srcSkelClass: If set, enables data-model depended queries (like relational queries) as well as the
72
78
:meth:fetch method
73
79
"""
74
- super (Query , self ).__init__ ()
80
+ super ().__init__ ()
75
81
self .kind = kind
76
82
self .srcSkel = srcSkelClass
77
83
self .queries : t .Union [None , QueryDefinition , t .List [QueryDefinition ]] = QueryDefinition (kind , {}, [])
78
- cbSignature = t .Union [
79
- None ,
80
- t .Callable [
81
- [Query , str , t .Union [DATASTORE_BASE_TYPES , t .List [DATASTORE_BASE_TYPES ]]],
82
- t .Union [None , t .Tuple [str , t .Union [DATASTORE_BASE_TYPES , t .List [DATASTORE_BASE_TYPES ]]]]
83
- ]
84
- ]
85
- self ._filterHook : cbSignature = None
86
- self ._orderHook : cbSignature = None
84
+ self ._filterHook : TFilterHook | None = None
85
+ self ._orderHook : TOrderHook | None = None
87
86
# Sometimes, the default merge functionality from MultiQuery is not sufficient
88
87
self ._customMultiQueryMerge : t .Union [None , t .Callable [[Query , t .List [t .List [Entity ]], int ], t .List [Entity ]]] \
89
88
= None
90
89
# Some (Multi-)Queries need a different amount of results per subQuery than actually returned
91
90
self ._calculateInternalMultiQueryLimit : t .Union [None , t .Callable [[Query , int ], int ]] = None
92
91
# Allow carrying custom data along with the query.
93
- # Currently only used by SpartialBone to record the guaranteed correctness
92
+ # Currently only used by SpatialBone to record the guaranteed correctness
94
93
self .customQueryInfo = {}
95
94
self .origKind = kind
96
95
self ._lastEntry = None
@@ -101,7 +100,7 @@ def __init__(self, kind: str, srcSkelClass: t.Union["SkeletonInstance", None] =
101
100
# if isinstance(accessLog, set):
102
101
# accessLog.add(kind)
103
102
104
- def setFilterHook (self , hook : t . Callable ) -> t . Optional [ t . Callable ] :
103
+ def setFilterHook (self , hook : TFilterHook ) -> TFilterHook | None :
105
104
"""
106
105
Installs *hook* as a callback function for new filters.
107
106
@@ -117,7 +116,7 @@ def setFilterHook(self, hook: t.Callable) -> t.Optional[t.Callable]:
117
116
self ._filterHook = hook
118
117
return old
119
118
120
- def setOrderHook (self , hook : t . Callable ) -> t . Callable :
119
+ def setOrderHook (self , hook : TOrderHook ) -> TOrderHook | None :
121
120
"""
122
121
Installs *hook* as a callback function for new orderings.
123
122
@@ -278,7 +277,7 @@ def order(self, *orderings: t.Tuple[str, SortOrder]) -> t.Self:
278
277
279
278
.. code-block:: python
280
279
281
- query = Query( "Person" )
280
+ query = Query("Person")
282
281
query.order(("bday" db.SortOrder.Ascending), ("age", db.SortOrder.Descending))
283
282
284
283
sorts every Person in order of their birthday, starting with January 1.
@@ -307,7 +306,7 @@ def order(self, *orderings: t.Tuple[str, SortOrder]) -> t.Self:
307
306
308
307
309
308
:param orderings: The properties to sort by, in sort order.
310
- Each argument must be a (string , direction) 2-tuple.
309
+ Each argument must be a (name , direction) 2-tuple.
311
310
:returns: Returns the query itself for chaining.
312
311
"""
313
312
if self .queries is None :
@@ -326,8 +325,6 @@ def order(self, *orderings: t.Tuple[str, SortOrder]) -> t.Self:
326
325
327
326
orders .append (order )
328
327
329
- orderings = tuple (orders )
330
-
331
328
if self ._orderHook is not None :
332
329
try :
333
330
orderings = self ._orderHook (self , orderings )
0 commit comments