Skip to content
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

Merged
merged 7 commits into from
Sep 24, 2021

Conversation

carltongibson
Copy link
Collaborator

@carltongibson carltongibson commented Sep 22, 2021

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 in install_requires. There's a small amount of work to be done in fields.py and test_fields.py to remove the pytz dependency. (Django will remove support for pytz in Django 5.0.)

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.
@carltongibson carltongibson force-pushed the django-4.0-compatibility branch 2 times, most recently from 9822649 to a7e017d Compare September 22, 2021 08:45
Copy link
Member

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

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

Copy link
Collaborator Author

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

Copy link
Collaborator Author

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

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
Collaborator Author

@carltongibson carltongibson Sep 23, 2021

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

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

Copy link
Collaborator Author

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 use cls._overridden_settings and remove redundant cls._override.enable()/disable() calls 🤔

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

Copy link
Member

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.

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

    @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

tests/authentication/test_authentication.py Show resolved Hide resolved
tests/conftest.py Show resolved Hide resolved
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.

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?

Copy link
Collaborator Author

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.

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

Copy link
Collaborator Author

@carltongibson carltongibson Sep 23, 2021

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.

😜

Copy link
Member

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

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 seems very likely.

Copy link
Collaborator Author

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
Collaborator Author

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 in
django/django@306607d.
@carltongibson carltongibson force-pushed the django-4.0-compatibility branch 2 times, most recently from c62e3ca to 3f67343 Compare September 23, 2021 12:15
@carltongibson
Copy link
Collaborator Author

Grrr.

In other words, this works...

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

So added that in 8eefb21.

It fails 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 __________________
1358
tests/schemas/test_coreapi.py:1284: in test_head_and_options_methods_are_excluded
1359
    inspector = EndpointEnumerator()
1360
rest_framework/schemas/generators.py:68: in __init__
1361
    patterns = urls.urlpatterns
1362
E   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 the super() call it passes on py38-django40 but fails on py38-django32.

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

@carltongibson carltongibson force-pushed the django-4.0-compatibility branch from 3f67343 to c62e3ca Compare September 23, 2021 14:19
@carltongibson
Copy link
Collaborator Author

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.

Copy link
Member

@tomchristie tomchristie 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 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?)

@carltongibson
Copy link
Collaborator Author

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.

@carltongibson carltongibson merged commit c62e3ca into encode:master Sep 24, 2021
@carltongibson
Copy link
Collaborator Author

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 free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants