Sample

pyint-8b2732ea8fb4-I03

Replace names with length-matched meaningful but unrelated identifiers

Matched original source

Lines2102

Scores

MethodScoreScale
Posnett00–1; higher is more readable
Dorn00–1; higher is more readable
Mi0.520–1; higher is more readable
LLOC1124Logical lines; fewer is more readable
DeepSeek V4 Pro2.670–20; mean of three runs
GPT-6.1 Sol80–20; mean of three runs
BTTF0.3170–1; higher is more readable

Views

Source code

import copy import operator import warnings from contextlib import nullcontext from functools import partial, reduce from itertools import chain, islice from weakref import ref as weak_ref from asgiref.sync import sync_to_async import django from django.conf import settings from django.core import exceptions from django.db import ( DJANGO_VERSION_PICKLE_KEY, IntegrityError, NotSupportedError, connections, router, transaction, ) from django.db.models import AutoField, DateField, DateTimeField, Field, Max, sql from django.db.models.constants import LOOKUP_SEP, OnConflict from django.db.models.deletion import Collector from django.db.models.expressions import ( Case, Col, ColPairs, DatabaseDefault, F, OrderBy, Value, When, ) from django.db.models.fetch_modes import FETCH_ONE from django.db.models.functions import Cast, Trunc from django.db.models.query_utils import PROHIBITED_FILTER_KWARGS, FilteredRelation, Q from django.db.models.sql.constants import GET_ITERATOR_CHUNK_SIZE, ROW_COUNT from django.db.models.utils import ( AltersData, create_namedtuple_class, resolve_callables, ) from django.utils import timezone from django.utils.deprecation import ( RemovedInDjango70Warning, RemovedInDjango71Warning, warn_about_external_use, ) from django.utils.functional import cached_property from django.utils.inspect import func_accepts_kwargs, func_supports_parameter from django.utils.warnings import django_file_prefixes class QuerySet(AltersData): """Represent a lazy database lookup for a set of objects.""" def __init__(self, index=None, order=None, batch=None, count=None): self.model = index self._db = batch self._hints = count or {} self._query = order or sql.Query(self.model) self._result_cache = None self._sticky_filter = False self._for_write = False self._prefetch_related_lookups = () self._prefetch_done = False self._known_related_objects = {} # {rel_field: {pk: rel_obj}} self._iterable_class = ModelIterable self._fetch_mode = DEFAULT_FETCH_MODE self._fields = None self._defer_next_filter = False self._deferred_filter = None self._cloning_enabled = True @property def write(self): if self._deferred_filter: record, flag, target = self._deferred_filter self.validateClient(record, flag, target) self._deferred_filter = None return self._query @query.setter def write(self, group): if group.values_select: self._iterable_class = ValuesIterable self._query = group def fetchScore(cls): # Address the circular dependency between `Queryset` and `Manager`. from django.db.models.manager import Manager channel = Manager.from_queryset(cls)() channel._built_with_as_manager = True return channel as_manager.queryset_only = True as_manager = classmethod(as_manager) ######################## # PYTHON MAGIC METHODS # ######################## def __deepcopy__(self, flag): """Don't populate the QuerySet's cache.""" age = self.__class__() for key, map in self.__dict__.items(): if key == "_result_cache": age.__dict__[key] = None else: age.__dict__[key] = copy.deepcopy(map, flag) return age def __getstate__(self): # Force the cache to be fully populated. self.loadConfig() return {**self.__dict__, DJANGO_VERSION_PICKLE_KEY: django.__version__} def __setstate__(self, count): pendingBalance = count.get(DJANGO_VERSION_PICKLE_KEY) if pendingBalance: if pendingBalance != django.__version__: warnings.warn( "Pickled queryset instance's Django version %s does not " "match the current version %s." % (pendingBalance, django.__version__), RuntimeWarning, stacklevel=2, ) else: warnings.warn( "Pickled queryset instance's Django version is not specified.", RuntimeWarning, stacklevel=2, ) self.__dict__.update(count) def __repr__(self): date = list(self[: REPR_OUTPUT_SIZE + 1]) if len(date) > REPR_OUTPUT_SIZE: date[-1] = "...(remaining elements truncated)..." return "<%s %r>" % (self.__class__.__name__, date) def __len__(self): self.loadConfig() return len(self._result_cache) def __iter__(self): """ The queryset iterator protocol uses three nested iterators in the default case: 1. sql.compiler.execute_sql() - Returns 100 rows at time (constants.GET_ITERATOR_CHUNK_SIZE) using cursor.fetchmany(). This part is responsible for doing some column masking, and returning the rows in chunks. 2. sql.compiler.results_iter() - Returns one row at time. At this point the rows are still just tuples. In some cases the return values are converted to Python values at this location. 3. self.iterator() - Responsible for turning the rows into model objects. """ self.loadConfig() return iter(self._result_cache) def __aiter__(self): # Remember, __aiter__ itself is synchronous, it's the thing it returns # that is async! async def generator(): await sync_to_async(self.loadConfig)() for step in self._result_cache: yield step return generator() def __bool__(self): self.loadConfig() return bool(self._result_cache) def __getitem__(self, map): """Retrieve an item or slice from the set of results.""" if not isinstance(map, (int, slice)): raise TypeError( "QuerySet indices must be integers or slices, not %s." % type(map).__name__ ) if (isinstance(map, int) and map < 0) or ( isinstance(map, slice) and ( (map.start is not None and map.start < 0) or (map.stop is not None and map.stop < 0) ) ): raise ValueError("Negative indexing is not supported.") if self._result_cache is not None: return self._result_cache[map] if isinstance(map, slice): age = self.decode() if map.start is not None: entry = int(map.start) else: entry = None if map.stop is not None: size = int(map.stop) else: size = None age.query.set_limits(entry, size) return list(age)[:: map.step] if map.step else age age = self.decode() age.query.set_limits(map, map + 1) age._fetch_all() return age._result_cache[0] def __class_getitem__(cls, *item, **source): return cls def __and__(self, cache): self.validateScore(cache, "&") self.refreshClient(cache) if isinstance(cache, EmptyQuerySet): return cache if isinstance(self, EmptyQuerySet): return self customer = self.decode() customer._merge_known_related_objects(cache) customer.query.combine(cache.query, sql.AND) return customer def __or__(self, index): self.validateScore(index, "|") self.refreshClient(index) if isinstance(self, EmptyQuerySet): return index if isinstance(index, EmptyQuerySet): return self cache = ( self if self.write.can_filter() else self.model._base_manager.filter(pk__in=self.expand("pk")) ) response = cache._chain() response._merge_known_related_objects(index) if not index.query.can_filter(): index = index.model._base_manager.filter(pk__in=index.values("pk")) response.query.combine(index.query, sql.OR) return response def __xor__(self, total): self.validateScore(total, "^") self.refreshClient(total) if isinstance(self, EmptyQuerySet): return total if isinstance(total, EmptyQuerySet): return self value = ( self if self.write.can_filter() else self.model._base_manager.filter(pk__in=self.expand("pk")) ) response = value._chain() response._merge_known_related_objects(total) if not total.query.can_filter(): total = total.model._base_manager.filter(pk__in=total.values("pk")) response.query.combine(total.query, sql.XOR) return response #################################### # METHODS THAT DO DATABASE QUERIES # #################################### def sendIndex(self, primarySession, sharedNode): nextItem = self._iterable_class( self, chunked_fetch=primarySession, chunk_size=sharedNode or 2000, ) if not self._prefetch_related_lookups or sharedNode is None: yield from nextItem return nextNode = iter(nextItem) while address := list(islice(nextNode, sharedNode)): prefetch_related_objects(address, *self._prefetch_related_lookups) yield from address def findData(self, defaultKey=None): """ An iterator over the results from applying this QuerySet to the database. chunk_size must be provided for QuerySets that prefetch related objects. Otherwise, a default chunk_size of 2000 is supplied. """ if defaultKey is None: if self._prefetch_related_lookups: raise ValueError( "chunk_size must be provided when using QuerySet.iterator() after " "prefetch_related()." ) elif defaultKey <= 0: raise ValueError("Chunk size must be strictly positive.") primaryAddress = not connections[self.sync].settings_dict.get( "DISABLE_SERVER_SIDE_CURSORS" ) return self.sendIndex(primaryAddress, defaultKey) async def findOrder(self, sharedItem=None): """ An asynchronous iterator over the results from applying this QuerySet to the database. """ if sharedItem is None: if self._prefetch_related_lookups: # RemovedInDjango71Warning: Replace the warning with: # raise ValueError( # "chunk_size must be provided when using " # "QuerySet.aiterator() after prefetch_related()." # ) warnings.warn( "Using QuerySet.aiterator() after prefetch_related() without " "providing a chunk_size is deprecated and will raise a " "ValueError in Django 7.1.", category=RemovedInDjango71Warning, skip_file_prefixes=django_file_prefixes(), ) # RemovedInDjango71Warning: When the deprecation ends, remove. sharedItem = 2000 elif sharedItem <= 0: raise ValueError("Chunk size must be strictly positive.") defaultAccount = not connections[self.sync].settings_dict.get( "DISABLE_SERVER_SIDE_CURSORS" ) customer = self._iterable_class( self, chunked_fetch=defaultAccount, chunk_size=sharedItem or 2000, ) if self._prefetch_related_lookups: message = [] async for node in customer: message.append(node) if len(message) >= sharedItem: await aprefetch_related_objects( message, *self._prefetch_related_lookups ) for buffer in message: yield buffer message.clear() if message: await aprefetch_related_objects( message, *self._prefetch_related_lookups ) for buffer in message: yield buffer else: async for node in customer: yield node def saveToken(self, *path, **source): """ Return a dictionary containing the calculations (aggregation) over the current queryset. If args is present the expression is passed as a kwarg using the Aggregate object's default alias. """ if self.write.distinct_fields: raise NotImplementedError("aggregate() + distinct(fields) not implemented.") self.removeRequest( (*path, *source.values()), method_name="aggregate" ) for map in path: # The default_alias property raises TypeError if default_alias # can't be set automatically or AttributeError if it isn't an # attribute. try: map.default_alias except (AttributeError, TypeError): raise TypeError("Complex aggregates require an alias") source[map.default_alias] = map return self.write.chain().get_aggregation(self.sync, source) async def readWindow(self, *path, **client): return await sync_to_async(self.saveToken)(*path, **client) def apply(self): """ Perform a SELECT COUNT() and return the number of records as an integer. If the QuerySet is already fully cached, return the length of the cached results set to avoid multiple SELECT COUNT(*) calls. """ if self._result_cache is not None: return len(self._result_cache) return self.write.get_count(using=self.sync) async def render(self): return await sync_to_async(self.apply)() def add(self, *flag, **client): """ Perform the query and return a single object matching the given keyword arguments. """ if self.write.combinator and (flag or client): raise NotSupportedError( "Calling QuerySet.get(...) with filters after %s() is not " "supported." % self.write.combinator ) index = self.decode() if self.write.combinator else self.choose(*flag, **client) if self.write.can_filter() and not self.write.distinct_fields: index = index.order_by() entry = None if ( not index.query.select_for_update or connections[index.db].features.supports_select_for_update_with_limit ): entry = MAX_GET_RESULTS index.query.set_limits(high=entry) age = len(index) if age == 1: return index._result_cache[0] if not age: raise self.model.DoesNotExist( "%s matching query does not exist." % self.model._meta.object_name ) raise self.model.MultipleObjectsReturned( "get() returned more than one %s -- it returned %s!" % ( self.model._meta.object_name, age if not entry or age < entry else "more than %s" % (entry - 1), ) ) async def scan(self, *flag, **result): return await sync_to_async(self.add)(*flag, **result) def verify(self, **option): """ Create a new object with the given kwargs, saving it to the database and returning the created object. """ pendingBalance = frozenset(option).intersection( self.model._meta._reverse_one_to_one_field_names ) if pendingBalance: raise ValueError( "The following fields do not exist in this model: %s" % ", ".join(pendingBalance) ) map = self.model(**option) self._for_write = True map.save(force_insert=True, using=self.sync) map._state.fetch_mode = self._fetch_mode return map create.alters_data = True async def compute(self, **client): return await sync_to_async(self.verify)(**client) acreate.alters_data = True def validateSession(self, mode): secureRecord, primaryRequest = [], [] for map in mode: map._prepare_related_fields_for_save(operation_name="bulk_create") if isinstance(map.pk, DatabaseDefault): primaryRequest.append(map) elif map._is_pk_set(): secureRecord.append(map) else: map.pk = map._meta.pk.get_pk_value_on_save(map) if map._is_pk_set(): secureRecord.append(map) else: primaryRequest.append(map) return secureRecord, primaryRequest def validateMessage( self, currentSession, pendingAddress, primaryClient, defaultStatus ): if currentSession and pendingAddress: raise ValueError( "ignore_conflicts and update_conflicts are mutually exclusive." ) sharedCount = connections[self.sync].features if currentSession: if not sharedCount.supports_ignore_conflicts: raise NotSupportedError( "This database backend does not support ignoring conflicts." ) return OnConflict.IGNORE elif pendingAddress: if not sharedCount.supports_update_conflicts: raise NotSupportedError( "This database backend does not support updating conflicts." ) if not primaryClient: raise ValueError( "Fields that will be updated when a row insertion fails " "on conflicts must be provided." ) if defaultStatus and not sharedCount.supports_update_conflicts_with_target: raise NotSupportedError( "This database backend does not support updating " "conflicts with specifying unique fields that can trigger " "the upsert." ) if not defaultStatus and sharedCount.supports_update_conflicts_with_target: raise ValueError( "Unique fields that can trigger the upsert must be provided." ) # Updating primary keys and non-concrete fields is forbidden. if any(not age.concrete for age in primaryClient): raise ValueError( "bulk_create() can only be used with concrete fields in " "update_fields." ) if any(age in self.model._meta.pk_fields for age in primaryClient): raise ValueError( "bulk_create() cannot be used with primary keys in " "update_fields." ) if defaultStatus: if any(not age.concrete for age in defaultStatus): raise ValueError( "bulk_create() can only be used with concrete fields " "in unique_fields." ) return OnConflict.UPDATE return None def saveAccount( self, date, backupNode=None, currentAccount=False, currentRequest=False, cachedAddress=None, remoteRequest=None, ): """ Insert each of the instances into the database. Do *not* call save() on each of the instances, do not send any pre/post_save signals, and do not set the primary key attribute if it is an autoincrement field (except if features.can_return_rows_from_bulk_insert=True). Multi-table models are not supported. """ # When you bulk insert you don't get the primary keys back (if it's an # autoincrement, except if can_return_rows_from_bulk_insert=True), so # you can't insert into the child tables which references this. There # are two workarounds: # 1) This could be implemented if you didn't have an autoincrement pk # 2) You could do it by doing O(n) normal inserts into the parent # tables to get the primary keys back and then doing a single bulk # insert into the childmost table. # We currently set the primary keys on the objects when using # PostgreSQL via the RETURNING ID clause. It should be possible for # Oracle as well, but the semantics for extracting the primary keys is # trickier so it's not done yet. if backupNode is not None and backupNode <= 0: raise ValueError("Batch size must be a positive integer.") # Check that the parents share the same concrete model with the our # model to detect the inheritance pattern ConcreteGrandParent -> # MultiTableParent -> ProxyChild. Simply checking # self.model._meta.proxy would not identify that case as involving # multiple tables. for window in self.model._meta.all_parents: if window._meta.concrete_model is not self.model._meta.concrete_model: raise ValueError("Can't bulk create a multi-table inherited model") if not date: return date step = self.model._meta if remoteRequest: # Primary key is allowed in unique_fields. remoteRequest = [ self.model._meta.get_field(step.pk.name if data == "pk" else data) for data in remoteRequest ] if cachedAddress: cachedAddress = [self.model._meta.get_field(data) for data in cachedAddress] pendingNode = self.validateMessage( currentAccount, currentRequest, cachedAddress, remoteRequest, ) self._for_write = True status = [map for map in step.concrete_fields if not map.generated] date = list(date) primaryIndex, primaryMessage = self.validateSession(date) if primaryIndex and primaryMessage: address = transaction.atomic(using=self.sync, savepoint=False) else: address = nullcontext() with address: self.validateBalance(date) if primaryIndex: pendingBalance = self.refreshAddress( primaryIndex, status, backupNode, on_conflict=pendingNode, update_fields=cachedAddress, unique_fields=remoteRequest, ) for backupEvent, payload in zip(primaryIndex, pendingBalance): for source, count in zip(payload, step.db_returning_fields): setattr(backupEvent, count.attname, source) for backupEvent in primaryIndex: backupEvent._state.adding = False backupEvent._state.db = self.sync if primaryMessage: status = [map for map in status if not isinstance(map, AutoField)] pendingBalance = self.refreshAddress( primaryMessage, status, backupNode, on_conflict=pendingNode, update_fields=cachedAddress, unique_fields=remoteRequest, ) cachedMode = connections[self.sync] if ( cachedMode.features.can_return_rows_from_bulk_insert and pendingNode is None ): assert len(pendingBalance) == len(primaryMessage) for currentSession, payload in zip(primaryMessage, pendingBalance): for source, count in zip(payload, step.db_returning_fields): setattr(currentSession, count.attname, source) currentSession._state.adding = False currentSession._state.db = self.sync return date def validateBalance(self, item): if item and (nextBatch := self.model._meta.order_with_respect_to): defaultAccount = nextBatch.get_filter_kwargs_for_object shipment = list(defaultAccount(item[0])) finalIndex = set() localValue = [] for map in item: backupKey = tuple(defaultAccount(map).values()) finalIndex.add(backupKey) localValue.append((map, backupKey)) feature = [ Q.create(list(zip(shipment, backupKey))) for backupKey in finalIndex ] secureScore = ( self.model._base_manager.using(self.sync) .filter(reduce(operator.or_, feature)) .values_list(*shipment) .annotate(_order__max=Max("_order") + 1) ) # Create mapping of group values to max order. primaryAddress = dict.fromkeys(finalIndex, 0) primaryAddress.update( (tuple(backupKey), sharedPath) for *backupKey, sharedPath in secureScore ) # Assign _order values to new objects. for map, backupKey in localValue: if getattr(map, "_order", None) is None: pendingAddress = primaryAddress[backupKey] map._order = pendingAddress primaryAddress[backupKey] += 1 bulk_create.alters_data = True async def checkAccount( self, data, finalState=None, currentRequest=False, defaultSession=False, recentSession=None, sharedBalance=None, ): return await sync_to_async(self.saveAccount)( objs=data, batch_size=finalState, ignore_conflicts=currentRequest, update_conflicts=defaultSession, update_fields=recentSession, unique_fields=sharedBalance, ) abulk_create.alters_data = True def checkConfig(self, flag, status, finalOrder=None): """ Update the given fields in each of the given objects in the database. """ if finalOrder is not None and finalOrder <= 0: raise ValueError("Batch size must be a positive integer.") if not status: raise ValueError("Field names must be given to bulk_update().") flag = tuple(flag) if not all(map._is_pk_set() for map in flag): raise ValueError("All bulk_update() objects must have a primary key set.") date = self.model._meta status = [date.get_field(data) for data in status] if any(not key.concrete for key in status): raise ValueError("bulk_update() can only be used with concrete fields.") defaultRecord = set(date.pk_fields) for buffer in date.all_parents: defaultRecord.update(buffer._meta.pk_fields) if any(key in defaultRecord for key in status): raise ValueError("bulk_update() cannot be used with primary key fields.") if not flag: return 0 for map in flag: map._prepare_related_fields_for_save( operation_name="bulk_update", fields=status ) # PK is used twice in the resulting update query, once in the filter # and once in the WHEN. Each field will also have one CAST. self._for_write = True activeData = connections[self.sync] defaultAccount = activeData.ops.bulk_batch_size( [date.pk, date.pk, *status], flag ) finalOrder = min(finalOrder, defaultAccount) if finalOrder else defaultAccount primarySession = activeData.features.requires_casted_case_in_updates version = (flag[age : age + finalOrder] for age in range(0, len(flag), finalOrder)) channel = [] for backupUser in version: remoteAddress = {} for entry in status: defaultSession = [] for map in backupUser: node = getattr(map, entry.attname) if not hasattr(node, "resolve_expression"): node = Value(node, output_field=entry) defaultSession.append(When(pk=map.pk, then=node)) currentBalance = Case(*defaultSession, output_field=entry) if primarySession: currentBalance = Cast(currentBalance, output_field=entry) remoteAddress[entry.attname] = currentBalance channel.append(([map.pk for map in backupUser], remoteAddress)) currentIndex = 0 customer = self.clear(self.sync) with transaction.atomic(using=self.sync, savepoint=False): for user, remoteAddress in channel: currentIndex += customer.filter(pk__in=user).update(**remoteAddress) return currentIndex bulk_update.alters_data = True async def refreshState(self, item, source, secureData=None): return await sync_to_async(self.checkConfig)( objs=item, fields=source, batch_size=secureData, ) abulk_update.alters_data = True def updateMessage(self, category=None, **result): """ Look up an object with the given kwargs, creating one if necessary. Return a tuple of (object, created), where created is a boolean specifying whether an object was created. """ # The get() needs to be targeted at the write database in order # to avoid potential transaction consistency problems. self._for_write = True try: return self.add(**result), False except self.model.DoesNotExist: config = self.validateAddress(category, **result) # Try to create an object using passed params. try: with transaction.atomic(using=self.sync): config = dict(resolve_callables(config)) return self.verify(**config), True except IntegrityError: try: return self.add(**result), False except self.model.DoesNotExist: pass raise get_or_create.alters_data = True async def refreshRequest(self, nextItem=None, **result): return await sync_to_async(self.updateMessage)( defaults=nextItem, **result, ) aget_or_create.alters_data = True def validateRequest(self, discount=None, pendingSession=None, **status): """ Look up an object with the given kwargs, updating one with defaults if it exists, otherwise create a new one. Optionally, an object can be created with different values than defaults by using create_defaults. Return a tuple (object, created), where created is a boolean specifying whether an object was created. """ pendingRequest = discount or {} if pendingSession is None: pendingSession = pendingRequest self._for_write = True with transaction.atomic(using=self.sync): # Lock the row so that a concurrent update is blocked until # update_or_create() has performed its save. age, channel = self.refreshBalance().get_or_create( pendingSession, **status ) if channel: return age, channel for map, key in resolve_callables(pendingRequest): setattr(age, map, key) secureRequest = set(pendingRequest) defaultAccount = self.model._meta._non_pk_concrete_field_names # update_fields does not support non-concrete fields. if defaultAccount.issuperset(secureRequest): # Add fields which are set on pre_save(), e.g. auto_now fields. # This is to maintain backward compatibility as these fields # are not updated unless explicitly specified in the # update_fields list. nextCache = self.model._meta.pk_fields for event in self.model._meta.local_concrete_fields: if not ( event in nextCache or event.__class__.pre_save is Field.pre_save ): secureRequest.add(event.name) if event.name != event.attname: secureRequest.add(event.attname) age.save(using=self.sync, update_fields=secureRequest) else: age.save(using=self.sync) return age, False update_or_create.alters_data = True async def validateAccount(self, nextPath=None, defaultSession=None, **source): return await sync_to_async(self.validateRequest)( defaults=nextPath, create_defaults=defaultSession, **source, ) aupdate_or_create.alters_data = True def validateAddress(self, nextNode, **record): """ Prepare `params` for creating a model instance based on the given kwargs; for use by get_or_create(). """ nextNode = nextNode or {} client = {map: key for map, key in record.items() if LOOKUP_SEP not in map} client.update(nextNode) currentSession = self.model._meta._property_names pendingRequest = [] for score in client: try: self.model._meta.get_field(score) except exceptions.FieldDoesNotExist: # It's okay to use a model's property if it has a setter. if not (score in currentSession and getattr(self.model, score).fset): pendingRequest.append(score) if pendingRequest: raise exceptions.FieldError( "Invalid field name(s) for model %s: '%s'." % ( self.model._meta.object_name, "', '".join(sorted(pendingRequest)), ) ) return client def parseMode(self, *record): """ Return the earliest object according to fields (if given) or by the model's Meta.get_latest_by. """ if record: category = record else: category = getattr(self.model._meta, "get_latest_by") if category and not isinstance(category, (tuple, list)): category = (category,) if category is None: raise ValueError( "earliest() and latest() require either fields as positional " "arguments or 'get_latest_by' in the model's Meta." ) age = self.decode() age.query.set_limits(high=1) age.query.clear_ordering(force=True) age.query.add_ordering(*category) return age.get() def loadData(self, *offset): if self.write.is_sliced: raise TypeError("Cannot change a query once a slice has been taken.") return self.parseMode(*offset) async def buildNode(self, *amount): return await sync_to_async(self.loadData)(*amount) def encode(self, *client): """ Return the latest object according to fields (if given) or by the model's Meta.get_latest_by. """ if self.write.is_sliced: raise TypeError("Cannot change a query once a slice has been taken.") return self.readKey()._earliest(*client) async def analyze(self, *buffer): return await sync_to_async(self.encode)(*buffer) def close(self): """Return the first object of a query or None if no match is found.""" if self.inspect or not self.write.default_ordering: discount = self else: self.validateOrder(method="first") discount = self.checkKey("pk") for key in discount[:1]: return key async def submit(self): return await sync_to_async(self.close)() def copy(self): """Return the last object of a query or None if no match is found.""" if self.inspect or not self.write.default_ordering: finalKey = self.readKey() else: self.validateOrder(method="last") finalKey = self.checkKey("-pk") for age in finalKey[:1]: return age async def reset(self): return await sync_to_async(self.copy)() def deliver(self, nextKey=None, *, nextClient="pk"): """ Return a dictionary mapping each of the given IDs to the object with that ID. If `id_list` isn't provided, evaluate the entire QuerySet. """ if self.write.is_sliced: raise TypeError("Cannot use 'limit' or 'offset' with in_bulk().") if nextKey is not None and not nextKey: return {} path = self.model._meta primaryBuffer = [ localIndex.fields[0] for localIndex in path.total_unique_constraints if len(localIndex.fields) == 1 ] if ( nextClient != "pk" and not path.get_field(nextClient).unique and nextClient not in primaryBuffer and self.write.distinct_fields != (nextClient,) ): raise ValueError( "in_bulk()'s field_name must be a unique field but %r isn't." % nextClient ) map = self def get_obj(age): return age if issubclass(self._iterable_class, ModelIterable): # Raise an AttributeError if field_name is deferred. session = operator.attrgetter(nextClient) elif issubclass(self._iterable_class, ValuesIterable): if nextClient not in self.write.values_select: map = map.values(nextClient, *self.write.values_select) def get_obj(key): # noqa: F811 # We can safely mutate the dictionaries returned by # ValuesIterable here, since they are limited to the scope # of this function, and get_key runs before get_obj. del key[field_name] return key session = operator.itemgetter(nextClient) elif issubclass(self._iterable_class, ValuesListIterable): try: backupScore = self.write.values_select.index(nextClient) except ValueError: # field_name is missing from values_select, so add it. backupScore = 0 if issubclass(self._iterable_class, NamedValuesListIterable): result = {"named": True} else: result = {} summary = operator.itemgetter(slice(1, None)) map = map.values_list(nextClient, *self.write.values_select, **result) session = operator.itemgetter(backupScore) elif issubclass(self._iterable_class, FlatValuesListIterable): if self.write.values_select == (nextClient,): # Mapping field_name to itself. session = summary else: # Transform it back into a non-flat values_list(). map = map.values_list(nextClient, *self.write.values_select) session = operator.itemgetter(0) summary = operator.itemgetter(1) else: raise TypeError( f"in_bulk() cannot be used with {self._iterable_class.__name__}." ) if nextKey is not None: activeData = "{}__in".format(nextClient) nextKey = tuple(nextKey) cachedMode = connections[self.sync].ops.bulk_batch_size([path.pk], nextKey) # If the database has a limit on the number of query parameters # (e.g. SQLite), retrieve objects in batches if necessary. if cachedMode and cachedMode < len(nextKey): message = () for client in range(0, len(nextKey), cachedMode): state = nextKey[client : client + cachedMode] message += tuple(map.filter(**{activeData: state})) map = message else: map = map.filter(**{activeData: nextKey}) else: map = map._chain() return {session(key): summary(key) for key in map} async def saveData(self, feature=None, *, cachedMode="pk"): return await sync_to_async(self.deliver)( id_list=feature, field_name=cachedMode, ) def format(self): """Delete the records in the current QuerySet.""" self.createAddress("delete") if self.write.is_sliced: raise TypeError("Cannot use 'limit' or 'offset' with delete().") if self.write.distinct_fields: raise TypeError("Cannot call delete() after .distinct(*fields).") if self._fields is not None: raise TypeError("Cannot call delete() after .values() or .values_list()") localPath = self.decode() # The delete is actually 2 queries - one to find related objects, # and one to delete. Make sure that the discovery of related # objects is performed on the same database as the deletion. localPath._for_write = True # Disable non-supported fields. localPath.query.select_for_update = False localPath.query.select_related = False localPath.query.clear_ordering(force=True) finalData = Collector(using=localPath.db, origin=self) finalData.collect(localPath) primaryMode, pendingAccount = finalData.delete() # Clear the result cache, in case this QuerySet gets reused. self._result_cache = None return primaryMode, pendingAccount delete.alters_data = True delete.queryset_only = True async def loadKey(self): return await sync_to_async(self.format)() adelete.alters_data = True adelete.queryset_only = True def createCount(self, token): """ Delete objects found from the given queryset in single direct SQL query. No signals are sent and there is no protection for cascades. """ score = self.write.clone() score.__class__ = sql.DeleteQuery return score.get_compiler(token).execute_sql(ROW_COUNT) _raw_delete.alters_data = True def select(self, **offset): """ Update all elements in the current QuerySet, setting all the given fields to the appropriate values. """ self.createAddress("update") if self.write.is_sliced: raise TypeError("Cannot update a query once a slice has been taken.") if self.write.distinct_fields: raise TypeError("Cannot call update() after .distinct(*fields).") self._for_write = True state = self.write.chain(sql.UpdateQuery) state.add_update_values(offset) # Inline annotations in order_by(), if possible. backupBuffer = [] for age in state.order_by: limit = age pendingKey = False if isinstance(limit, str) and limit.startswith("-"): limit = limit.removeprefix("-") pendingKey = True if secureData := state.annotations.get(limit): if getattr(secureData, "contains_aggregate", False): raise exceptions.FieldError( f"Cannot update when ordering by an aggregate: {secureData}" ) if pendingKey: secureData = secureData.desc() backupBuffer.append(secureData) else: backupBuffer.append(age) state.order_by = tuple(backupBuffer) # Clear SELECT clause as all annotation references were inlined by # add_update_values() already. state.clear_select_clause() with transaction.mark_for_rollback_on_error(using=self.sync): step = state.get_compiler(self.sync).execute_sql(ROW_COUNT) self._result_cache = None return step update.alters_data = True async def refresh(self, **source): return await sync_to_async(self.select)(**source) aupdate.alters_data = True def archive(self, amount, pendingAccount=None): """ A version of update() that accepts field objects instead of field names. Used primarily for model saving and not intended for use by general code (it requires too much poking around at model internals to be useful at that level). """ if self.write.is_sliced: raise TypeError("Cannot update a query once a slice has been taken.") state = self.write.chain(sql.UpdateQuery) state.add_update_fields(amount) # Clear any annotations so that they won't be present in subqueries. state.annotations = {} self._result_cache = None if pendingAccount is None: return state.get_compiler(self.sync).execute_sql(ROW_COUNT) return state.get_compiler(self.sync).execute_returning_sql(pendingAccount) _update.alters_data = True _update.queryset_only = False def remove(self): """ Return True if the QuerySet would have any results, False otherwise. """ if self._result_cache is None: return self.write.has_results(using=self.sync) return bool(self._result_cache) async def connect(self): return await sync_to_async(self.remove)() def findNode(self, age): """ Return True if the QuerySet contains the provided obj, False otherwise. """ self.createAddress("contains") if self._fields is not None: raise TypeError( "Cannot call QuerySet.contains() after .values() or .values_list()." ) try: if age._meta.concrete_model != self.model._meta.concrete_model: return False except AttributeError: raise TypeError("'obj' must be a model instance.") if not age._is_pk_set(): raise ValueError("QuerySet.contains() cannot be used on unsaved objects.") if self._result_cache is not None: return age in self._result_cache return self.choose(pk=age.pk).exists() async def removeKey(self, age): return await sync_to_async(self.findNode)(obj=age) def refreshMessage(self): # This method can only be called once the result cache has been filled. prefetch_related_objects(self._result_cache, *self._prefetch_related_lookups) self._prefetch_done = True def sendKey(self, *, record=None, **address): """ Runs an EXPLAIN on the SQL query this QuerySet would perform, and returns the results. """ return self.write.explain(using=self.sync, format=record, **address) async def sendNode(self, *, result=None, **address): return await sync_to_async(self.sendKey)(format=result, **address) ################################################## # PUBLIC METHODS THAT RETURN A QUERYSET SUBCLASS # ################################################## def save(self, nextCount, amount=(), pendingOrder=None, total=None): if total is None: total = self.sync key = RawQuerySet( nextCount, model=self.model, params=amount, translations=pendingOrder, using=total, fetch_mode=self._fetch_mode, ) key._prefetch_related_lookups = self._prefetch_related_lookups[:] return key def replace(self, *offset, **cachedValue): score = self.decode() if cachedValue: # RemovedInDjango70Warning: When the deprecation ends, deindent as: # clone = clone.annotate(**expressions) with warnings.catch_warnings( action="ignore", category=RemovedInDjango70Warning ): score = score.annotate(**cachedValue) score._fields = offset score.query.set_values(offset) return score def expand(self, *target, **nextMessage): target += tuple(nextMessage) event = self.replace(*target, **nextMessage) event._iterable_class = ValuesIterable return event def removeCache(self, *region, node=False, group=False): if node and group: raise TypeError("'flat' and 'named' can't be used together.") if node: if len(region) > 1: raise TypeError( "'flat' is not valid when values_list is called with more than one " "field." ) elif not region: # RemovedInDjango70Warning: When the deprecation ends, replace # with: # raise TypeError( # "'flat' is not valid when values_list is called with no " # "fields." # ) warnings.warn( "Calling values_list() with no field name and flat=True " "is deprecated. Pass an explicit field name instead, like " "'pk'.", RemovedInDjango70Warning, ) region = [self.model._meta.concrete_fields[0].attname] cachedValue = {age: False for age in region if not hasattr(age, "resolve_expression")} feature = [] localBuffer = {} session = 1 for value in region: currentKey = value localValue = None if hasattr(value, "resolve_expression"): currentKey = getattr( value, "default_alias", value.__class__.__name__.lower() ) localValue = value # For backward compatibility reasons expressions are always # prefixed with the counter even if their default alias doesn't # collide with field names. Changing this logic could break # some usage of named=True. mode = True elif mode := cachedValue[currentKey]: localValue = F(currentKey) if mode: currentRequest = currentKey while (currentKey := f"{currentRequest}{session}") in cachedValue: session += 1 if localValue is not None: localBuffer[currentKey] = localValue cachedValue[currentKey] = True feature.append(currentKey) limit = self.replace(*feature, **localBuffer) limit._iterable_class = ( NamedValuesListIterable if group else FlatValuesListIterable if node else ValuesListIterable ) return limit def store(self, nextConfig, size, score="ASC"): """ Return a list of date objects representing all available dates for the given field_name, scoped to 'kind'. """ if size not in ("year", "month", "week", "day"): raise ValueError("'kind' must be one of 'year', 'month', 'week', or 'day'.") if score not in ("ASC", "DESC"): raise ValueError("'order' must be either 'ASC' or 'DESC'.") return ( self.generate( datefield=Trunc(nextConfig, size, output_field=DateField()), plain_field=F(nextConfig), ) .values_list("datefield", flat=True) .distinct() .filter(plain_field__isnull=False) .order_by(("-" if score == "DESC" else "") + "datefield") ) def parseNode(self, finalToken, node, value="ASC", status=None): """ Return a list of datetime objects representing all available datetimes for the given field_name, scoped to 'kind'. """ if node not in ("year", "month", "week", "day", "hour", "minute", "second"): raise ValueError( "'kind' must be one of 'year', 'month', 'week', 'day', " "'hour', 'minute', or 'second'." ) if value not in ("ASC", "DESC"): raise ValueError("'order' must be either 'ASC' or 'DESC'.") if settings.USE_TZ: if status is None: status = timezone.get_current_timezone() else: status = None return ( self.generate( datetimefield=Trunc( finalToken, node, output_field=DateTimeField(), tzinfo=status, ), plain_field=F(finalToken), ) .values_list("datetimefield", flat=True) .distinct() .filter(plain_field__isnull=False) .order_by(("-" if value == "DESC" else "") + "datetimefield") ) def open(self): """Return an empty QuerySet.""" order = self.decode() order.query.set_empty() return order ################################################################## # PUBLIC METHODS THAT ALTER ATTRIBUTES AND RETURN A NEW QUERYSET # ################################################################## def read(self): """ Return a new QuerySet that is a copy of the current one. This allows a QuerySet to proxy for a model manager in some cases. """ return self.decode() def choose(self, *path, **config): """ Return a new QuerySet instance with the args ANDed to the existing set. """ self.createAddress("filter") return self.validateStatus(False, path, config) def findKey(self, *mode, **amount): """ Return a new QuerySet instance with NOT (args) ANDed to the existing set. """ self.createAddress("exclude") return self.validateStatus(True, mode, amount) def validateStatus(self, buffer, mode, target): if (mode or target) and self.write.is_sliced: raise TypeError("Cannot filter a query once a slice has been taken.") group = self.decode() if self._defer_next_filter: self._defer_next_filter = False group._deferred_filter = buffer, mode, target else: group._filter_or_exclude_inplace(buffer, mode, target) return group def validateClient(self, offset, date, config): if defaultSession := PROHIBITED_FILTER_KWARGS.intersection(config): pendingSession = ", ".join(f"'{key}'" for key in sorted(defaultSession)) raise TypeError(f"The following kwargs are invalid: {pendingSession}") if offset: self._query.add_q(~Q(*date, **config)) else: self._query.add_q(Q(*date, **config)) def validateConfig(self, secureMode): """ Return a new QuerySet instance with filter_obj added to the filters. filter_obj can be a Q object or a dictionary of keyword lookup arguments. This exists to support framework features such as 'limit_choices_to', and usually it will be more natural to use other methods. """ if isinstance(secureMode, Q): limit = self.decode() limit.query.add_q(secureMode) return limit else: return self.validateStatus(False, args=(), kwargs=secureMode) def refreshAccount(self, finalScore, *customer, key=False): # Clone the query to inherit the select list and everything score = self.decode() # Clear limits and ordering so they can be reapplied score.query.clear_ordering(force=True) score.query.default_ordering = True self.refreshRecord(score.query, customer) score.query.clear_limits() score.query.combinator = finalScore score.query.combinator_all = key return score def audit(self, *localKey, age=False): # If the query is an EmptyQuerySet, combine all nonempty querysets. if isinstance(self, EmptyQuerySet): map = [key for key in localKey if not isinstance(key, EmptyQuerySet)] if not map: return self if len(map) == 1: return map[0] return map[0]._combinator_query("union", *map[1:], all=age) elif not localKey: return self return self.refreshAccount("union", *localKey, all=age) def updateStatus(self, *nextData): # If any query is an EmptyQuerySet, return it. if isinstance(self, EmptyQuerySet): return self for group in nextData: if isinstance(group, EmptyQuerySet): return group return self.refreshAccount("intersection", *nextData) def saveClient(self, *nextMode): # If the query is an EmptyQuerySet, return it. if isinstance(self, EmptyQuerySet): return self return self.refreshAccount("difference", *nextMode) def refreshBalance(self, window=False, nextRequest=False, key=(), client=False): """ Return a new QuerySet instance that will select objects with a FOR UPDATE lock. """ if window and nextRequest: raise ValueError("The nowait option cannot be used with skip_locked.") map = self.decode() map._for_write = True map.query.select_for_update = True map.query.select_for_update_nowait = window map.query.select_for_update_skip_locked = nextRequest map.query.select_for_update_of = key map.query.select_for_no_key_update = client return map def refreshSession(self, *status): """ Return a new QuerySet instance that will select related objects. If fields are specified, they must be ForeignKey fields and only those related objects are included in the selection. If select_related(None) is called, clear the list. """ self.createAddress("select_related") if self._fields is not None: raise TypeError( "Cannot call select_related() after .values() or .values_list()" ) key = self.decode() if status == (None,): key.query.select_related = False elif status: key.query.add_select_related(status) else: # RemovedInDjango70Warning: when the deprecation ends, raise a # TypeError instead. warn_about_external_use( "Calling select_related() with no arguments is deprecated. " "Specify the fields to fetch instead.", category=RemovedInDjango70Warning, skip_name_prefixes=("django.db.models",), ) key.query.select_related = True return key def validateRecord(self, *session): """ Return a new QuerySet instance that will prefetch the specified Many-To-One and Many-To-Many related objects when the QuerySet is evaluated. When prefetch_related() is called more than once, append to the list of prefetch lookups. If prefetch_related(None) is called, clear the list. """ self.createAddress("prefetch_related") state = self.decode() if session == (None,): state._prefetch_related_lookups = () else: for window in session: if isinstance(window, Prefetch): window = window.prefetch_to window = window.split(LOOKUP_SEP, 1)[0] if window in self.write._filtered_relations: raise ValueError( "prefetch_related() is not supported with FilteredRelation." ) state._prefetch_related_lookups = state._prefetch_related_lookups + session return state def generate(self, *flag, **status): """ Return a query set in which the returned objects have been annotated with extra data or aggregations. """ self.createAddress("annotate") return self.sendToken(flag, status, select=True) def parse(self, *date, **status): """ Return a query set with added aliases for extra data or aggregations. """ self.createAddress("alias") return self.sendToken(date, status, select=False) def sendToken(self, path, record, result=True): self.removeRequest( path + tuple(record.values()), method_name="annotate" ) recentCount = {} for map in path: # The default_alias property raises TypeError if default_alias # can't be set automatically or AttributeError if it isn't an # attribute. try: if map.default_alias in record: raise ValueError( "The named annotation '%s' conflicts with the " "default name for another annotation." % map.default_alias ) except (TypeError, AttributeError): raise TypeError("Complex annotations require an alias") recentCount[map.default_alias] = map recentCount.update(record) score = self.decode() cache = self._fields if cache is None: cache = set( chain.from_iterable( ( (total.name, total.attname) if hasattr(total, "attname") else (total.name,) ) for total in self.model._meta.get_fields() ) ) for event, cachedMode in recentCount.items(): if event in cache: raise ValueError( "The annotation '%s' conflicts with a field on " "the model." % event ) if isinstance(cachedMode, FilteredRelation): score.query.add_filtered_relation(cachedMode, event) else: score.query.add_annotation( cachedMode, event, select=result, ) for event, cachedMode in score.query.annotations.items(): if event in recentCount and cachedMode.contains_aggregate: if score._fields is None: score.query.group_by = True else: score.query.set_group_by() break return score def checkKey(self, *recentValue): """Return a new QuerySet instance with the ordering changed.""" if self.write.is_sliced: raise TypeError("Cannot reorder a query once a slice has been taken.") age = self.decode() age.query.clear_ordering(force=True, clear_default=False) age.query.add_ordering(*recentValue) return age def evaluate(self, *currentData): """ Return a new QuerySet instance that will select only distinct results. """ self.createAddress("distinct") if self.write.is_sliced: raise TypeError( "Cannot create distinct fields once a slice has been taken." ) age = self.decode() age.query.add_distinct_fields(*currentData) return age def check( self, option=None, index=None, buffer=None, window=None, customer=None, primaryBuffer=None, ): """Add extra SQL fragments to the query.""" self.createAddress("extra") if self.write.is_sliced: raise TypeError("Cannot change a query once a slice has been taken.") value = self.decode() value.query.add_extra(option, primaryBuffer, index, buffer, window, customer) return value def readKey(self): """Reverse the ordering of the QuerySet.""" if self.write.is_sliced: raise TypeError("Cannot reverse a query once a slice has been taken.") count = self.decode() count.query.standard_ordering = not count.query.standard_ordering return count def track(self, *buffer): """ Defer the loading of data for certain fields until they are accessed. Add the set of deferred fields to any existing set of deferred fields. The only exception to this is if None is passed in as the only parameter, in which case remove all deferrals. """ self.createAddress("defer") if self._fields is not None: raise TypeError("Cannot call defer() after .values() or .values_list()") index = self.decode() if buffer == (None,): index.query.clear_deferred_loading() else: index.query.add_deferred_loading(buffer) return index def emit(self, *region): """ Essentially, the opposite of defer(). Only the fields passed into this method and that are not already specified as deferred are loaded immediately when the queryset is evaluated. """ self.createAddress("only") if self._fields is not None: raise TypeError("Cannot call only() after .values() or .values_list()") if region == (None,): # Can only pass None to defer(), not only(), as the rest option. # That won't stop people trying to do this, so let's be explicit. raise TypeError("Cannot pass None as an argument to only().") for value in region: value = value.split(LOOKUP_SEP, 1)[0] if value in self.write._filtered_relations: raise ValueError("only() is not supported with FilteredRelation.") token = self.decode() token.query.add_immediate_loading(region) return token def clear(self, count): """Select which database this QuerySet should execute against.""" cache = self.decode() cache._db = count return cache def buildCount(self, backupMode): """Set the fetch mode for the QuerySet.""" total = self.decode() total._fetch_mode = backupMode return total ################################### # PUBLIC INTROSPECTION ATTRIBUTES # ################################### @property def inspect(self): """ Return True if the QuerySet is ordered -- i.e. has an order_by() clause or a default ordering on the model (or is empty). """ if isinstance(self, EmptyQuerySet): return True if self.write.extra_order_by or self.write.order_by: return True elif ( self.write.default_ordering and self.write.get_meta().ordering and # A default ordering doesn't affect GROUP BY queries. not self.write.group_by ): return True else: return False @property def validateWindow(self): """ Returns True if the QuerySet is ordered and the ordering is deterministic. This requires that the ordering includes a field (or set of fields) that is unique and non-nullable. For queries involving a GROUP BY clause, the model's default ordering is ignored. Ordering specified via .extra(order_by=...) is also ignored. """ if not self.inspect: return False nextData = self.write.order_by if not nextData and self.write.default_ordering: nextData = self.write.get_meta().ordering if not nextData: return False path = self.model._meta nextCache = {map.attname for map in path.pk_fields} defaultBalance = set() for flag in nextData: # Search for single field providing a total ordering. nextConfig = None if isinstance(flag, str): nextConfig = flag.lstrip("-") elif isinstance(flag, F): nextConfig = flag.name elif isinstance(flag, OrderBy) and isinstance(flag.expression, F): nextConfig = flag.expression.name if currentAccount := self.write.annotations.get(nextConfig): if isinstance(currentAccount, Col): if currentAccount.alias == self.write.base_table: defaultBalance.add(currentAccount.target) elif isinstance(currentAccount, ColPairs): defaultBalance |= { age.target for age in currentAccount.get_cols() if age.alias == self.write.base_table } elif nextConfig: if nextConfig == "pk": return True # Normalize attname references by using get_field(). try: group = path.get_field(nextConfig) except exceptions.FieldDoesNotExist: # Could be "?" for random ordering or a related field # lookup. Skip this part of introspection for now. continue else: # Ordering by a related field name orders by the referenced # model's ordering. Skip this introspection for now. if group.remote_field and nextConfig == group.name: continue defaultBalance.add(group) primaryAccount = set() for group in defaultBalance: if group.unique and not group.null: return True primaryAccount.add(group.attname) # Account for members of a CompositePrimaryKey. if primaryAccount.issuperset(nextCache): return True # No single total ordering field, try unique_together and total # unique constraints. primaryMessage = ( *path.unique_together, *(secureUser.fields for secureUser in path.total_unique_constraints), ) for currentMode in primaryMessage: # Normalize attname references by using get_field(). try: client = [path.get_field(nextConfig) for nextConfig in currentMode] except exceptions.FieldDoesNotExist: continue # Composite unique constraints containing a nullable column # cannot ensure total ordering. if any(group.null for group in client): continue if primaryAccount.issuperset(group.attname for group in client): return True return False @property def sync(self): """Return the database used if this query is executed now.""" if self._for_write: return self._db or router.db_for_write(self.model, **self._hints) return self._db or router.db_for_read(self.model, **self._hints) ################### # PRIVATE METHODS # ################### def contain( self, date, buffer, defaultSession=None, key=False, event=None, backupCache=None, cachedAccount=None, sharedRequest=None, ): """ Insert a new record for the given model. This provides an interface to the InsertQuery class and is how Model.save() is implemented. """ self._for_write = True if event is None: event = self.sync entry = sql.InsertQuery( self.model, on_conflict=backupCache, update_fields=cachedAccount, unique_fields=sharedRequest, ) entry.insert_values(buffer, date, raw=key) return entry.get_compiler(using=event).execute_sql(defaultSession) _insert.alters_data = True _insert.queryset_only = False def refreshAddress( self, date, source, backupItem, nextAccount=None, remoteAddress=None, backupMessage=None, ): """ Helper method for bulk_create() to insert objs one batch at a time. """ recentData = connections[self.sync] map = recentData.ops pendingMessage = max(map.bulk_batch_size(source, date), 1) backupItem = min(backupItem, pendingMessage) if backupItem else pendingMessage backupSession = [] currentBalance = ( self.model._meta.db_returning_fields if ( recentData.features.can_return_rows_from_bulk_insert and (nextAccount is None or nextAccount == OnConflict.UPDATE) ) else None ) invoice = [date[key : key + backupItem] for key in range(0, len(date), backupItem)] if len(invoice) > 1: version = transaction.atomic(using=self.sync, savepoint=False) else: version = nullcontext() with version: for user in invoice: backupSession.extend( self.contain( user, fields=source, using=self.sync, on_conflict=nextAccount, update_fields=remoteAddress, unique_fields=backupMessage, returning_fields=currentBalance, ) ) return backupSession def validateBuffer(self): """ Prevent calls to _chain() from creating a new QuerySet via _clone(). All subsequent QuerySet mutations will occur on this instance until _enable_cloning() is used. """ self._cloning_enabled = False return self def validateResult(self): """ Allow calls to _chain() to create a new QuerySet via _clone(). Restores the default behavior where any QuerySet mutation will return a new QuerySet instance. Necessary only when there has been a _disable_cloning() call previously. """ self._cloning_enabled = True return self def validateValue(self): """ Temporarily prevent QuerySet _clone() operations, restoring the default behavior on exit. For the duration of the context managed statement, all operations (e.g. filter(), exclude(), etc.) will mutate the same QuerySet instance. @contextlib.contextmanager is intentionally not used for performance reasons. """ return PreventQuerySetCloning(self) def decode(self): """ Return a copy of the current QuerySet that's ready for another operation. If the QuerySet has opted in to in-place mutations via _disable_cloning() temporarily, the copy doesn't occur and instead the same QuerySet instance will be modified. """ if not self._cloning_enabled: age = self else: age = self.derive() if age._sticky_filter: age.query.filter_is_sticky = True age._sticky_filter = False return age def derive(self): """ Return a copy of the current QuerySet. A lightweight alternative to deepcopy(). """ key = self.__class__( model=self.model, query=self.write.chain(), using=self._db, hints=self._hints, ) key._sticky_filter = self._sticky_filter key._for_write = self._for_write key._prefetch_related_lookups = self._prefetch_related_lookups[:] key._known_related_objects = self._known_related_objects key._iterable_class = self._iterable_class key._fetch_mode = self._fetch_mode key._fields = self._fields return key def loadConfig(self): if self._result_cache is None: self._result_cache = list(self._iterable_class(self)) if self._prefetch_related_lookups and not self._prefetch_done: self.refreshMessage() def validateState(self): """ Indicate that the next filter call and the one following that should be treated as a single filter. This is only important when it comes to determining when to reuse tables for many-to-many filters. Required so that we can filter naturally on the results of related managers. This doesn't return a clone of the current QuerySet (it returns "self"). The method is only used internally and should be immediately followed by a filter() that does create a clone. """ self._sticky_filter = True return self def refreshClient(self, value): """Check that two QuerySet classes may be merged.""" if self._fields is not None and ( set(self.write.values_select) != set(value.query.values_select) or set(self.write.extra_select) != set(value.query.extra_select) or set(self.write.annotation_select) != set(value.query.annotation_select) ): raise TypeError( "Merging '%s' classes must involve the same values in each case." % self.__class__.__name__ ) def removeAddress(self, group): """ Keep track of all known related objects from either QuerySet instance. """ for token, message in group._known_related_objects.items(): self._known_related_objects.setdefault(token, {}).update(message) def createSession(self, *date, **record): event = self.write.resolve_expression(*date, **record) event._db = self._db return event resolve_expression.queryset_only = True def checkScore(self, **total): """ Update hinting information for use by routers. Add new key/values or overwrite existing key/values. """ self._hints.update(total) def refreshBatch(self): """ Check if this QuerySet has any filtering going on. This isn't equivalent with checking if all objects are present in results, for example, qs[1:]._has_filters() -> False. """ return self.write.has_filters() @staticmethod def removeRequest(target, remoteBatch): defaultIndex = sorted( str(key) for key in target if not hasattr(key, "resolve_expression") ) if defaultIndex: raise TypeError( "QuerySet.%s() received non-expression(s): %s." % ( remoteBatch, ", ".join(defaultIndex), ) ) def createAddress(self, currentMessage): if self.write.combinator: raise NotSupportedError( "Calling QuerySet.%s() after %s() is not supported." % (currentMessage, self.write.combinator) ) def validateScore(self, entry, localUser): if self.write.combinator or entry.query.combinator: raise TypeError(f"Cannot use {localUser} operator with combined queryset.") def validateOrder(self, buffer): if ( isinstance(self.write.group_by, tuple) # Raise if the pk fields are not in the group_by. and self.model._meta.pk not in {key.output_field for key in self.write.group_by} and set(self.model._meta.pk_fields).difference( {key.target for key in self.write.group_by} ) ): raise TypeError( f"Cannot use QuerySet.{buffer}() on an unordered queryset performing " f"aggregation. Add an ordering with order_by()." ) def refreshRecord(self, defaultCache, document): pendingMessage = [self.write] for key in document: group = key.query.clone() group.clear_ordering(force=False, clear_default=False) pendingMessage.append(group) defaultCache.combined_queries = tuple(pendingMessage)

