-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Type hints/toolbar #2227
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
Open
JohananOppongAmoateng
wants to merge
5
commits into
django-commons:main
Choose a base branch
from
JohananOppongAmoateng:type-hints/toolbar
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Type hints/toolbar #2227
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
489ff82
typing: adds a GetResponse class and type middleware/toolbar modules
leandrodesouzadev 4b7584f
typing: adds type hints to the toolbar module
leandrodesouzadev 3fb0059
typing: add type hints in middleware, panels, and toolbar modules
JohananOppongAmoateng 53004a4
docs: add documentation for GetResponse class in panels
JohananOppongAmoateng 7885071
Merge branch 'django-commons:main' into type-hints/toolbar
JohananOppongAmoateng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,31 +5,41 @@ | |
import logging | ||
import re | ||
import uuid | ||
from collections import OrderedDict | ||
from functools import cache | ||
from typing import TYPE_CHECKING, Any, Optional | ||
|
||
from django.apps import apps | ||
from django.conf import settings | ||
from django.core.exceptions import ImproperlyConfigured | ||
from django.dispatch import Signal | ||
from django.http import HttpRequest | ||
from django.template import TemplateSyntaxError | ||
from django.template.loader import render_to_string | ||
from django.urls import include, path, re_path, resolve | ||
from django.urls import URLPattern, include, path, re_path, resolve | ||
from django.urls.exceptions import Resolver404 | ||
from django.utils.module_loading import import_string | ||
from django.utils.translation import get_language, override as lang_override | ||
|
||
from debug_toolbar import APP_NAME, settings as dt_settings | ||
from debug_toolbar._stubs import GetResponse | ||
from debug_toolbar.store import get_store | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
if TYPE_CHECKING: | ||
from .panels import Panel | ||
|
||
|
||
class DebugToolbar: | ||
# for internal testing use only | ||
_created = Signal() | ||
store = None | ||
|
||
def __init__(self, request, get_response, request_id=None): | ||
def __init__( | ||
self, request: HttpRequest, get_response: GetResponse, request_id=None | ||
): | ||
self.request = request | ||
self.config = dt_settings.get_config().copy() | ||
panels = [] | ||
|
@@ -39,24 +49,31 @@ def __init__(self, request, get_response, request_id=None): | |
if panel.enabled: | ||
get_response = panel.process_request | ||
self.process_request = get_response | ||
self._panels = {panel.panel_id: panel for panel in reversed(panels)} | ||
self.stats = {} | ||
self.server_timing_stats = {} | ||
# Use OrderedDict for the _panels attribute so that items can be efficiently | ||
# removed using FIFO order in the DebugToolbar.store() method. The .popitem() | ||
# method of Python's built-in dict only supports LIFO removal. | ||
# type: ignore[var-annotated] | ||
self._panels = OrderedDict() | ||
while panels: | ||
panel = panels.pop() | ||
self._panels[panel.panel_id] = panel | ||
self.stats: dict[str, Any] = {} | ||
self.server_timing_stats: dict[str, Any] = {} | ||
self.request_id = request_id | ||
self.init_store() | ||
self._created.send(request, toolbar=self) | ||
|
||
# Manage panels | ||
|
||
@property | ||
def panels(self): | ||
def panels(self) -> list["Panel"]: | ||
""" | ||
Get a list of all available panels. | ||
""" | ||
return list(self._panels.values()) | ||
|
||
@property | ||
def enabled_panels(self): | ||
def enabled_panels(self) -> list["Panel"]: | ||
""" | ||
Get a list of panels enabled for the current request. | ||
""" | ||
|
@@ -72,15 +89,15 @@ def csp_nonce(self): | |
""" | ||
return getattr(self.request, "csp_nonce", None) | ||
|
||
def get_panel_by_id(self, panel_id): | ||
def get_panel_by_id(self, panel_id: str) -> "Panel": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I'm likely missing something basic. Why can't we use |
||
""" | ||
Get the panel with the given id, which is the class name by default. | ||
""" | ||
return self._panels[panel_id] | ||
|
||
# Handle rendering the toolbar in HTML | ||
|
||
def render_toolbar(self): | ||
def render_toolbar(self) -> str: | ||
""" | ||
Renders the overall Toolbar with panels inside. | ||
""" | ||
|
@@ -101,7 +118,7 @@ def render_toolbar(self): | |
else: | ||
raise | ||
|
||
def should_render_panels(self): | ||
def should_render_panels(self) -> bool: | ||
"""Determine whether the panels should be rendered during the request | ||
|
||
If False, the panels will be loaded via Ajax. | ||
|
@@ -128,10 +145,10 @@ def fetch(cls, request_id, panel_id=None): | |
# Manually implement class-level caching of panel classes and url patterns | ||
# because it's more obvious than going through an abstraction. | ||
|
||
_panel_classes = None | ||
_panel_classes: Optional[list[type["Panel"]]] = None | ||
|
||
@classmethod | ||
def get_panel_classes(cls): | ||
def get_panel_classes(cls) -> list[type["Panel"]]: | ||
if cls._panel_classes is None: | ||
# Load panels in a temporary variable for thread safety. | ||
panel_classes = [ | ||
|
@@ -140,10 +157,10 @@ def get_panel_classes(cls): | |
cls._panel_classes = panel_classes | ||
return cls._panel_classes | ||
|
||
_urlpatterns = None | ||
_urlpatterns: Optional[list[URLPattern]] = None | ||
|
||
@classmethod | ||
def get_urls(cls): | ||
def get_urls(cls) -> list[URLPattern]: | ||
if cls._urlpatterns is None: | ||
from . import views | ||
|
||
|
@@ -159,7 +176,7 @@ def get_urls(cls): | |
return cls._urlpatterns | ||
|
||
@classmethod | ||
def is_toolbar_request(cls, request): | ||
def is_toolbar_request(cls, request: HttpRequest) -> bool: | ||
""" | ||
Determine if the request is for a DebugToolbar view. | ||
""" | ||
|
@@ -171,7 +188,10 @@ def is_toolbar_request(cls, request): | |
) | ||
except Resolver404: | ||
return False | ||
return resolver_match.namespaces and resolver_match.namespaces[-1] == APP_NAME | ||
return ( | ||
bool(resolver_match.namespaces) | ||
and resolver_match.namespaces[-1] == APP_NAME | ||
) | ||
|
||
@staticmethod | ||
@cache | ||
|
@@ -185,7 +205,7 @@ def get_observe_request(): | |
return func_or_path | ||
|
||
|
||
def observe_request(request): | ||
def observe_request(request: HttpRequest): | ||
""" | ||
Determine whether to update the toolbar from a client side request. | ||
""" | ||
|
@@ -200,7 +220,9 @@ def from_store_get_response(request): | |
|
||
|
||
class StoredDebugToolbar(DebugToolbar): | ||
def __init__(self, request, get_response, request_id=None): | ||
def __init__( | ||
self, request: HttpRequest, get_response: "GetResponse", request_id=None | ||
): | ||
self.request = None | ||
self.config = dt_settings.get_config().copy() | ||
self.process_request = get_response | ||
|
@@ -210,7 +232,7 @@ def __init__(self, request, get_response, request_id=None): | |
self.init_store() | ||
|
||
@classmethod | ||
def from_store(cls, request_id, panel_id=None): | ||
def from_store(cls, request_id, panel_id=None) -> "StoredDebugToolbar": | ||
toolbar = StoredDebugToolbar( | ||
None, from_store_get_response, request_id=request_id | ||
) | ||
|
@@ -226,7 +248,7 @@ def from_store(cls, request_id, panel_id=None): | |
return toolbar | ||
|
||
|
||
def debug_toolbar_urls(prefix="__debug__"): | ||
def debug_toolbar_urls(prefix="__debug__") -> list[URLPattern]: | ||
""" | ||
Return a URL pattern for serving toolbar in debug mode. | ||
|
||
|
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
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.
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.
This feels like there may have been a merge conflict issue.