Skip to content

Commit

Permalink
Added ContextDict type alias
Browse files Browse the repository at this point in the history
  • Loading branch information
smithdc1 committed Dec 30, 2022
1 parent 0e0bb69 commit c83675d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
6 changes: 4 additions & 2 deletions crispy_forms/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from django.forms import BaseForm
from django.utils.functional import SimpleLazyObject

from crispy_forms.utils import ContextDict


class PrependedAppendedText(Field):
"""
Expand Down Expand Up @@ -100,7 +102,7 @@ def render(
form: BaseForm,
context: Context,
template_pack: str | SimpleLazyObject = TEMPLATE_PACK,
extra_context: dict[int | str | Node, Any] | None = None,
extra_context: ContextDict | None = None,
**kwargs: Any,
) -> SafeString:
extra_context = extra_context.copy() if extra_context is not None else {}
Expand Down Expand Up @@ -507,7 +509,7 @@ def render(
form: BaseForm,
context: Context,
template_pack: str | SimpleLazyObject = TEMPLATE_PACK,
extra_context: dict[int | str | Node, Any] | None = None,
extra_context: ContextDict | None = None,
**kwargs: Any,
) -> SafeString:
# We first render the buttons
Expand Down
7 changes: 4 additions & 3 deletions crispy_forms/templatetags/crispy_forms_field.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any, Iterable, Tuple
from typing import TYPE_CHECKING, Iterable, Tuple

from django import forms, template
from django.conf import settings
from django.template import Context, Node, Variable, loader
from django.template import Context, Variable, loader

from crispy_forms.utils import get_template_pack

Expand All @@ -13,6 +13,7 @@
from django.template.base import Parser, Token
from django.utils.safestring import SafeString

from crispy_forms.utils import ContextDict

register = template.Library()

Expand Down Expand Up @@ -169,7 +170,7 @@ def crispy_addon(
if field:
if not prepend and not append:
raise TypeError("Expected a prepend and/or append argument")
context: dict[int | str | Node, Any] = {
context: ContextDict = {
"field": field,
"form_show_errors": True,
"form_show_labels": form_show_labels,
Expand Down
6 changes: 4 additions & 2 deletions crispy_forms/templatetags/crispy_forms_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from django.template.backends.base import _EngineTemplate
from django.utils.functional import SimpleLazyObject

from crispy_forms.utils import ContextDict


@lru_cache()
def uni_formset_template(template_pack: str | SimpleLazyObject = TEMPLATE_PACK) -> _EngineTemplate:
Expand Down Expand Up @@ -92,7 +94,7 @@ def as_crispy_errors(
"""
if isinstance(form, BaseFormSet):
template = get_template("%s/errors_formset.html" % template_pack)
c: dict[int | str | Node, Any] = {"formset": form}
c: ContextDict = {"formset": form}
else:
template = get_template("%s/errors.html" % template_pack)
c = {"form": form}
Expand Down Expand Up @@ -122,7 +124,7 @@ def as_crispy_field(
if not isinstance(field, BoundField) and settings.DEBUG: # type: ignore [unreachable]
raise CrispyError("|as_crispy_field got passed an invalid or inexistent field")

attributes: dict[int | str | Node, Any] = {
attributes: ContextDict = {
"field": field,
"form_show_errors": True,
"form_show_labels": True,
Expand Down
9 changes: 5 additions & 4 deletions crispy_forms/templatetags/crispy_forms_tags.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from functools import lru_cache
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING

from django import template
from django.conf import settings
Expand All @@ -15,10 +15,11 @@
from django.forms import BaseForm
from django.template import Context
from django.template.backends.base import _EngineTemplate
from django.template.base import Node, Parser, Token
from django.template.base import Parser, Token
from django.utils.functional import SimpleLazyObject
from django.utils.safestring import SafeString

from crispy_forms.utils import ContextDict

register = template.Library()

Expand Down Expand Up @@ -145,7 +146,7 @@ def get_render(self, context: Context) -> Context:

return final_context

def get_response_dict(self, helper: FormHelper, context: Context, is_formset: bool) -> dict[int | str | Node, Any]:
def get_response_dict(self, helper: FormHelper, context: Context, is_formset: bool) -> ContextDict:
"""
Returns a dictionary with all the parameters necessary to render the form/formset in a template.
Expand All @@ -161,7 +162,7 @@ def get_response_dict(self, helper: FormHelper, context: Context, is_formset: bo
form_type = "formset"

# We take form/formset parameters from attrs if they are set, otherwise we use defaults
response_dict: dict[int | str | Node, Any] = {
response_dict: ContextDict = {
"%s_action" % form_type: attrs["attrs"].get("action", ""),
"%s_attrs" % form_type: attrs.get("attrs", ""),
"%s_class" % form_type: attrs["attrs"].get("class", ""),
Expand Down
4 changes: 3 additions & 1 deletion crispy_forms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from crispy_forms.helper import FormHelper
from crispy_forms.layout import HTML, BaseInput, LayoutObject

ContextDict = dict[int | str | Node, Any]


def get_template_pack() -> str:
return getattr(settings, "CRISPY_TEMPLATE_PACK")
Expand All @@ -45,7 +47,7 @@ def render_field( # noqa: C901
layout_object: LayoutObject | None = None,
attrs: dict[str, str] | Sequence[dict[str, str]] | None = None,
template_pack: SimpleLazyObject | str = TEMPLATE_PACK,
extra_context: dict[int | str | Node, Any] | None = None,
extra_context: ContextDict | None = None,
) -> SafeString:
"""
Renders a django-crispy-forms field
Expand Down

0 comments on commit c83675d

Please sign in to comment.