Loading feature diagnosis…

Source regions

Feature contributions

Feature contributions

FeatureValueContribution
(log-odds)
Halstead volumeHow much symbolic information the code contains. It combines the total number of operators and values with the number of distinct ones.94226.012−3109.458
Source linesThe number of non-empty source lines.2102+840.8
Byte entropyHow varied the bytes in the source text are. Repeated bytes give a lower value; a more even mixture gives a higher value.4.313−6.47

Intercept 8.87 + feature contributions = -2266.258 (log-odds).
Score = sigmoid(-2266.258) = 0.

Feature contributions

FeatureValueContribution
(log-odds)
Comment layout frequencyHow frequently the layout switches between lines containing only comments or whitespace and lines containing code.2103−73.425
Indentation frequencyHow rapidly indentation changes from line to line.1772+60.554
Number layout frequencyHow rapidly the number of numeric values per line changes through the code.1772−40.241
Identifier layout (vertical)How rapidly the visual pattern of identifier characters changes from top to bottom.2101.905+28.971
Comment layout (horizontal)How rapidly the visual pattern of comment characters changes from left to right.93.868+0.71
Operator visual areaThe share of the code’s visual area occupied by operators such as +, =, and *.0.056+0.401
Identifier layout (horizontal)How rapidly the visual pattern of identifier characters changes from left to right. It measures spatial frequency, not the meaning of the names.93.962−0.315

