Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Django 4.0 compatibility#8178

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

carltongibson
Copy link
Collaborator

@carltongibsoncarltongibson commentedSep 22, 2021
edited
Loading

Adds compatibility for Django 4.0.

From Django 4.0zoneinfo is the default time zone implementation. This PR makes the minimum changes to work with that, plus other necessary adjustments.

DRF now requirespytz 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.)

auvipy and mjmaurer reacted with hooray emojis4ke, rob4226, and mjmaurer reacted with heart emoji
pytz will not automatically be installed with Django from v4.0.
Refsdjango/django@faba5b7.addClassCleanup() is available from Python 3.8, which is the minimum supportedPython from Django 4.0.
@carltongibsoncarltongibsonforce-pushed thedjango-4.0-compatibility branch 2 times, most recently from9822649 toa7e017dCompareSeptember 22, 2021 08:45
Copy link
Member

@tomchristietomchristie left a 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

IsaddClassCleanup a new feature in Django 4.0?

I'm not seeing any differences in the docs re. the existingtearDownClass...https://docs.djangoproject.com/en/4.0/topics/testing/tools/#simpletestcase

Copy link
CollaboratorAuthor

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.8https://docs.python.org/3.9/library/unittest.html#unittest.TestCase.addClassCleanup

Copy link
CollaboratorAuthor

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 — Seedjango/django@faba5b7

Copy link
Member

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?

Copy link
CollaboratorAuthor

@carltongibsoncarltongibsonSep 23, 2021
edited
Loading

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 asURLPatternsTestCase

Copy link
Contributor

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 callsuper().setUpClass() at the beginning? 🤔

@classmethoddefsetUpClass(cls):super().setUpClass()# Get the module of the TestCase subclasscls._module=import_module(cls.__module__)        ...

Copy link
CollaboratorAuthor

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 🥪

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

... or usecls._overridden_settings and remove redundantcls._override.enable()/disable() calls 🤔

cls._overridden_settings= {'ROOT_URLCONF':cls.__module__,**cls._overridden_settings,}super().setUpClass()

carltongibson reacted with thumbs up emoji
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Movingsuper().setUpClass() from thebottom of thesetUpClass method to the top resolves this without any4.0 branching.

Do I properly understand why? Nope.

Copy link
Member

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...

@classmethoddefsetUpClass(cls):super().setUpClass()# <--- This line has moved.# Get the module of the TestCase subclasscls._module=import_module(cls.__module__)cls._override=override_settings(ROOT_URLCONF=cls.__module__)ifhasattr(cls._module,'urlpatterns'):cls._module_urlpatterns=cls._module.urlpatternscls._module.urlpatterns=cls.urlpatternscls._override.enable()@classmethoddeftearDownClass(cls):super().tearDownClass()cls._override.disable()ifhasattr(cls,'_module_urlpatterns'):cls._module.urlpatterns=cls._module_urlpatternselse:delcls._module.urlpatterns

felixxm reacted with hooray emoji
def tearDownClass(cls):
assert urlpatterns is cls.urlpatterns
super().tearDownClass()
assert urlpatterns is not cls.urlpatterns
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

SametearDownClass vsaddClassCleanup 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?

Copy link
CollaboratorAuthor

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 calltearDown ourselves sosuper() 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.

Copy link
Member

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 indjango/django@faba5b7 I can'tsee any reason why subclasses would need to stop usingtearDownClass, 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.

Copy link
CollaboratorAuthor

@carltongibsoncarltongibsonSep 23, 2021
edited
Loading

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 ontheoverride_settings usage — which is alwaysdelicate, as we know from e.g.#5668 &friends.

😜

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Oncesuper().setUpClass() is moved inrest_framework/test.py this class doesn't need to change at all.
(Or at least, that's how it seems from my testing.)

carltongibson reacted with thumbs up emoji
Copy link
Member

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 seemsvery likely.

Copy link
CollaboratorAuthor

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)

@carltongibson
Copy link
CollaboratorAuthor

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.
…ions.zoneinfo was made the default time zone implementation indjango/django@306607d.
@carltongibsoncarltongibsonforce-pushed thedjango-4.0-compatibility branch 2 times, most recently fromc62e3ca to3f67343CompareSeptember 23, 2021 12:15
@carltongibson
Copy link
CollaboratorAuthor

Grrr.

In other words, this works...

@classmethoddef setUpClass(cls):     super().setUpClass()  # <--- This line has moved.

So added that in8eefb21.

Itfails on Python 3.7 (and then the rest of the runs fast-fail and cancel)

E   django.core.exceptions.ImproperlyConfigured: The included URLconf 'tests.test_routers' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.1357__________________ test_head_and_options_methods_are_excluded __________________1358tests/schemas/test_coreapi.py:1284: in test_head_and_options_methods_are_excluded1359    inspector = EndpointEnumerator()1360rest_framework/schemas/generators.py:68: in __init__1361    patterns = urls.urlpatterns1362E   AttributeError: module 'tests.test_routers' has no attribute 'urlpatterns'

Trying different envs locally it looks like the 3.2 to 4.0 change. Just moving thesuper() call it passes onpy38-django40 but fails onpy38-django32.

I need to do something else now, but I'll look at@felixxm's suggestions later on.

@carltongibson
Copy link
CollaboratorAuthor

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.

tomchristie and s4ke reacted with thumbs up emoji

Copy link
Member

@tomchristietomchristie left a 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 onURLPatternsTestCase 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?)

@carltongibson
Copy link
CollaboratorAuthor

Thanks Tom — In that case I think we should get this in, so we'reclean against 4.0a1 from our POV. Folks then testing can then turn up any other issues.

@carltongibsoncarltongibson merged commitc62e3ca intoencode:masterSep 24, 2021
@carltongibson
Copy link
CollaboratorAuthor

Important thing is that we probably don't think there's anything that needs adding to the Django release notes, right?

I'm not exactly sure what I'd say. :) — But I will chat with@felixxm about it next week.
😉

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@felixxmfelixxmfelixxm left review comments

@tomchristietomchristietomchristie approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@carltongibson@tomchristie@felixxm

[8]ページ先頭

©2009-2025 Movatter.jp