Skip to content
Prev Previous commit
Next Next commit
refactor: type annotations in DebugToolbar
  • Loading branch information
JohananOppongAmoateng committed Oct 24, 2025
commit bcc674df23a58aadb80ee750aa90f6975d8acf15
25 changes: 7 additions & 18 deletions debug_toolbar/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import logging
import re
import uuid
from collections import OrderedDict
from functools import cache
from typing import TYPE_CHECKING, Any, Optional
from typing import TYPE_CHECKING, Optional

from django.apps import apps
from django.conf import settings
Expand Down Expand Up @@ -49,16 +48,9 @@ def __init__(
if panel.enabled:
get_response = panel.process_request
self.process_request = get_response
# 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._panels = {panel.panel_id: panel for panel in reversed(panels)}
self.stats = {}
self.server_timing_stats = {}
self.request_id = request_id
self.init_store()
self._created.send(request, toolbar=self)
Expand All @@ -73,7 +65,7 @@ def panels(self) -> list["Panel"]:
return list(self._panels.values())

@property
def enabled_panels(self) -> list["Panel"]:
def enabled_panels(self) -> list[Panel]:
"""
Get a list of panels enabled for the current request.
"""
Expand All @@ -89,7 +81,7 @@ def csp_nonce(self):
"""
return getattr(self.request, "csp_nonce", None)

def get_panel_by_id(self, panel_id: str) -> "Panel":
def get_panel_by_id(self, panel_id: str) -> Panel:
"""
Get the panel with the given id, which is the class name by default.
"""
Expand Down Expand Up @@ -142,13 +134,10 @@ def fetch(cls, request_id, panel_id=None):
if get_store().exists(request_id):
return StoredDebugToolbar.from_store(request_id, panel_id=panel_id)

# Manually implement class-level caching of panel classes and url patterns
# because it's more obvious than going through an abstraction.

_panel_classes: Optional[list[type["Panel"]]] = None

@classmethod
def get_panel_classes(cls) -> list[type["Panel"]]:
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 = [
Expand Down
Loading