Avoid importing django.test package when not testing (#8699)

Importing anything `rest_framework` causes `django.test` to be imported.
This is because DRF registers a receiver on the
`django.test_signals.setting_changed` signal.

This is not really a problem, but it is good to avoid this because it
bloats the memory with unnecessary modules (e.g. `django.test`,
`django.core.servers.basehttp`, `socketserver`) and increases the
startup time. It also doesn't feel right to import test code into
non-test code.

Try to import the signal from a core module if possible.

Note that there's another `django.test` import in `MultiPartRenderer`,
however this import is done lazily only if the functionality is used so
can be easily avoided.
This commit is contained in:
Ran Benita 2022-10-14 18:30:26 +03:00 committed by GitHub
parent 1fd268ae2b
commit 9407833a83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,7 +19,9 @@ REST framework settings, checking for user settings first, then falling
back to the defaults. back to the defaults.
""" """
from django.conf import settings from django.conf import settings
from django.test.signals import setting_changed # Import from `django.core.signals` instead of the official location
# `django.test.signals` to avoid importing the test module unnecessarily.
from django.core.signals import setting_changed
from django.utils.module_loading import import_string from django.utils.module_loading import import_string
from rest_framework import ISO_8601 from rest_framework import ISO_8601