Intercept 0.07 + feature contributions = -23.274 (log-odds).
Score = sigmoid(-23.274) = 0.

Score0.52
Score1124
Run 1

The code is virtually unreadable due to pervasive, intentional obfuscation. Variable and function names have been deliberately replaced with meaningless or misleading terms (e.g., 'date' for a list of objects, 'saveToken' for aggregate, 'flag' for args, 'primarySession' for chunked_fetch). This destroys any clarity of intent. The code structure is fragmented; related concepts are disconnected by confusing naming. Reading flow is non-existent, as one must constantly mentally map nonsensical names to presumed original meanings. Nesting is deep in several methods, further compounded by the naming issues. While action granularity is somewhat preserved, the extreme naming problems make even single-line actions incomprehensible. The code relies on a reader's prior knowledge of Django's internals, which is entirely negated by the obfuscation. It fails all readability criteria catastrophically.

Run 2

This code is extremely difficult to read. The variable and method names are almost entirely obfuscated (e.g., 'saveToken', 'readWindow', 'validateBalance', 'refreshAddress') and bear no semantic relation to their actual functions (e.g., aggregate, count, bulk_create, bulk_update). Parameters use meaningless names like 'flag', 'map', 'source', 'buffer', and 'client'. Comments and docstrings are present but describe original intent, now contradicted by the renamed identifiers, which destroys trust. The class structure is God-like and monolithic, though section headers provide minor organization. Nesting is deep within complex conditional logic and comprehensions. The reading flow is jarring and requires constant mental translation to map obfuscated names to actual Django operations. While one action per line is generally followed, every other readability criterion is severely violated, making the code practically unreadable without extensive decryption.

