-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Django 4.0 compatibility #8178
Django 4.0 compatibility #8178
Changes from all commits
250479d
f651878
2d9eee5
4916854
19b6091
f46c33e
c62e3ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from io import BytesIO | ||
|
||
import django | ||
from django.contrib.auth.models import User | ||
from django.shortcuts import redirect | ||
from django.test import TestCase, override_settings | ||
|
@@ -282,6 +283,10 @@ def test_empty_request_content_type(self): | |
assert request.META['CONTENT_TYPE'] == 'application/json' | ||
|
||
|
||
def check_urlpatterns(cls): | ||
assert urlpatterns is not cls.urlpatterns | ||
|
||
|
||
class TestUrlPatternTestCase(URLPatternsTestCase): | ||
urlpatterns = [ | ||
path('', view), | ||
|
@@ -293,11 +298,18 @@ def setUpClass(cls): | |
super().setUpClass() | ||
assert urlpatterns is cls.urlpatterns | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
assert urlpatterns is cls.urlpatterns | ||
super().tearDownClass() | ||
assert urlpatterns is not cls.urlpatterns | ||
if django.VERSION > (4, 0): | ||
cls.addClassCleanup( | ||
check_urlpatterns, | ||
cls | ||
) | ||
|
||
if django.VERSION < (4, 0): | ||
@classmethod | ||
def tearDownClass(cls): | ||
assert urlpatterns is cls.urlpatterns | ||
super().tearDownClass() | ||
assert urlpatterns is not cls.urlpatterns | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same Nothing about it in the release notes... https://docs.djangoproject.com/en/4.0/releases/4.0/ so I'm curious what prompts this to need changing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the commit I linked — we no longer call I didn't trace the exact call flow... — timings on those enable/disable or There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hrm that's interesting yeah. Looking through the commit in django/django@faba5b7 I can't see any reason why subclasses would need to stop using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, no 😀 — it's not obvious. My suspicion it's the timing on the 😜 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes that seems very likely. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, if you want to adjust please do! (Otherwise I'll have another look later on) |
||
|
||
def test_urlpatterns(self): | ||
assert self.client.get('/').status_code == 200 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
addClassCleanup
a new feature in Django 4.0?I'm not seeing any differences in the docs re. the existing
tearDownClass
... https://docs.djangoproject.com/en/4.0/topics/testing/tools/#simpletestcaseThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addClassCleanup
is fromunittest.TestCase
, added in Python 3.8 https://docs.python.org/3.9/library/unittest.html#unittest.TestCase.addClassCleanupThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Django just happens to be using it from Django 4.0 — See django/django@faba5b7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah fab. How about we switch based on the Python version, rather than the Django version?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should comment on the release notes for this? 🤔 (cc @felixxm )
I don't suppose there are many projects doing anything as subtle as URLPatternsTestCase
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, if you think it might be helpful. Is it not enough to call
super().setUpClass()
at the beginning? 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be... Let me try that after lunch 🥪
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... or use
cls._overridden_settings
and remove redundantcls._override.enable()/disable()
calls 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moving
super().setUpClass()
from the bottom of thesetUpClass
method to the top resolves this without any4.0
branching.Do I properly understand why? Nope.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In other words, this works...