Skip to content
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

Add HttpRequest subclass with htmx property type hint #411

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Tobi-De
Copy link

@Tobi-De Tobi-De commented Jan 15, 2024

I added this for convenience, I don't know how others handle this, but I was tired of having to add # type: ignore every time I use request.htmx.

class HttpRequest(HttpRequestBase):
    htmx: HtmxDetails

@adamchainz
Copy link
Owner

django-stubs recommends creating a per-project request subclass with your custom User model: https://github.com/typeddjango/django-stubs#how-can-i-create-a-httprequest-thats-guaranteed-to-have-an-authenticated-user . I think this advice probably applies generally for anything your middleware adds to request.

I am hesitant to add a request subclass in the package. It saves one line in your custom request class, which is not a huge ask for projects that use typing. In my experience, there are many custom types needed to properly type a Django project, even with everything django-stubs gives us.

Perhaps we could make this a documentation-only change, showing how to define htmx on a custom request class on top of django-stubs’ recommendation?

@Tobi-De
Copy link
Author

Tobi-De commented Jan 22, 2024

django-stubs recommends creating a per-project request subclass with your custom User model: typeddjango/django-stubs#how-can-i-create-a-httprequest-thats-guaranteed-to-have-an-authenticated-user . I think this advice probably applies generally for anything your middleware adds to request.

I am hesitant to add a request subclass in the package. It saves one line in your custom request class, which is not a huge ask for projects that use typing. In my experience, there are many custom types needed to properly type a Django project, even with everything django-stubs gives us.

Perhaps we could make this a documentation-only change, showing how to define htmx on a custom request class on top of django-stubs’ recommendation?

Okay, understood. I will remove the code and update the docs.
Just curious, are there any conventions on where to place the types? Perhaps in a module like types.py? If not, do you have any suggestions or recommendations?

@Tobi-De
Copy link
Author

Tobi-De commented Jan 22, 2024

Considering the recommendations from django-stubs, here's what I believe is the optimal approach:

from django.http import HttpRequest as HttpRequestBase
from my_user_app.models import MyUser

class HttpRequest(HttpRequestBase):
    htmx: HtmxDetails

class AuthenticatedHttpRequest(HttpRequest):
    user: MyUser

Any thoughts on a more refined solution? Perhaps introducing a dedicated HtmxRequest class could be a cleaner option?

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.

None yet

2 participants