Skip to content

Commit bcc674d

Browse files
refactor: type annotations in DebugToolbar
1 parent 7885071 commit bcc674d

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

debug_toolbar/toolbar.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
import logging
66
import re
77
import uuid
8-
from collections import OrderedDict
98
from functools import cache
10-
from typing import TYPE_CHECKING, Any, Optional
9+
from typing import TYPE_CHECKING, Optional
1110

1211
from django.apps import apps
1312
from django.conf import settings
@@ -49,16 +48,9 @@ def __init__(
4948
if panel.enabled:
5049
get_response = panel.process_request
5150
self.process_request = get_response
52-
# Use OrderedDict for the _panels attribute so that items can be efficiently
53-
# removed using FIFO order in the DebugToolbar.store() method. The .popitem()
54-
# method of Python's built-in dict only supports LIFO removal.
55-
# type: ignore[var-annotated]
56-
self._panels = OrderedDict()
57-
while panels:
58-
panel = panels.pop()
59-
self._panels[panel.panel_id] = panel
60-
self.stats: dict[str, Any] = {}
61-
self.server_timing_stats: dict[str, Any] = {}
51+
self._panels = {panel.panel_id: panel for panel in reversed(panels)}
52+
self.stats = {}
53+
self.server_timing_stats = {}
6254
self.request_id = request_id
6355
self.init_store()
6456
self._created.send(request, toolbar=self)
@@ -73,7 +65,7 @@ def panels(self) -> list["Panel"]:
7365
return list(self._panels.values())
7466

7567
@property
76-
def enabled_panels(self) -> list["Panel"]:
68+
def enabled_panels(self) -> list[Panel]:
7769
"""
7870
Get a list of panels enabled for the current request.
7971
"""
@@ -89,7 +81,7 @@ def csp_nonce(self):
8981
"""
9082
return getattr(self.request, "csp_nonce", None)
9183

92-
def get_panel_by_id(self, panel_id: str) -> "Panel":
84+
def get_panel_by_id(self, panel_id: str) -> Panel:
9385
"""
9486
Get the panel with the given id, which is the class name by default.
9587
"""
@@ -142,13 +134,10 @@ def fetch(cls, request_id, panel_id=None):
142134
if get_store().exists(request_id):
143135
return StoredDebugToolbar.from_store(request_id, panel_id=panel_id)
144136

145-
# Manually implement class-level caching of panel classes and url patterns
146-
# because it's more obvious than going through an abstraction.
147-
148137
_panel_classes: Optional[list[type["Panel"]]] = None
149138

150139
@classmethod
151-
def get_panel_classes(cls) -> list[type["Panel"]]:
140+
def get_panel_classes(cls) -> list[type[Panel]]:
152141
if cls._panel_classes is None:
153142
# Load panels in a temporary variable for thread safety.
154143
panel_classes = [

0 commit comments

Comments
 (0)