From d47b63c5f13e5bdac293d109c20e04a27beeaaaa Mon Sep 17 00:00:00 2001 From: VincentRPS Date: Thu, 22 Dec 2022 19:28:55 +0800 Subject: [PATCH 1/3] refactor: slightly improve typing --- cassandra/cqlengine/models.py | 26 +++++++++++++++-------- cassandra/cqlengine/query.py | 40 ++++++++++++++++++++--------------- 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/cassandra/cqlengine/models.py b/cassandra/cqlengine/models.py index b3c7c9e37f..ecf8be9a3f 100644 --- a/cassandra/cqlengine/models.py +++ b/cassandra/cqlengine/models.py @@ -14,6 +14,7 @@ import logging import re +from typing import Any, Type, TypeVar import six from warnings import warn @@ -86,11 +87,12 @@ class QuerySetDescriptor(object): it's declared on everytime it's accessed """ - def __get__(self, obj, model): + def __get__(self, obj: Any, model: "BaseModel"): """ :rtype: ModelQuerySet """ if model.__abstract__: raise CQLEngineException('cannot execute queries against abstract models') queryset = model.__queryset__(model) + queryset # if this is a concrete polymorphic model, and the discriminator # key is an indexed column, add a filter clause to only return @@ -329,6 +331,7 @@ def __delete__(self, instance): else: raise AttributeError('cannot delete {0} columns'.format(self.column.column_name)) +M = TypeVar('M', bound='BaseModel') class BaseModel(object): """ @@ -341,7 +344,7 @@ class DoesNotExist(_DoesNotExist): class MultipleObjectsReturned(_MultipleObjectsReturned): pass - objects = QuerySetDescriptor() + objects: query.ModelQuerySet = QuerySetDescriptor() ttl = TTLDescriptor() consistency = ConsistencyDescriptor() iff = ConditionalDescriptor() @@ -422,6 +425,7 @@ def __str__(self): return '{0} <{1}>'.format(self.__class__.__name__, ', '.join('{0}={1}'.format(k, getattr(self, k)) for k in self._primary_keys.keys())) + @classmethod def _routing_key_from_values(cls, pk_values, protocol_version): return cls._key_serializer(pk_values, protocol_version) @@ -658,7 +662,7 @@ def _as_dict(self): return values @classmethod - def create(cls, **kwargs): + def create(cls: Type[M], **kwargs) -> M: """ Create an instance of this model in the database. @@ -673,7 +677,7 @@ def create(cls, **kwargs): return cls.objects.create(**kwargs) @classmethod - def all(cls): + def all(cls: Type[M]) -> list[M]: """ Returns a queryset representing all stored objects @@ -682,7 +686,7 @@ def all(cls): return cls.objects.all() @classmethod - def filter(cls, *args, **kwargs): + def filter(cls: Type[M], *args, **kwargs): """ Returns a queryset based on filter parameters. @@ -691,7 +695,7 @@ def filter(cls, *args, **kwargs): return cls.objects.filter(*args, **kwargs) @classmethod - def get(cls, *args, **kwargs): + def get(cls: Type[M], *args, **kwargs) -> M: """ Returns a single object based on the passed filter constraints. @@ -699,7 +703,7 @@ def get(cls, *args, **kwargs): """ return cls.objects.get(*args, **kwargs) - def timeout(self, timeout): + def timeout(self: M, timeout: float | None) -> M: """ Sets a timeout for use in :meth:`~.save`, :meth:`~.update`, and :meth:`~.delete` operations @@ -708,7 +712,7 @@ def timeout(self, timeout): self._timeout = timeout return self - def save(self): + def save(self: M) -> M: """ Saves an object to the database. @@ -744,7 +748,7 @@ def save(self): return self - def update(self, **values): + def update(self: M, **values) -> M: """ Performs an update on the model instance. You can pass in values to set on the model for updating, or you can call without values to execute an update against any modified @@ -835,9 +839,13 @@ def _class_get_connection(cls): def _inst_get_connection(self): return self._connection or self.__connection__ + def __getitem__(self, s: slice | int) -> M | list[M]: + return self.objects.__getitem__(s) + _get_connection = hybrid_classmethod(_class_get_connection, _inst_get_connection) + class ModelMetaClass(type): def __new__(cls, name, bases, attrs): diff --git a/cassandra/cqlengine/query.py b/cassandra/cqlengine/query.py index 11f664ec02..2b421fed2b 100644 --- a/cassandra/cqlengine/query.py +++ b/cassandra/cqlengine/query.py @@ -12,10 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import annotations + import copy from datetime import datetime, timedelta from functools import partial import time +from typing import TYPE_CHECKING, ClassVar, Type, TypeVar import six from warnings import warn @@ -336,10 +339,12 @@ def __enter__(self): def __exit__(self, exc_type, exc_val, exc_tb): return +if TYPE_CHECKING: + from .models import M class AbstractQuerySet(object): - def __init__(self, model): + def __init__(self, model: Type[M]): super(AbstractQuerySet, self).__init__() self.model = model @@ -529,7 +534,7 @@ def __iter__(self): idx += 1 - def __getitem__(self, s): + def __getitem__(self, s: slice | int) -> M | list[M]: self._execute_query() if isinstance(s, slice): @@ -602,7 +607,7 @@ def batch(self, batch_obj): clone._batch = batch_obj return clone - def first(self): + def first(self) -> M | None: try: return six.next(iter(self)) except StopIteration: @@ -619,7 +624,7 @@ def all(self): """ return copy.deepcopy(self) - def consistency(self, consistency): + def consistency(self, consistency: int): """ Sets the consistency level for the operation. See :class:`.ConsistencyLevel`. @@ -743,7 +748,7 @@ def filter(self, *args, **kwargs): return clone - def get(self, *args, **kwargs): + def get(self, *args, **kwargs) -> M: """ Returns a single instance matching this query, optionally with additional filter kwargs. @@ -784,7 +789,7 @@ def _get_ordering_condition(self, colname): return colname, order_type - def order_by(self, *colnames): + def order_by(self, *colnames: str): """ Sets the column(s) to be used for ordering @@ -828,7 +833,7 @@ class Comment(Model): clone._order.extend(conditions) return clone - def count(self): + def count(self) -> int: """ Returns the number of rows matched by this query. @@ -881,7 +886,7 @@ class Automobile(Model): return clone - def limit(self, v): + def limit(self, v: int): """ Limits the number of results returned by Cassandra. Use *0* or *None* to disable. @@ -913,7 +918,7 @@ def limit(self, v): clone._limit = v return clone - def fetch_size(self, v): + def fetch_size(self, v: int): """ Sets the number of rows that are fetched at a time. @@ -969,15 +974,15 @@ def _only_or_defer(self, action, fields): return clone - def only(self, fields): + def only(self, fields: list[str]): """ Load only these fields for the returned query """ return self._only_or_defer('only', fields) - def defer(self, fields): + def defer(self, fields: list[str]): """ Don't load these fields for the returned query """ return self._only_or_defer('defer', fields) - def create(self, **kwargs): + def create(self, **kwargs) -> M: return self.model(**kwargs) \ .batch(self._batch) \ .ttl(self._ttl) \ @@ -1014,7 +1019,7 @@ def __eq__(self, q): def __ne__(self, q): return not (self != q) - def timeout(self, timeout): + def timeout(self, timeout: float | None): """ :param timeout: Timeout for the query (in seconds) :type timeout: float or None @@ -1065,6 +1070,7 @@ def _get_result_constructor(self): """ return ResultObject +T = TypeVar('T', 'ModelQuerySet') class ModelQuerySet(AbstractQuerySet): """ @@ -1157,7 +1163,7 @@ def values_list(self, *fields, **kwargs): clone._flat_values_list = flat return clone - def ttl(self, ttl): + def ttl(self: T, ttl: int) -> T: """ Sets the ttl (in seconds) for modified data. @@ -1167,7 +1173,7 @@ def ttl(self, ttl): clone._ttl = ttl return clone - def timestamp(self, timestamp): + def timestamp(self: T, timestamp: datetime) -> T: """ Allows for custom timestamps to be saved with the record. """ @@ -1175,7 +1181,7 @@ def timestamp(self, timestamp): clone._timestamp = timestamp return clone - def if_not_exists(self): + def if_not_exists(self: T) -> T: """ Check the existence of an object before insertion. @@ -1187,7 +1193,7 @@ def if_not_exists(self): clone._if_not_exists = True return clone - def if_exists(self): + def if_exists(self: T) -> T: """ Check the existence of an object before an update or delete. From e7efcf0dc6e37c3e2dc6fec5ba99401daa9cd9bc Mon Sep 17 00:00:00 2001 From: VincentRPS Date: Thu, 22 Dec 2022 19:33:12 +0800 Subject: [PATCH 2/3] chore: remove redundant queryset mention --- cassandra/cqlengine/models.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cassandra/cqlengine/models.py b/cassandra/cqlengine/models.py index ecf8be9a3f..df37d50e87 100644 --- a/cassandra/cqlengine/models.py +++ b/cassandra/cqlengine/models.py @@ -92,7 +92,6 @@ def __get__(self, obj: Any, model: "BaseModel"): if model.__abstract__: raise CQLEngineException('cannot execute queries against abstract models') queryset = model.__queryset__(model) - queryset # if this is a concrete polymorphic model, and the discriminator # key is an indexed column, add a filter clause to only return From 5f35565fe03b8ea7d14807a4836e286815bfc71a Mon Sep 17 00:00:00 2001 From: VincentRPS Date: Wed, 19 Jul 2023 16:49:01 +0800 Subject: [PATCH 3/3] fix: error related to typevar --- cassandra/cqlengine/query.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cassandra/cqlengine/query.py b/cassandra/cqlengine/query.py index 2b421fed2b..9a23f990b1 100644 --- a/cassandra/cqlengine/query.py +++ b/cassandra/cqlengine/query.py @@ -1070,7 +1070,7 @@ def _get_result_constructor(self): """ return ResultObject -T = TypeVar('T', 'ModelQuerySet') +T = TypeVar('T', bound=ModelQuerySet) class ModelQuerySet(AbstractQuerySet): """