-
-
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
Conversation
pytz will not automatically be installed with Django from v4.0.
Refs django/django@faba5b7. addClassCleanup() is available from Python 3.8, which is the minimum supported Python from Django 4.0.
9822649
to
a7e017d
Compare
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.
Alrighty! Thanks so much for cracking on with this Carlton. 😀
I've left a few review comments for cases where it wasn't obvious to me why things have changed. More out of the sake of completeness of review, rather than anything that's necessarily a blocker. I'd actually be happy for it to go in either with or without resolution to these. (But who knows perhaps it'll help tease out some 4.0 release notes or something?)
|
||
if django.VERSION > (4, 0): | ||
cls.addClassCleanup(cls._override.disable) | ||
cls.addClassCleanup(cleanup_url_patterns, cls) |
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/#simpletestcase
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.
addClassCleanup
is from unittest.TestCase
, added in Python 3.8 https://docs.python.org/3.9/library/unittest.html#unittest.TestCase.addClassCleanup
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.
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.
Maybe we should comment on the release notes for this? thinking (cc @felixxm )
Sure, if you think it might be helpful. Is it not enough to call super().setUpClass()
at the beginning? 🤔
@classmethod
def setUpClass(cls):
super().setUpClass()
# Get the module of the TestCase subclass
cls._module = import_module(cls.__module__)
...
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 redundant cls._override.enable()/disable()
calls 🤔
cls._overridden_settings = {
'ROOT_URLCONF': cls.__module__,
**cls._overridden_settings,
}
super().setUpClass()
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 the setUpClass
method to the top resolves this without any 4.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...
@classmethod
def setUpClass(cls):
super().setUpClass() # <--- This line has moved.
# Get the module of the TestCase subclass
cls._module = import_module(cls.__module__)
cls._override = override_settings(ROOT_URLCONF=cls.__module__)
if hasattr(cls._module, 'urlpatterns'):
cls._module_urlpatterns = cls._module.urlpatterns
cls._module.urlpatterns = cls.urlpatterns
cls._override.enable()
@classmethod
def tearDownClass(cls):
super().tearDownClass()
cls._override.disable()
if hasattr(cls, '_module_urlpatterns'):
cls._module.urlpatterns = cls._module_urlpatterns
else:
del cls._module.urlpatterns
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Same tearDownClass
vs addClassCleanup
question as above.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
See the commit I linked — we no longer call tearDown
ourselves so super()
isn't doing what it used to. (I presume the base implementation is empty.)
I didn't trace the exact call flow... — timings on those enable/disable or __enter__
/__exit__
s is tricky, but there were failures without following the change in Django.
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.
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 tearDownClass
, even if Django 4.0 isn't using it directly for it's own implementation. But there's possibly some change in interaction I've not figured out there.
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.
Yeah, no 😀 — it's not obvious.
My suspicion it's the timing on the override_settings
usage — which is always delicate, as we know from e.g. #5668 &friends.
😜
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.
Once super().setUpClass()
is moved in rest_framework/test.py
this class doesn't need to change at all.
(Or at least, that's how it seems from my testing.)
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.
My suspicion it's the timing on the override_settings usage — which is always delicate, as we know from e.g. #5668 &friends.
Yes that seems very likely.
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.
Okay, if you want to adjust please do! (Otherwise I'll have another look later on)
Hey @tomchristie Thanks — I've put initial answers there. Just follow up if you want to dig further. (np my end :) |
USE_L10N defaults to True from Django 4.0, and will be removed in Django 5.0.
Private _get_new_csrf_token() was removed in django/django@231de68.
…ions. zoneinfo was made the default time zone implementation in django/django@306607d.
c62e3ca
to
3f67343
Compare
Grrr.
So added that in 8eefb21. It fails on Python 3.7 (and then the rest of the runs fast-fail and cancel)
Trying different envs locally it looks like the 3.2 to 4.0 change. Just moving the I need to do something else now, but I'll look at @felixxm's suggestions later on. |
3f67343
to
c62e3ca
Compare
OK, likely it's resolvable with enough staring at it, but I can't get it lined up for all versions immediately, so I've reset the PR to its earlier state, which has the branching, but does in fact work. |
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.
Okey dokes. I don't think the implementation details on URLPatternsTestCase
really matter here, so happy for this to go in whenever. (Important thing is that we probably don't think there's anything that needs adding to the Django release notes, right?)
Thanks Tom — In that case I think we should get this in, so we're clean against 4.0a1 from our POV. Folks then testing can then turn up any other issues. |
I'm not exactly sure what I'd say. :) — But I will chat with @felixxm about it next week. |
Adds compatibility for Django 4.0.
From Django 4.0
zoneinfo
is the default time zone implementation. This PR makes the minimum changes to work with that, plus other necessary adjustments.DRF now requires
pytz
ininstall_requires
. There's a small amount of work to be done infields.py
andtest_fields.py
to remove thepytz
dependency. (Django will remove support forpytz
in Django 5.0.)