Repository: django/django
File: django/db/models/sql/query.py (Python)

Code before:
                    continue
                # names_to_path() validates the lookup. A descriptive
                # FieldError will be raise if it's not.
                self.names_to_path(item.split(LOOKUP_SEP), self.model._meta)
            elif not hasattr(item, "resolve_expression"):
                errors.append(item)
            if getattr(item, "contains_aggregate", False):
                raise FieldError(
                    "Using an aggregate in order_by() without also including "
                    "it in annotate() is not allowed: %s" % item
                )
        if errors:
            raise FieldError("Invalid order_by arguments: %s" % errors)
        if ordering:
            self.order_by += ordering
        else:
            self.default_ordering = False

    @property
    def orderby_issubset_groupby(self):
        """
        Return true if order_by of query is subset of group_by.
        """
        if self.group_by is False:
            # there is no use case for that, but we need to be sure
            return False
        if self.group_by in (None, True):
            # it seems like there is no aggregation at all (None)
            # or there are all required groupbies(True) generated automatically
            # from models fields - so, it is safe to clear ordering
            return True
        if not (self.order_by or self.group_by):
            # empty set is necessarily a subset of the group by
            return True
        # we don't want to harm original query, so we need to clone it
        q = self.clone()
        order_by_set = {
            (
                order_by.resolve_expression(q)
                if hasattr(order_by, "resolve_expression")
                else F(order_by).resolve_expression(q)
            )
            for order_by in q.order_by
        }.union(q.extra_order_by)
        return order_by_set.issubset(self.group_by)

    def clear_ordering(self, force=False, clear_default=True):
        """
        Remove any ordering settings if the current query allows it without
        side effects, set 'force' to True to clear the ordering regardless.
        If 'clear_default' is True, there will be no ordering in the resulting

Review comment (suggestion):
If there is no use case it means it's dead code from its inception and thus and it can be removed.

`Query.group_by` appears to be `None | True | tuple[Compilable]`

Code after:
            elif not hasattr(item, "resolve_expression"):
                errors.append(item)
            if getattr(item, "contains_aggregate", False):
                raise FieldError(
                    "Using an aggregate in order_by() without also including "
                    "it in annotate() is not allowed: %s" % item
                )
        if errors:
            raise FieldError("Invalid order_by arguments: %s" % errors)
        if ordering:
            self.order_by += ordering
        else:
            self.default_ordering = False

    @property
    def orderby_issubset_groupby(self):
        if self.extra_order_by:
            # Raw SQL from extra(order_by=...) can't be reliably compared
            # against resolved OrderBy/Col expressions. Treat as not a subset.
            return False
        if self.group_by in (None, True):
            # There is either no aggregation at all (None), or the group by
            # is generated automatically from model fields (True), in which
            # case the order by is necessarily a subset of them.
            return True
        if not self.order_by:
            # Although an empty set is always a subset, there's no point in
            # clearing ordering when there isn't any. Avoid the clone() below.
            return True
        # Don't pollute the original query (might disrupt joins).
        q = self.clone()
        order_by_set = {
            (
                order_by.resolve_expression(q)
                if hasattr(order_by, "resolve_expression")
                else F(order_by).resolve_expression(q)
            )
            for order_by in q.order_by
        }
        return order_by_set.issubset(self.group_by)

    def clear_ordering(self, force=False, clear_default=True):
        """
        Remove any ordering settings if the current query allows it without
        side effects, set 'force' to True to clear the ordering regardless.
        If 'clear_default' is True, there will be no ordering in the resulting
        query (not even the model's default).
        """
        if not force and (
            self.is_sliced or self.distinct_fields or self.select_for_update
        ):