Skip to content

Conversation

@JohananOppongAmoateng
Copy link
Contributor

Description

This PR adds type hints to the middleware and toolbar modules. It's based on the work of @leandrodesouzadev in this pr #1848

Fixes #1705

Checklist:

  • I have added the relevant tests for this change.
  • I have added an item to the Pending section of docs/changes.rst.

@JohananOppongAmoateng
Copy link
Contributor Author

@tim-schilling @matthiask I suggest we start using https://github.com/typeddjango/django-stubs for the typing

@JohananOppongAmoateng JohananOppongAmoateng marked this pull request as ready for review October 13, 2025 23:37
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":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm likely missing something basic. Why can't we use -> Panel since we have the TYPE_CHECKING import check?

@JohananOppongAmoateng
Copy link
Contributor Author

@tim-schilling have you had time to look at the new changes?

Copy link
Member

@tim-schilling tim-schilling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is coming along nicely, thank you for continuing to work on it. I think there are a few arguments in the method signatures that are missing. I think there may be too many TYPE_CHECKING protections as well, so we should be able to simplify things some more.

Not to be solved right now:

I'm also realizing we need a way to evaluate this programmatically. It's going to be near impossible to keep this inline without some type checking.


@classmethod
def from_store(cls, request_id, panel_id=None):
def from_store(cls, request_id, panel_id=None) -> "StoredDebugToolbar":
Copy link
Member

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?

Copy link
Contributor Author

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

class StoredDebugToolbar(DebugToolbar):
def __init__(self, request, get_response, request_id=None):
def __init__(
self, request: HttpRequest, get_response: "GetResponse", request_id=None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Copy link
Contributor Author

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

@tim-schilling tim-schilling changed the title Type hints/toolbar Add type hints to the toolbar and middlware modules Nov 10, 2025
@tim-schilling tim-schilling changed the title Add type hints to the toolbar and middlware modules Add type hints to the toolbar and middleware modules Nov 10, 2025
Copy link
Member

@tim-schilling tim-schilling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I think this is good to go now. There's a bug / bad design with the stored toolbar class, but I think we can punt on it for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Type hints to the codebase

3 participants