1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ from __future__ import annotations
16+
1517import copy
1618from datetime import datetime , timedelta
1719from functools import partial
1820import time
21+ from typing import TYPE_CHECKING , ClassVar , Type , TypeVar
1922import six
2023from warnings import warn
2124
@@ -336,10 +339,12 @@ def __enter__(self):
336339 def __exit__ (self , exc_type , exc_val , exc_tb ):
337340 return
338341
342+ if TYPE_CHECKING :
343+ from .models import M
339344
340345class AbstractQuerySet (object ):
341346
342- def __init__ (self , model ):
347+ def __init__ (self , model : Type [ M ] ):
343348 super (AbstractQuerySet , self ).__init__ ()
344349 self .model = model
345350
@@ -529,7 +534,7 @@ def __iter__(self):
529534
530535 idx += 1
531536
532- def __getitem__ (self , s ) :
537+ def __getitem__ (self , s : slice | int ) -> M | list [ M ] :
533538 self ._execute_query ()
534539
535540 if isinstance (s , slice ):
@@ -602,7 +607,7 @@ def batch(self, batch_obj):
602607 clone ._batch = batch_obj
603608 return clone
604609
605- def first (self ):
610+ def first (self ) -> M | None :
606611 try :
607612 return six .next (iter (self ))
608613 except StopIteration :
@@ -619,7 +624,7 @@ def all(self):
619624 """
620625 return copy .deepcopy (self )
621626
622- def consistency (self , consistency ):
627+ def consistency (self , consistency : int ):
623628 """
624629 Sets the consistency level for the operation. See :class:`.ConsistencyLevel`.
625630
@@ -743,7 +748,7 @@ def filter(self, *args, **kwargs):
743748
744749 return clone
745750
746- def get (self , * args , ** kwargs ):
751+ def get (self , * args , ** kwargs ) -> M :
747752 """
748753 Returns a single instance matching this query, optionally with additional filter kwargs.
749754
@@ -784,7 +789,7 @@ def _get_ordering_condition(self, colname):
784789
785790 return colname , order_type
786791
787- def order_by (self , * colnames ):
792+ def order_by (self , * colnames : str ):
788793 """
789794 Sets the column(s) to be used for ordering
790795
@@ -828,7 +833,7 @@ class Comment(Model):
828833 clone ._order .extend (conditions )
829834 return clone
830835
831- def count (self ):
836+ def count (self ) -> int :
832837 """
833838 Returns the number of rows matched by this query.
834839
@@ -881,7 +886,7 @@ class Automobile(Model):
881886
882887 return clone
883888
884- def limit (self , v ):
889+ def limit (self , v : int ):
885890 """
886891 Limits the number of results returned by Cassandra. Use *0* or *None* to disable.
887892
@@ -913,7 +918,7 @@ def limit(self, v):
913918 clone ._limit = v
914919 return clone
915920
916- def fetch_size (self , v ):
921+ def fetch_size (self , v : int ):
917922 """
918923 Sets the number of rows that are fetched at a time.
919924
@@ -969,15 +974,15 @@ def _only_or_defer(self, action, fields):
969974
970975 return clone
971976
972- def only (self , fields ):
977+ def only (self , fields : list [ str ] ):
973978 """ Load only these fields for the returned query """
974979 return self ._only_or_defer ('only' , fields )
975980
976- def defer (self , fields ):
981+ def defer (self , fields : list [ str ] ):
977982 """ Don't load these fields for the returned query """
978983 return self ._only_or_defer ('defer' , fields )
979984
980- def create (self , ** kwargs ):
985+ def create (self , ** kwargs ) -> M :
981986 return self .model (** kwargs ) \
982987 .batch (self ._batch ) \
983988 .ttl (self ._ttl ) \
@@ -1014,7 +1019,7 @@ def __eq__(self, q):
10141019 def __ne__ (self , q ):
10151020 return not (self != q )
10161021
1017- def timeout (self , timeout ):
1022+ def timeout (self , timeout : float | None ):
10181023 """
10191024 :param timeout: Timeout for the query (in seconds)
10201025 :type timeout: float or None
@@ -1065,6 +1070,7 @@ def _get_result_constructor(self):
10651070 """
10661071 return ResultObject
10671072
1073+ T = TypeVar ('T' , 'ModelQuerySet' )
10681074
10691075class ModelQuerySet (AbstractQuerySet ):
10701076 """
@@ -1157,7 +1163,7 @@ def values_list(self, *fields, **kwargs):
11571163 clone ._flat_values_list = flat
11581164 return clone
11591165
1160- def ttl (self , ttl ) :
1166+ def ttl (self : T , ttl : int ) -> T :
11611167 """
11621168 Sets the ttl (in seconds) for modified data.
11631169
@@ -1167,15 +1173,15 @@ def ttl(self, ttl):
11671173 clone ._ttl = ttl
11681174 return clone
11691175
1170- def timestamp (self , timestamp ) :
1176+ def timestamp (self : T , timestamp : datetime ) -> T :
11711177 """
11721178 Allows for custom timestamps to be saved with the record.
11731179 """
11741180 clone = copy .deepcopy (self )
11751181 clone ._timestamp = timestamp
11761182 return clone
11771183
1178- def if_not_exists (self ) :
1184+ def if_not_exists (self : T ) -> T :
11791185 """
11801186 Check the existence of an object before insertion.
11811187
@@ -1187,7 +1193,7 @@ def if_not_exists(self):
11871193 clone ._if_not_exists = True
11881194 return clone
11891195
1190- def if_exists (self ) :
1196+ def if_exists (self : T ) -> T :
11911197 """
11921198 Check the existence of an object before an update or delete.
11931199
0 commit comments