Skip to content

Commit 9407833

Browse files
authored
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.
1 parent 1fd268a commit 9407833

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

rest_framework/settings.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
back to the defaults.
2020
"""
2121
from django.conf import settings
22-
from django.test.signals import setting_changed
22+
# Import from `django.core.signals` instead of the official location
23+
# `django.test.signals` to avoid importing the test module unnecessarily.
24+
from django.core.signals import setting_changed
2325
from django.utils.module_loading import import_string
2426

2527
from rest_framework import ISO_8601

0 commit comments

Comments
 (0)