5
5
This is an internal API not covered by versioning policy.
6
6
"""
7
7
8
+ from __future__ import annotations
9
+
8
10
import logging
9
11
import re
10
12
import traceback
@@ -28,7 +30,7 @@ class ObjectDoesNotExist(Exception):
28
30
def keygetter (
29
31
obj : Mapping [str , t .Any ],
30
32
path : str ,
31
- ) -> t . Union [ None , t .Any , str , list [str ], Mapping [str , str ] ]:
33
+ ) -> None | t .Any | str | list [str ] | Mapping [str , str ]:
32
34
"""Fetch values in objects and keys, supported nested data.
33
35
34
36
**With dictionaries**:
@@ -94,7 +96,7 @@ def keygetter(
94
96
return dct
95
97
96
98
97
- def parse_lookup (obj : Mapping [str , t .Any ], path : str , lookup : str ) -> t .Optional [ t . Any ] :
99
+ def parse_lookup (obj : Mapping [str , t .Any ], path : str , lookup : str ) -> t .Any | None :
98
100
"""Check if field lookup key, e.g. "my__path__contains" has comparator, return val.
99
101
100
102
If comparator not used or value not found, return None.
@@ -134,23 +136,23 @@ class LookupProtocol(t.Protocol):
134
136
135
137
def __call__ (
136
138
self ,
137
- data : t . Union [ str , list [str ], Mapping [str , str ] ],
138
- rhs : t . Union [ str , list [str ], Mapping [str , str ], re .Pattern [str ] ],
139
+ data : str | list [str ] | Mapping [str , str ],
140
+ rhs : str | list [str ] | Mapping [str , str ] | re .Pattern [str ],
139
141
) -> bool :
140
142
"""Return callback for :class:`QueryList` filtering operators."""
141
143
...
142
144
143
145
144
146
def lookup_exact (
145
- data : t . Union [ str , list [str ], Mapping [str , str ] ],
146
- rhs : t . Union [ str , list [str ], Mapping [str , str ], re .Pattern [str ] ],
147
+ data : str | list [str ] | Mapping [str , str ],
148
+ rhs : str | list [str ] | Mapping [str , str ] | re .Pattern [str ],
147
149
) -> bool :
148
150
return rhs == data
149
151
150
152
151
153
def lookup_iexact (
152
- data : t . Union [ str , list [str ], Mapping [str , str ] ],
153
- rhs : t . Union [ str , list [str ], Mapping [str , str ], re .Pattern [str ] ],
154
+ data : str | list [str ] | Mapping [str , str ],
155
+ rhs : str | list [str ] | Mapping [str , str ] | re .Pattern [str ],
154
156
) -> bool :
155
157
if not isinstance (rhs , str ) or not isinstance (data , str ):
156
158
return False
@@ -159,8 +161,8 @@ def lookup_iexact(
159
161
160
162
161
163
def lookup_contains (
162
- data : t . Union [ str , list [str ], Mapping [str , str ] ],
163
- rhs : t . Union [ str , list [str ], Mapping [str , str ], re .Pattern [str ] ],
164
+ data : str | list [str ] | Mapping [str , str ],
165
+ rhs : str | list [str ] | Mapping [str , str ] | re .Pattern [str ],
164
166
) -> bool :
165
167
if not isinstance (rhs , str ) or not isinstance (data , (str , Mapping , list )):
166
168
return False
@@ -169,8 +171,8 @@ def lookup_contains(
169
171
170
172
171
173
def lookup_icontains (
172
- data : t . Union [ str , list [str ], Mapping [str , str ] ],
173
- rhs : t . Union [ str , list [str ], Mapping [str , str ], re .Pattern [str ] ],
174
+ data : str | list [str ] | Mapping [str , str ],
175
+ rhs : str | list [str ] | Mapping [str , str ] | re .Pattern [str ],
174
176
) -> bool :
175
177
if not isinstance (rhs , str ) or not isinstance (data , (str , Mapping , list )):
176
178
return False
@@ -184,8 +186,8 @@ def lookup_icontains(
184
186
185
187
186
188
def lookup_startswith (
187
- data : t . Union [ str , list [str ], Mapping [str , str ] ],
188
- rhs : t . Union [ str , list [str ], Mapping [str , str ], re .Pattern [str ] ],
189
+ data : str | list [str ] | Mapping [str , str ],
190
+ rhs : str | list [str ] | Mapping [str , str ] | re .Pattern [str ],
189
191
) -> bool :
190
192
if not isinstance (rhs , str ) or not isinstance (data , str ):
191
193
return False
@@ -194,8 +196,8 @@ def lookup_startswith(
194
196
195
197
196
198
def lookup_istartswith (
197
- data : t . Union [ str , list [str ], Mapping [str , str ] ],
198
- rhs : t . Union [ str , list [str ], Mapping [str , str ], re .Pattern [str ] ],
199
+ data : str | list [str ] | Mapping [str , str ],
200
+ rhs : str | list [str ] | Mapping [str , str ] | re .Pattern [str ],
199
201
) -> bool :
200
202
if not isinstance (rhs , str ) or not isinstance (data , str ):
201
203
return False
@@ -204,8 +206,8 @@ def lookup_istartswith(
204
206
205
207
206
208
def lookup_endswith (
207
- data : t . Union [ str , list [str ], Mapping [str , str ] ],
208
- rhs : t . Union [ str , list [str ], Mapping [str , str ], re .Pattern [str ] ],
209
+ data : str | list [str ] | Mapping [str , str ],
210
+ rhs : str | list [str ] | Mapping [str , str ] | re .Pattern [str ],
209
211
) -> bool :
210
212
if not isinstance (rhs , str ) or not isinstance (data , str ):
211
213
return False
@@ -214,17 +216,17 @@ def lookup_endswith(
214
216
215
217
216
218
def lookup_iendswith (
217
- data : t . Union [ str , list [str ], Mapping [str , str ] ],
218
- rhs : t . Union [ str , list [str ], Mapping [str , str ], re .Pattern [str ] ],
219
+ data : str | list [str ] | Mapping [str , str ],
220
+ rhs : str | list [str ] | Mapping [str , str ] | re .Pattern [str ],
219
221
) -> bool :
220
222
if not isinstance (rhs , str ) or not isinstance (data , str ):
221
223
return False
222
224
return data .lower ().endswith (rhs .lower ())
223
225
224
226
225
227
def lookup_in (
226
- data : t . Union [ str , list [str ], Mapping [str , str ] ],
227
- rhs : t . Union [ str , list [str ], Mapping [str , str ], re .Pattern [str ] ],
228
+ data : str | list [str ] | Mapping [str , str ],
229
+ rhs : str | list [str ] | Mapping [str , str ] | re .Pattern [str ],
228
230
) -> bool :
229
231
if isinstance (rhs , list ):
230
232
return data in rhs
@@ -248,8 +250,8 @@ def lookup_in(
248
250
249
251
250
252
def lookup_nin (
251
- data : t . Union [ str , list [str ], Mapping [str , str ] ],
252
- rhs : t . Union [ str , list [str ], Mapping [str , str ], re .Pattern [str ] ],
253
+ data : str | list [str ] | Mapping [str , str ],
254
+ rhs : str | list [str ] | Mapping [str , str ] | re .Pattern [str ],
253
255
) -> bool :
254
256
if isinstance (rhs , list ):
255
257
return data not in rhs
@@ -273,17 +275,17 @@ def lookup_nin(
273
275
274
276
275
277
def lookup_regex (
276
- data : t . Union [ str , list [str ], Mapping [str , str ] ],
277
- rhs : t . Union [ str , list [str ], Mapping [str , str ], re .Pattern [str ] ],
278
+ data : str | list [str ] | Mapping [str , str ],
279
+ rhs : str | list [str ] | Mapping [str , str ] | re .Pattern [str ],
278
280
) -> bool :
279
281
if isinstance (data , (str , bytes , re .Pattern )) and isinstance (rhs , (str , bytes )):
280
282
return bool (re .search (rhs , data ))
281
283
return False
282
284
283
285
284
286
def lookup_iregex (
285
- data : t . Union [ str , list [str ], Mapping [str , str ] ],
286
- rhs : t . Union [ str , list [str ], Mapping [str , str ], re .Pattern [str ] ],
287
+ data : str | list [str ] | Mapping [str , str ],
288
+ rhs : str | list [str ] | Mapping [str , str ] | re .Pattern [str ],
287
289
) -> bool :
288
290
if isinstance (data , (str , bytes , re .Pattern )) and isinstance (rhs , (str , bytes )):
289
291
return bool (re .search (rhs , data , re .IGNORECASE ))
@@ -467,9 +469,9 @@ class QueryList(list[T], t.Generic[T]):
467
469
"""
468
470
469
471
data : Sequence [T ]
470
- pk_key : t . Optional [ str ]
472
+ pk_key : str | None
471
473
472
- def __init__ (self , items : t . Optional [ " Iterable[T]" ] = None ) -> None :
474
+ def __init__ (self , items : Iterable [T ] | None = None ) -> None :
473
475
super ().__init__ (items if items is not None else [])
474
476
475
477
def items (self ) -> list [tuple [str , T ]]:
@@ -502,9 +504,9 @@ def __eq__(
502
504
503
505
def filter (
504
506
self ,
505
- matcher : t . Optional [ t . Union [ Callable [[T ], bool ], T ]] = None ,
507
+ matcher : Callable [[T ], bool ] | T | None = None ,
506
508
** kwargs : t .Any ,
507
- ) -> " QueryList[T]" :
509
+ ) -> QueryList [T ]:
508
510
def filter_lookup (obj : t .Any ) -> bool :
509
511
for path , v in kwargs .items ():
510
512
try :
@@ -529,7 +531,7 @@ def filter_lookup(obj: t.Any) -> bool:
529
531
filter_ = matcher
530
532
elif matcher is not None :
531
533
532
- def val_match (obj : t . Union [ str , list [t .Any ], T ] ) -> bool :
534
+ def val_match (obj : str | list [t .Any ] | T ) -> bool :
533
535
if isinstance (matcher , list ):
534
536
return obj in matcher
535
537
return bool (obj == matcher )
@@ -542,10 +544,10 @@ def val_match(obj: t.Union[str, list[t.Any], T]) -> bool:
542
544
543
545
def get (
544
546
self ,
545
- matcher : t . Optional [ t . Union [ Callable [[T ], bool ], T ]] = None ,
546
- default : t .Optional [ t . Any ] = no_arg ,
547
+ matcher : Callable [[T ], bool ] | T | None = None ,
548
+ default : t .Any | None = no_arg ,
547
549
** kwargs : t .Any ,
548
- ) -> t . Optional [ T ] :
550
+ ) -> T | None :
549
551
objs = self .filter (matcher = matcher , ** kwargs )
550
552
if len (objs ) > 1 :
551
553
raise MultipleObjectsReturned
0 commit comments