Skip to content

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
Contributor

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
Contributor

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

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.

2 participants