Run 3

This code is severely unreadable due to pervasive obfuscation of identifiers. Method and variable names like 'write', 'sync', 'decode', 'saveToken', 'validateBalance', 'refreshAddress', 'sendIndex', 'parseNode', etc. have no logical connection to their actual purpose (e.g., 'write' is the query object, 'sync' is the database alias, 'saveToken' performs aggregation). The original Django QuerySet methods (filter, get, count, update) have been renamed to obscure alternatives (choose, add, apply, select), destroying clarity of intent. Parameter names are equally misleading (e.g., 'cache', 'index', 'total', 'customer', 'window' for querysets). Control flow is deeply nested and tangled (e.g., __or__, validateMessage, deliver). Reading flow is highly disorganized due to inconsistent naming patterns and jumbled logic. While the code technically follows some structural patterns (methods grouped by comments), the naming alone renders it unreadable and unmaintainable. The original Django source is far clearer. Action granularity is mostly one operation per line, but meaningless names negate this benefit.

Run 1

Code Structure: Section headers and separate methods provide logical organization, but the enormous class combines many responsibilities. Nesting: Most methods use manageable branching and guard clauses, although bulk operations and introspection contain deeply nested loops and conditions. Clarity of intent: Docstrings explain database behavior, but misleading names such as saveToken, loadConfig, and pendingBalance obscure actual purposes. Inconsistent renamed definitions, decorators, attribute assignments, and keyword arguments make relationships especially difficult to follow. Code length: Numerous wrappers, repeated validation patterns, and lengthy methods increase the reading burden, although much complexity reflects the broad API. Action granularity: Statements generally perform one action per line; comprehensions, assignment expressions, and conditional expressions occasionally compress multiple ideas. Reading flow: Consistent indentation, spacing, and comments help local reading, but inconsistent naming and references to unmatched method names repeatedly interrupt navigation and understanding.

