From a76587531b1f41343ed1f6977a999e4e3a9f6b44 Mon Sep 17 00:00:00 2001 From: Natalia <124304+nessita@users.noreply.github.com> Date: Wed, 16 Jul 2025 14:08:57 -0300 Subject: [PATCH] [5.2.x] Added SimpleTestCase.enterContext() on Python < 3.11. This reverts commit 47a618d45c6e40dd59f4cdd46fd5fc7d11626f6d and uses a solution similar to ed4f83782d9f3404ad600f6131ef78244ff1e162 instead. --- django/test/testcases.py | 4 ++++ tests/composite_pk/test_filter.py | 9 +-------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/django/test/testcases.py b/django/test/testcases.py index 36366bd777..98ca963834 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -404,6 +404,10 @@ class SimpleTestCase(unittest.TestCase): def enterClassContext(cls, cm): return _enter_context(cm, cls.addClassCleanup) + # Backport of unittest.TestCase.enterContext() from Python 3.11. + def enterContext(self, cm): + return _enter_context(cm, self.addCleanup) + def settings(self, **kwargs): """ A context manager that temporarily sets a setting and reverts to the diff --git a/tests/composite_pk/test_filter.py b/tests/composite_pk/test_filter.py index b88d02731c..03037d4d82 100644 --- a/tests/composite_pk/test_filter.py +++ b/tests/composite_pk/test_filter.py @@ -14,7 +14,6 @@ from django.db.models import ( from django.db.models.functions import Cast from django.db.models.lookups import Exact from django.test import TestCase, skipUnlessDBFeature -from django.utils.version import PY311 from .models import Comment, Tenant, User @@ -557,10 +556,4 @@ class CompositePKFilterTupleLookupFallbackTests(CompositePKFilterTests): feature_patch = patch.object( connection.features, "supports_tuple_lookups", False ) - if PY311: - self.enterContext(feature_patch) - else: - # unittest.TestCase.enterContext() was added in Python 3.11. - from django.test.testcases import _enter_context - - _enter_context(feature_patch, self.addCleanup) + self.enterContext(feature_patch)