Avoid importing django.test package when not testing#8699
Merged
Conversation
| try: | ||
| from django.core.signals import setting_changed | ||
| except ImportError: | ||
| from django.test.signals import setting_changed |
Contributor
There was a problem hiding this comment.
Can't we always just import it from django.core.signals?
It looks to me like that's where it's defined... https://github.com/django/django/blob/main/django/test/signals.py#L9
It what cases is the ImportError raised?
Contributor
Author
There was a problem hiding this comment.
The signal import path is documented as django.test.signals.setting_changed. So technically the django.core.signals is an implementation detail that is not guaranteed to be stable. So I added the fallback only for future compat. That said, I don't foresee it changing, and even if it does change, it can be fixed in a new release.
Should I remove the fallback?
Contributor
There was a problem hiding this comment.
🤷🏼♂️ Seems low risk to me.
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.
4dd40fc to
235f7e1
Compare
Contributor
|
Let's 'av it. |
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Importing anything from
rest_frameworkcausesdjango.testto be imported. This is because DRF registers a receiver on thedjango.test_signals.setting_changedsignal.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.testimport inMultiPartRenderer, however this import is done lazily only if the functionality is used so can be easily avoided.