Refs #30158 -- Made alias argument required in signature of Expression.get_group_by_cols() subclasses.

Per deprecation timeline.
This commit is contained in:
Mariusz Felisiak 2021-01-07 07:55:06 +01:00
parent d134b0b93e
commit 5e33ec80d1
3 changed files with 6 additions and 38 deletions

View File

@ -9,7 +9,6 @@ all about the internals of models in order to get the information it needs.
import copy import copy
import difflib import difflib
import functools import functools
import inspect
import sys import sys
import warnings import warnings
from collections import Counter, namedtuple from collections import Counter, namedtuple
@ -2038,19 +2037,9 @@ class Query(BaseExpression):
group_by = list(self.select) group_by = list(self.select)
if self.annotation_select: if self.annotation_select:
for alias, annotation in self.annotation_select.items(): for alias, annotation in self.annotation_select.items():
signature = inspect.signature(annotation.get_group_by_cols) if not allow_aliases or alias in column_names:
if 'alias' not in signature.parameters: alias = None
annotation_class = annotation.__class__ group_by_cols = annotation.get_group_by_cols(alias=alias)
msg = (
'`alias=None` must be added to the signature of '
'%s.%s.get_group_by_cols().'
) % (annotation_class.__module__, annotation_class.__qualname__)
warnings.warn(msg, category=RemovedInDjango40Warning)
group_by_cols = annotation.get_group_by_cols()
else:
if not allow_aliases or alias in column_names:
alias = None
group_by_cols = annotation.get_group_by_cols(alias=alias)
group_by.extend(group_by_cols) group_by.extend(group_by_cols)
self.group_by = tuple(group_by) self.group_by = tuple(group_by)

View File

@ -257,6 +257,9 @@ to remove usage of these features.
* ``django.views.i18n.set_language()`` doesn't set the user language in * ``django.views.i18n.set_language()`` doesn't set the user language in
``request.session`` (key ``_language``). ``request.session`` (key ``_language``).
* ``alias=None`` is required in the signature of
``django.db.models.Expression.get_group_by_cols()`` subclasses.
See :ref:`deprecated-features-3.1` for details on these changes, including how See :ref:`deprecated-features-3.1` for details on these changes, including how
to remove usage of these features. to remove usage of these features.

View File

@ -1,24 +0,0 @@
from django.db.models import Count, Func
from django.test import SimpleTestCase
from django.utils.deprecation import RemovedInDjango40Warning
from .models import Employee
class MissingAliasFunc(Func):
template = '1'
def get_group_by_cols(self):
return []
class GetGroupByColsTest(SimpleTestCase):
def test_missing_alias(self):
msg = (
'`alias=None` must be added to the signature of '
'expressions.test_deprecation.MissingAliasFunc.get_group_by_cols().'
)
with self.assertRaisesMessage(RemovedInDjango40Warning, msg):
Employee.objects.values(
one=MissingAliasFunc(),
).annotate(cnt=Count('company_ceo_set'))