1+ from __future__ import annotations
2+
13import contextlib
24import json
35from functools import partial
4- from typing import Dict , List , Optional , Union
56
67from django .conf import settings
78from django .contrib import messages
@@ -85,12 +86,12 @@ class BaseHxRequest:
8586
8687 name : str = ""
8788 hx_object_name : str = "hx_object"
88- GET_template : Union [ str , list ] = ""
89- POST_template : Union [ str , list ] = ""
90- GET_block : Union [ str , list ] = ""
91- POST_block : Union [ str , list , dict [str , str ] ] = ""
89+ GET_template : str | list = ""
90+ POST_template : str | list = ""
91+ GET_block : str | list = ""
92+ POST_block : str | list | dict [str , str ] = ""
9293 refresh_page : bool = False
93- redirect : str = None
94+ redirect : str | None = None
9495 return_empty : bool = False
9596 no_swap = False
9697 show_messages : bool = True
@@ -118,7 +119,7 @@ def is_post_request(self):
118119 def use_messages (self ):
119120 return getattr (settings , "HX_REQUESTS_USE_HX_MESSAGES" , False ) and self .show_messages
120121
121- def get_context_data (self , ** kwargs ) -> Dict :
122+ def get_context_data (self , ** kwargs ) -> dict :
122123 """
123124 Adds the context from the view and additionally adds:
124125
@@ -143,7 +144,7 @@ def get_context_data(self, **kwargs) -> Dict:
143144 # Turn into dict for template rendering which expects a dict
144145 return context .flatten ()
145146
146- def get_context_on_GET (self , ** kwargs ) -> Dict :
147+ def get_context_on_GET (self , ** kwargs ) -> dict :
147148 """
148149 Adds extra context to the context data only on GET.
149150 """
@@ -188,7 +189,7 @@ def _setup_hx_request(self, request, *args, **kwargs):
188189 def hx_object_to_str (self ) -> str :
189190 return self .hx_object ._meta .model .__name__ .capitalize () if self .hx_object else ""
190191
191- def get_headers (self , ** kwargs ) -> Dict :
192+ def get_headers (self , ** kwargs ) -> dict :
192193 """
193194 Prepare the headers for the response.
194195 """
@@ -204,7 +205,7 @@ def get_headers(self, **kwargs) -> Dict:
204205 headers .update (self .get_trigger_headers (** kwargs ))
205206 return headers
206207
207- def get_triggers (self , ** kwargs ) -> Union [ List [ Union [ str , dict ]], Dict [str , list ] ]:
208+ def get_triggers (self , ** kwargs ) -> list [ str | dict ] | dict [str , list ]:
208209 """
209210 Override to set the triggers for the response.
210211
@@ -232,7 +233,7 @@ def get_triggers(self, **kwargs) -> Union[List[Union[str, dict]], Dict[str, list
232233 """
233234 return []
234235
235- def get_trigger_headers (self , ** kwargs ) -> Dict [str , str ]:
236+ def get_trigger_headers (self , ** kwargs ) -> dict [str , str ]:
236237 """
237238 Build the HTMX trigger response headers from :meth:`get_triggers`.
238239
@@ -266,7 +267,7 @@ def format_triggers(self, **kwargs) -> str:
266267 return self ._format_trigger_value (triggers )
267268
268269 @staticmethod
269- def _format_trigger_value (triggers : List [ Union [ str , dict ] ]) -> str :
270+ def _format_trigger_value (triggers : list [ str | dict ]) -> str :
270271 """
271272 Format a single list of triggers into one header value.
272273
@@ -431,7 +432,7 @@ class FormHxRequest(BaseHxRequest):
431432 set_initial_from_kwargs : bool = False
432433 show_form_invalid_message : bool = True
433434
434- def get_context_data (self , ** kwargs ) -> Dict :
435+ def get_context_data (self , ** kwargs ) -> dict :
435436 """
436437 Additionally adds the form into the context.
437438 """
@@ -460,7 +461,7 @@ def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
460461
461462 return response if response is not None else self ._get_response (** kwargs )
462463
463- def form_valid (self , ** kwargs ) -> Optional [ HttpResponse ] :
464+ def form_valid (self , ** kwargs ) -> HttpResponse | None :
464465 """
465466 Saves the form and sets a success message.
466467
@@ -470,7 +471,7 @@ def form_valid(self, **kwargs) -> Optional[HttpResponse]:
470471 self .form .save ()
471472 messages .success (self .request , self .get_success_message (** kwargs ))
472473
473- def form_invalid (self , ** kwargs ) -> Optional [ HttpResponse ] :
474+ def form_invalid (self , ** kwargs ) -> HttpResponse | None :
474475 """
475476 Sets an error message unless ``show_form_invalid_message`` is False.
476477
@@ -573,7 +574,7 @@ def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
573574 response = self .delete (** kwargs )
574575 return response if response is not None else self ._get_response (** kwargs )
575576
576- def delete (self , ** kwargs ) -> Optional [ HttpResponse ] :
577+ def delete (self , ** kwargs ) -> HttpResponse | None :
577578 """
578579 Deletes the hx_object and sets a success message.
579580
@@ -681,7 +682,7 @@ def get_triggers(self, **kwargs) -> list:
681682 triggers .append ("closeHxModal" )
682683 return triggers
683684
684- def get_headers (self , ** kwargs ) -> Dict :
685+ def get_headers (self , ** kwargs ) -> dict :
685686 """
686687 If the form is invalid, headers are set to retarget the innerHTML of the modal body.
687688 This is done to show the form errors.
0 commit comments