-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
Fixed #35667 -- Improved deprecation warning handling by replacing stacklevel with skip_file_prefixes #19887
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking this on.
An earlier effort on this ticket also treated warning sites that didn't use stacklevel
at all, for example:
Lines 512 to 516 in 1acb00b
warnings.warn( | |
"Cycle in the exception chain detected: exception '%s' " | |
"encountered again." % exc_value, | |
8000 | ExceptionCycleWarning, |
) |
Are you up for adjusting those, too?
from functools import partial | ||
|
||
from django.core.exceptions import AppRegistryNotReady, ImproperlyConfigured | ||
from django.utils.deprecation import django_file_prefixes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In #18683 (comment), @charettes suggested this shouldn't be dotted under .deprecation
. (I held this feedback when landing #19823.) I think we should move it to django.utils.warnings
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing it out Jacob, I still think it shouldn't live under .deprecation
as it is useful for non-deprecation warnings as well as demonstrated here.
"Model '%s.%s' was already registered. Reloading models is not " | ||
"advised as it can lead to inconsistencies, most notably with " | ||
"related models." % (app_label, model_name), | ||
RuntimeWarning, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... given this is not a deprecation use here.
filename, _, _, _ = stack[-4] | ||
if not filename.startswith(os.path.dirname(django.__file__)): | ||
warnings.warn(message, category, stacklevel=2) | ||
warnings.warn(message, category, skip_file_prefixes=django_file_prefixes()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wondered what this function was for and realized it's no longer used, so I made a removal PR: #19901
Trac ticket number
ticket-35667
Branch description
Improved deprecation warning handling across Django by replacing stacklevel with skip_file_prefixes. This change ensures that deprecation warnings accurately point to the actual user code call sites by skipping Django's internal files, providing clearer and more actionable warning messages. The update leverages the django.utils.deprecation.django_file_prefixes() function to identify Django's internal files and implements consistent warning handling across the codebase including StreamingHttpResponse and AsyncPaginator.
Checklist
main
branch.