8000 Fixed #35667 -- Improved deprecation warning handling by replacing stacklevel with skip_file_prefixes by blingblin-g · Pull Request #19887 · django/django · GitHub
[go: up one dir, main page]

Skip to content

Conversation

blingblin-g
Copy link
Contributor
@blingblin-g blingblin-g commented Sep 20, 2025

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

  • This PR targets the main branch.
  • The commit message is written in past tense, mentions the ticket number, and ends with a period.
  • I have checked the "Has patch" ticket flag in the Trac system.
  • I have added or updated relevant tests.
  • I have added or updated relevant docs, including release notes if applicable.
  • I have attached screenshots in both light and dark modes for any UI changes.

Copy link
Member
@jacobtylerwalls jacobtylerwalls left a 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:

8000
warnings.warn(
"Cycle in the exception chain detected: exception '%s' "
"encountered again." % exc_value,
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
Copy link
Member

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...

Copy link
Member
@charettes charettes Sep 24, 2025

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,
Copy link
Member

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())
Copy link
Member

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

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.

4 participants

0