Skip to content

Commit b6cd695

Browse files
committed
fix: type hints
1 parent 623c64e commit b6cd695

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

src/viur/core/db/query.py

+15-18
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import copy
55
import functools
66
import logging
7-
import operator
87
import typing as t
98

109
from .config import conf
@@ -15,12 +14,19 @@
1514
KEY_SPECIAL_PROPERTY,
1615
QueryDefinition,
1716
SortOrder,
17+
TFilters,
18+
TOrders,
1819
)
1920
from .utils import IsInTransaction
2021

2122
if t.TYPE_CHECKING:
2223
from viur.core.skeleton import SkeletonInstance, SkelList
2324

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+
2430

2531
def _entryMatchesQuery(entry: Entity, singleFilter: dict) -> bool:
2632
"""
@@ -71,26 +77,19 @@ def __init__(self, kind: str, srcSkelClass: t.Union["SkeletonInstance", None] =
7177
:param srcSkelClass: If set, enables data-model depended queries (like relational queries) as well as the
7278
:meth:fetch method
7379
"""
74-
super(Query, self).__init__()
80+
super().__init__()
7581
self.kind = kind
7682
self.srcSkel = srcSkelClass
7783
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
8786
# Sometimes, the default merge functionality from MultiQuery is not sufficient
8887
self._customMultiQueryMerge: t.Union[None, t.Callable[[Query, t.List[t.List[Entity]], int], t.List[Entity]]] \
8988
= None
9089
# Some (Multi-)Queries need a different amount of results per subQuery than actually returned
9190
self._calculateInternalMultiQueryLimit: t.Union[None, t.Callable[[Query, int], int]] = None
9291
# 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
9493
self.customQueryInfo = {}
9594
self.origKind = kind
9695
self._lastEntry = None
@@ -101,7 +100,7 @@ def __init__(self, kind: str, srcSkelClass: t.Union["SkeletonInstance", None] =
101100
# if isinstance(accessLog, set):
102101
# accessLog.add(kind)
103102

104-
def setFilterHook(self, hook: t.Callable) -> t.Optional[t.Callable]:
103+
def setFilterHook(self, hook: TFilterHook) -> TFilterHook | None:
105104
"""
106105
Installs *hook* as a callback function for new filters.
107106
@@ -117,7 +116,7 @@ def setFilterHook(self, hook: t.Callable) -> t.Optional[t.Callable]:
117116
self._filterHook = hook
118117
return old
119118

120-
def setOrderHook(self, hook: t.Callable) -> t.Callable:
119+
def setOrderHook(self, hook: TOrderHook) -> TOrderHook | None:
121120
"""
122121
Installs *hook* as a callback function for new orderings.
123122
@@ -278,7 +277,7 @@ def order(self, *orderings: t.Tuple[str, SortOrder]) -> t.Self:
278277
279278
.. code-block:: python
280279
281-
query = Query( "Person" )
280+
query = Query("Person")
282281
query.order(("bday" db.SortOrder.Ascending), ("age", db.SortOrder.Descending))
283282
284283
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:
307306
308307
309308
: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.
311310
:returns: Returns the query itself for chaining.
312311
"""
313312
if self.queries is None:
@@ -326,8 +325,6 @@ def order(self, *orderings: t.Tuple[str, SortOrder]) -> t.Self:
326325

327326
orders.append(order)
328327

329-
orderings = tuple(orders)
330-
331328
if self._orderHook is not None:
332329
try:
333330
orderings = self._orderHook(self, orderings)

src/viur/core/db/types.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class SortOrder(enum.Enum):
4040
"""Fetch A->Z, then flip the results (useful in pagination)"""
4141

4242

43+
44+
4345
class Key(Datastore_key):
4446
"""
4547
The python representation of one datastore key. Unlike the original implementation, we don't store a
@@ -149,6 +151,10 @@ def __init__(
149151
assert not key or isinstance(key, Key), "Key must be a Key-Object (or None for an embedded entity)"
150152

151153

154+
TOrders :t.TypeAlias = list[tuple[str, SortOrder]]
155+
TFilters:t.TypeAlias = dict[str, DATASTORE_BASE_TYPES | list[DATASTORE_BASE_TYPES]]
156+
157+
152158
@dataclass
153159
class QueryDefinition:
154160
"""
@@ -158,10 +164,10 @@ class QueryDefinition:
158164
kind: t.Optional[str]
159165
"""The datastore kind to run the query on. Can be None for kindles queries."""
160166

161-
filters: dict[str, DATASTORE_BASE_TYPES | list[DATASTORE_BASE_TYPES]]
167+
filters: TFilters
162168
"""A dictionary of constrains to apply to the query."""
163169

164-
orders: t.Optional[list[tuple[str, SortOrder]]]
170+
orders: t.Optional[TOrders]
165171
"""The list of fields to sort the results by."""
166172

167173
distinct: t.Optional[list[str]] = None

0 commit comments

Comments
 (0)