12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ from __future__ import annotations
16
+
15
17
import copy
16
18
from datetime import datetime , timedelta
17
19
from functools import partial
18
20
import time
21
+ from typing import TYPE_CHECKING , ClassVar , Type , TypeVar
19
22
import six
20
23
from warnings import warn
21
24
@@ -336,10 +339,12 @@ def __enter__(self):
336
339
def __exit__ (self , exc_type , exc_val , exc_tb ):
337
340
return
338
341
342
+ if TYPE_CHECKING :
343
+ from .models import M
339
344
340
345
class AbstractQuerySet (object ):
341
346
342
- def __init__ (self , model ):
347
+ def __init__ (self , model : Type [ M ] ):
343
348
super (AbstractQuerySet , self ).__init__ ()
344
349
self .model = model
345
350
@@ -529,7 +534,7 @@ def __iter__(self):
529
534
530
535
idx += 1
531
536
532
- def __getitem__ (self , s ) :
537
+ def __getitem__ (self , s : slice | int ) -> M | list [ M ] :
533
538
self ._execute_query ()
534
539
535
540
if isinstance (s , slice ):
@@ -602,7 +607,7 @@ def batch(self, batch_obj):
602
607
clone ._batch = batch_obj
603
608
return clone
604
609
605
- def first (self ):
610
+ def first (self ) -> M | None :
606
611
try :
607
612
return six .next (iter (self ))
608
613
except StopIteration :
@@ -619,7 +624,7 @@ def all(self):
619
624
"""
620
625
return copy .deepcopy (self )
621
626
622
- def consistency (self , consistency ):
627
+ def consistency (self , consistency : int ):
623
628
"""
624
629
Sets the consistency level for the operation. See :class:`.ConsistencyLevel`.
625
630
@@ -743,7 +748,7 @@ def filter(self, *args, **kwargs):
743
748
744
749
return clone
745
750
746
- def get (self , * args , ** kwargs ):
751
+ def get (self , * args , ** kwargs ) -> M :
747
752
"""
748
753
Returns a single instance matching this query, optionally with additional filter kwargs.
749
754
@@ -784,7 +789,7 @@ def _get_ordering_condition(self, colname):
784
789
785
790
return colname , order_type
786
791
787
- def order_by (self , * colnames ):
792
+ def order_by (self , * colnames : str ):
788
793
"""
789
794
Sets the column(s) to be used for ordering
790
795
@@ -828,7 +833,7 @@ class Comment(Model):
828
833
clone ._order .extend (conditions )
829
834
return clone
830
835
831
- def count (self ):
836
+ def count (self ) -> int :
832
837
"""
833
838
Returns the number of rows matched by this query.
834
839
@@ -881,7 +886,7 @@ class Automobile(Model):
881
886
882
887
return clone
883
888
884
- def limit (self , v ):
889
+ def limit (self , v : int ):
885
890
"""
886
891
Limits the number of results returned by Cassandra. Use *0* or *None* to disable.
887
892
@@ -913,7 +918,7 @@ def limit(self, v):
913
918
clone ._limit = v
914
919
return clone
915
920
916
- def fetch_size (self , v ):
921
+ def fetch_size (self , v : int ):
917
922
"""
918
923
Sets the number of rows that are fetched at a time.
919
924
@@ -969,15 +974,15 @@ def _only_or_defer(self, action, fields):
969
974
970
975
return clone
971
976
972
- def only (self , fields ):
977
+ def only (self , fields : list [ str ] ):
973
978
""" Load only these fields for the returned query """
974
979
return self ._only_or_defer ('only' , fields )
975
980
976
- def defer (self , fields ):
981
+ def defer (self , fields : list [ str ] ):
977
982
""" Don't load these fields for the returned query """
978
983
return self ._only_or_defer ('defer' , fields )
979
984
980
- def create (self , ** kwargs ):
985
+ def create (self , ** kwargs ) -> M :
981
986
return self .model (** kwargs ) \
982
987
.batch (self ._batch ) \
983
988
.ttl (self ._ttl ) \
@@ -1014,7 +1019,7 @@ def __eq__(self, q):
1014
1019
def __ne__ (self , q ):
1015
1020
return not (self != q )
1016
1021
1017
- def timeout (self , timeout ):
1022
+ def timeout (self , timeout : float | None ):
1018
1023
"""
1019
1024
:param timeout: Timeout for the query (in seconds)
1020
1025
:type timeout: float or None
@@ -1065,6 +1070,7 @@ def _get_result_constructor(self):
1065
1070
"""
1066
1071
return ResultObject
1067
1072
1073
+ T = TypeVar ('T' , 'ModelQuerySet' )
1068
1074
1069
1075
class ModelQuerySet (AbstractQuerySet ):
1070
1076
"""
@@ -1157,7 +1163,7 @@ def values_list(self, *fields, **kwargs):
1157
1163
clone ._flat_values_list = flat
1158
1164
return clone
1159
1165
1160
- def ttl (self , ttl ) :
1166
+ def ttl (self : T , ttl : int ) -> T :
1161
1167
"""
1162
1168
Sets the ttl (in seconds) for modified data.
1163
1169
@@ -1167,15 +1173,15 @@ def ttl(self, ttl):
1167
1173
clone ._ttl = ttl
1168
1174
return clone
1169
1175
1170
- def timestamp (self , timestamp ) :
1176
+ def timestamp (self : T , timestamp : datetime ) -> T :
1171
1177
"""
1172
1178
Allows for custom timestamps to be saved with the record.
1173
1179
"""
1174
1180
clone = copy .deepcopy (self )
1175
1181
clone ._timestamp = timestamp
1176
1182
return clone
1177
1183
1178
- def if_not_exists (self ) :
1184
+ def if_not_exists (self : T ) -> T :
1179
1185
"""
1180
1186
Check the existence of an object before insertion.
1181
1187
@@ -1187,7 +1193,7 @@ def if_not_exists(self):
1187
1193
clone ._if_not_exists = True
1188
1194
return clone
1189
1195
1190
- def if_exists (self ) :
1196
+ def if_exists (self : T ) -> T :
1191
1197
"""
1192
1198
Check the existence of an object before an update or delete.
1193
1199
0 commit comments