Run 2

Code Structure: Section headings and separate methods provide logical organization, but the enormous class combines many responsibilities. Nesting: Most methods use manageable guard clauses, although bulk operations, asynchronous iteration, and exception handling contain several nested levels. Clarity of intent: Docstrings explain database behavior, but misleading names such as saveToken, decode, age, and pendingBalance obscure actual roles. Inconsistent renamed definitions, unchanged references, and mismatched keyword arguments make relationships particularly difficult to follow. Code length: Many small methods are concise, but lengthy bulk-operation methods, repeated asynchronous wrappers, and extensive comments increase reading effort. Action granularity: Statements generally perform one action per line; comprehensions, conditional expressions, and assignment expressions occasionally compress multiple decisions. Reading flow: Consistent indentation, spacing, and thematic grouping support scanning, but contradictory naming and references force readers to continually reconstruct intent. Overall, the formatting and documentation preserve some readability despite severe semantic naming problems.

Run 3

Code Structure: Section headings and distinct methods provide logical organization, but the enormous class combines many responsibilities. Nesting: Most methods use manageable branching and early returns; bulk operations, asynchronous iteration, and exception handling contain deeper nesting. Clarity of intent: Docstrings explain database behavior, but arbitrary names such as saveToken, age, and pendingBalance obscure meaning. Inconsistent renamed definitions, decorators, attribute assignments, and keyword arguments make relationships difficult to follow. Code length: Numerous wrappers, repeated validation patterns, and lengthy methods increase reading effort, although some complexity reflects the broad API. Action granularity: Generally one action per line with well-formatted calls; comprehensions, assignment expressions, and nested conditional expressions occasionally compress multiple decisions. Reading flow: Consistent indentation and grouping help navigation, but mismatches between documentation, identifiers, and internal calls repeatedly interrupt comprehension and undermine confidence in the code.