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

Avoid importing django.test package when not testing #8699

Merged
merged 1 commit into from Oct 14, 2022

Conversation

bluetech
Copy link
Contributor

Description

Importing anything from 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.

try:
from django.core.signals import setting_changed
except ImportError:
from django.test.signals import setting_changed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷🏼‍♂️ Seems low risk to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's gone!

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.
@tomchristie tomchristie merged commit 9407833 into encode:master Oct 14, 2022
@tomchristie
Copy link
Member

Let's 'av it.
Thanks. 🙏

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.

None yet

2 participants