From 578c03b276e435bcd3ce9eb17b81e85135c2d3f3 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sun, 2 Feb 2020 13:15:58 +0300 Subject: [PATCH] Fixed #31223 -- Added __class_getitem__() to Manager and QuerySet. --- django/db/models/manager.py | 3 +++ django/db/models/query.py | 3 +++ tests/managers_regress/tests.py | 7 +++++++ tests/model_inheritance/tests.py | 8 ++++++++ 4 files changed, 21 insertions(+) diff --git a/django/db/models/manager.py b/django/db/models/manager.py index e7a956cc512..e10c15b9ea5 100644 --- a/django/db/models/manager.py +++ b/django/db/models/manager.py @@ -35,6 +35,9 @@ class BaseManager: """Return "app_label.model_label.manager_name".""" return '%s.%s' % (self.model._meta.label, self.name) + def __class_getitem__(cls, *args, **kwargs): + return cls + def deconstruct(self): """ Return a 5-tuple of the form (as_manager (True), manager_class, diff --git a/django/db/models/query.py b/django/db/models/query.py index 1fa7a318c84..d9c9b0db043 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -323,6 +323,9 @@ class QuerySet: qs._fetch_all() return qs._result_cache[0] + def __class_getitem__(cls, *args, **kwargs): + return cls + def __and__(self, other): self._merge_sanity_check(other) if isinstance(other, EmptyQuerySet): diff --git a/tests/managers_regress/tests.py b/tests/managers_regress/tests.py index 3c2ba5e1f1b..7fa6815959c 100644 --- a/tests/managers_regress/tests.py +++ b/tests/managers_regress/tests.py @@ -1,7 +1,10 @@ +from unittest import skipUnless + from django.db import models from django.template import Context, Template from django.test import SimpleTestCase, TestCase, override_settings from django.test.utils import isolate_apps +from django.utils.version import PY37 from .models import ( AbstractBase1, AbstractBase2, AbstractBase3, Child1, Child2, Child3, @@ -285,3 +288,7 @@ class TestManagerInheritance(SimpleTestCase): self.assertEqual(TestModel._meta.managers, (TestModel.custom_manager,)) self.assertEqual(TestModel._meta.managers_map, {'custom_manager': TestModel.custom_manager}) + + @skipUnless(PY37, '__class_getitem__() was added in Python 3.7') + def test_manager_class_getitem(self): + self.assertIs(models.Manager[Child1], models.Manager) diff --git a/tests/model_inheritance/tests.py b/tests/model_inheritance/tests.py index 3568c538f06..60edaa3d99b 100644 --- a/tests/model_inheritance/tests.py +++ b/tests/model_inheritance/tests.py @@ -1,9 +1,11 @@ from operator import attrgetter +from unittest import skipUnless from django.core.exceptions import FieldError, ValidationError from django.db import connection, models from django.test import SimpleTestCase, TestCase from django.test.utils import CaptureQueriesContext, isolate_apps +from django.utils.version import PY37 from .models import ( Base, Chef, CommonInfo, GrandChild, GrandParent, ItalianRestaurant, @@ -217,6 +219,12 @@ class ModelInheritanceTests(TestCase): self.assertSequenceEqual(qs, [p2, p1]) self.assertIn(expected_order_by_sql, str(qs.query)) + @skipUnless(PY37, '__class_getitem__() was added in Python 3.7') + def test_queryset_class_getitem(self): + self.assertIs(models.QuerySet[Post], models.QuerySet) + self.assertIs(models.QuerySet[Post, Post], models.QuerySet) + self.assertIs(models.QuerySet[Post, int, str], models.QuerySet) + class ModelInheritanceDataTests(TestCase): @classmethod