-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add type hints to the toolbar and middleware modules #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
Changes from 5 commits
489ff82
4b7584f
3fb0059
53004a4
7885071
bcc674d
86a9258
e9dc9ae
2f7fc1d
9f5ca92
b5196f0
3b2a63e
d839800
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,10 +1,15 @@ | ||||||||
| from typing import TYPE_CHECKING | ||||||||
|
|
||||||||
| from django.core.handlers.asgi import ASGIRequest | ||||||||
| from django.template.loader import render_to_string | ||||||||
| from django.utils.functional import classproperty | ||||||||
|
|
||||||||
| from debug_toolbar import settings as dt_settings | ||||||||
| from debug_toolbar.utils import get_name_from_obj | ||||||||
|
|
||||||||
| if TYPE_CHECKING: | ||||||||
| from debug_toolbar._stubs import GetResponse | ||||||||
|
||||||||
| if TYPE_CHECKING: | |
| from debug_toolbar._stubs import GetResponse | |
| from debug_toolbar._stubs import GetResponse |
From my understanding, it isn't required to check the TYPE_CHECKING for the stubs. Let me know if I'm wrong please.
JohananOppongAmoateng marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
| 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] = {} | ||||||
tim-schilling marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| 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": | ||||||
|
||||||
| """ | ||||||
| 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]: | ||||||
JohananOppongAmoateng marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| 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: HttpRequest, get_response: "GetResponse", request_id=None | |
| self, request: HttpRequest, get_response: GetResponse, request_id=None |
I think this is also missing the type definition for request_id
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.
I was not really sure what to use for the type of request id that why i didnt type it
Outdated
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.
Should the rest of this signature (and debug_toolbar_urls) have types? Or how are you cutting things off?
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.
I think they should but i was trying to do things incrementally
Uh oh!
There was an error while loading. Please reload this page.