Refs #31395 -- Removed support for assigning objects which don't support deepcopy() in setUpTestData().

Per deprecation timeline.
This commit is contained in:
Mariusz Felisiak 2021-09-16 08:09:52 +02:00
parent 97237ad3fe
commit 2e10abeb7f
3 changed files with 5 additions and 41 deletions

View File

@ -1139,23 +1139,7 @@ class TestData:
if instance is None:
return self.data
memo = self.get_memo(instance)
try:
data = deepcopy(self.data, memo)
except TypeError:
# RemovedInDjango41Warning.
msg = (
"Assigning objects which don't support copy.deepcopy() during "
"setUpTestData() is deprecated. Either assign the %s "
"attribute during setUpClass() or setUp(), or add support for "
"deepcopy() to %s.%s.%s."
) % (
self.name,
owner.__module__,
owner.__qualname__,
self.name,
)
warnings.warn(msg, category=RemovedInDjango41Warning, stacklevel=2)
data = self.data
setattr(instance, self.name, data)
return data

View File

@ -246,4 +246,6 @@ in Django 4.1.
See :ref:`deprecated-features-3.2` for details on these changes, including how
to remove usage of these features.
* ...
* Support for assigning objects which don't support creating deep copies with
``copy.deepcopy()`` to class attributes in ``TestCase.setUpTestData()`` is
removed.

View File

@ -1,9 +1,8 @@
from functools import wraps
from django.db import IntegrityError, connections, transaction
from django.test import TestCase, ignore_warnings, skipUnlessDBFeature
from django.test import TestCase, skipUnlessDBFeature
from django.test.testcases import TestData
from django.utils.deprecation import RemovedInDjango41Warning
from .models import Car, Person, PossessedCar
@ -54,11 +53,6 @@ class TestTestCase(TestCase):
self.reset_sequences = old_reset_sequences
class NonDeepCopyAble:
def __deepcopy__(self, memo):
raise TypeError
def assert_no_queries(test):
@wraps(test)
def inner(self):
@ -79,7 +73,6 @@ class TestDataTests(TestCase):
car=cls.car,
belongs_to=cls.jim_douglas,
)
cls.non_deepcopy_able = NonDeepCopyAble()
@assert_no_queries
def test_class_attribute_equality(self):
@ -104,21 +97,6 @@ class TestDataTests(TestCase):
self.assertIs(self.herbie.car, self.car)
self.assertIs(self.herbie.belongs_to, self.jim_douglas)
@ignore_warnings(category=RemovedInDjango41Warning)
def test_undeepcopyable(self):
self.assertIs(self.non_deepcopy_able, self.__class__.non_deepcopy_able)
def test_undeepcopyable_warning(self):
msg = (
"Assigning objects which don't support copy.deepcopy() during "
"setUpTestData() is deprecated. Either assign the "
"non_deepcopy_able attribute during setUpClass() or setUp(), or "
"add support for deepcopy() to "
"test_utils.test_testcase.TestDataTests.non_deepcopy_able."
)
with self.assertRaisesMessage(RemovedInDjango41Warning, msg):
self.non_deepcopy_able
def test_repr(self):
self.assertEqual(
repr(TestData('attr', 'value')),