Skip to content

Commit

Permalink
Avoid importing django.test package when not testing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bluetech committed Oct 14, 2022
1 parent 1fd268a commit 235f7e1
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion rest_framework/settings.py
Expand Up @@ -19,7 +19,9 @@
back to the defaults.
"""
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 rest_framework import ISO_8601
Expand Down

0 comments on commit 235f7e1

Please sign in to comment.