Skip to content

Commit c0d457b

Browse files
Refactor type hints and imports in history panel and views
Add type hints for panel_id and __all__ in history panel for improved clarity.
1 parent ac02a69 commit c0d457b

File tree

5 files changed

+37
-28
lines changed

5 files changed

+37
-28
lines changed

debug_toolbar/panels/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self, toolbar, get_response: GetResponse):
2222
# Private panel properties
2323

2424
@classproperty
25-
def panel_id(cls):
25+
def panel_id(cls) -> str:
2626
return cls.__name__
2727

2828
@property
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from debug_toolbar.panels.history.panel import HistoryPanel
22

3-
__all__ = [HistoryPanel.panel_id]
3+
__all__: list[str] = [HistoryPanel.panel_id]

debug_toolbar/panels/history/panel.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import contextlib
22
import json
33

4-
from django.http.request import RawPostDataException
4+
from django.http import HttpResponse, QueryDict
5+
from django.http.request import HttpRequest, RawPostDataException
56
from django.template.loader import render_to_string
67
from django.templatetags.static import static
7-
from django.urls import path
8+
from django.urls import URLPattern, path
89
from django.utils import timezone
910
from django.utils.translation import gettext_lazy as _
1011

@@ -21,37 +22,38 @@ class HistoryPanel(Panel):
2122
nav_title = _("History")
2223
template = "debug_toolbar/panels/history.html"
2324

24-
def get_headers(self, request):
25-
headers = super().get_headers(request)
25+
def get_headers(self, request: HttpRequest) -> dict:
26+
headers: dict = super().get_headers(request)
2627
observe_request = self.toolbar.get_observe_request()
2728
request_id = self.toolbar.request_id
2829
if request_id and observe_request(request):
2930
headers["djdt-request-id"] = request_id
3031
return headers
3132

3233
@property
33-
def enabled(self):
34+
def enabled(self) -> bool:
3435
# Do not show the history panel if the panels are rendered on request
3536
# rather than loaded via ajax.
3637
return super().enabled and not self.toolbar.should_render_panels()
3738

3839
@property
39-
def is_historical(self):
40+
def is_historical(self) -> bool:
4041
"""The HistoryPanel should not be included in the historical panels."""
4142
return False
4243

4344
@classmethod
44-
def get_urls(cls):
45+
def get_urls(cls) -> list[URLPattern]:
4546
return [
4647
path("history_sidebar/", views.history_sidebar, name="history_sidebar"),
4748
path("history_refresh/", views.history_refresh, name="history_refresh"),
4849
]
4950

5051
@property
51-
def nav_subtitle(self):
52+
def nav_subtitle(self) -> str:
5253
return self.get_stats().get("request_url", "")
5354

54-
def generate_stats(self, request, response):
55+
def generate_stats(self, request: HttpRequest, response: HttpResponse) -> None:
56+
data: QueryDict | None = None
5557
try:
5658
if request.method == "GET":
5759
data = request.GET.copy()
@@ -69,7 +71,7 @@ def generate_stats(self, request, response):
6971

7072
except RawPostDataException:
7173
# It is not guaranteed that we may read the request data (again).
72-
data = None
74+
pass
7375

7476
self.record_stats(
7577
{
@@ -82,12 +84,12 @@ def generate_stats(self, request, response):
8284
)
8385

8486
@property
85-
def content(self):
87+
def content(self) -> str:
8688
"""Content of the panel when it's displayed in full screen.
8789
8890
Fetch every store for the toolbar and include it in the template.
8991
"""
90-
toolbar_history = {}
92+
toolbar_history: dict[str, dict] = {}
9193
for request_id in reversed(self.toolbar.store.request_ids()):
9294
toolbar_history[request_id] = {
9395
"history_stats": self.toolbar.store.panel(
@@ -113,7 +115,7 @@ def content(self):
113115
)
114116

115117
@property
116-
def scripts(self):
117-
scripts = super().scripts
118+
def scripts(self) -> list[str]:
119+
scripts: list[str] = super().scripts
118120
scripts.append(static("debug_toolbar/js/history.js"))
119121
return scripts

debug_toolbar/panels/history/views.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from django.http import HttpResponseBadRequest, JsonResponse
1+
from django.http import HttpRequest, HttpResponseBadRequest, JsonResponse
22
from django.template.loader import render_to_string
33

44
from debug_toolbar._compat import login_not_required
@@ -11,15 +11,17 @@
1111
@login_not_required
1212
@require_show_toolbar
1313
@render_with_toolbar_language
14-
def history_sidebar(request):
14+
def history_sidebar(
15+
request: HttpRequest,
16+
) -> HttpResponseBadRequest | JsonResponse:
1517
"""Returns the selected debug toolbar history snapshot."""
1618
form = HistoryStoreForm(request.GET)
1719

1820
if form.is_valid():
19-
request_id = form.cleaned_data["request_id"]
20-
toolbar = DebugToolbar.fetch(request_id)
21+
request_id: str = form.cleaned_data["request_id"]
22+
toolbar: DebugToolbar | None = DebugToolbar.fetch(request_id)
2123
exclude_history = form.cleaned_data["exclude_history"]
22-
context = {}
24+
context: dict[str, dict[str, str]] = {}
2325
if toolbar is None:
2426
# When the request_id has been popped already due to
2527
# RESULTS_CACHE_SIZE
@@ -43,12 +45,14 @@ def history_sidebar(request):
4345
@login_not_required
4446
@require_show_toolbar
4547
@render_with_toolbar_language
46-
def history_refresh(request):
48+
def history_refresh(
49+
request: HttpRequest,
50+
) -> HttpResponseBadRequest | JsonResponse:
4751
"""Returns the refreshed list of table rows for the History Panel."""
4852
form = HistoryStoreForm(request.GET)
4953

5054
if form.is_valid():
51-
requests = []
55+
requests: list[dict[str, str]] = []
5256
# Convert to list to handle mutations happening in parallel
5357
for request_id in get_store().request_ids():
5458
toolbar = DebugToolbar.fetch(request_id)

debug_toolbar/views.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
from django.http import JsonResponse
1+
from django.http import HttpRequest, JsonResponse
22
from django.utils.html import escape
33
from django.utils.translation import gettext as _
44

55
from debug_toolbar._compat import login_not_required
66
from debug_toolbar.decorators import render_with_toolbar_language, require_show_toolbar
7-
from debug_toolbar.toolbar import DebugToolbar
7+
from debug_toolbar.panels import Panel
8+
from debug_toolbar.toolbar import DebugToolbar, StoredDebugToolbar
89

910

1011
@login_not_required
1112
@require_show_toolbar
1213
@render_with_toolbar_language
13-
def render_panel(request):
14+
def render_panel(request: HttpRequest) -> JsonResponse:
1415
"""Render the contents of a panel"""
15-
toolbar = DebugToolbar.fetch(request.GET["request_id"], request.GET["panel_id"])
16+
toolbar: StoredDebugToolbar | None = DebugToolbar.fetch(
17+
request.GET["request_id"], request.GET["panel_id"]
18+
)
1619
if toolbar is None:
1720
content = _(
1821
"Data for this panel isn't available anymore. "
@@ -21,7 +24,7 @@ def render_panel(request):
2124
content = f"<p>{escape(content)}</p>"
2225
scripts = []
2326
else:
24-
panel = toolbar.get_panel_by_id(request.GET["panel_id"])
27+
panel: Panel = toolbar.get_panel_by_id(request.GET["panel_id"])
2528
content = panel.content
2629
scripts = panel.scripts
2730
return JsonResponse({"content": content, "scripts": scripts})

0 commit comments

Comments
 (0)