From 856e8c261a18367dddee5ff3a4b20b07c6dbbf19 Mon Sep 17 00:00:00 2001 From: Jae Hyuck Sa Date: Sat, 4 May 2024 04:58:03 +0900 Subject: [PATCH] 5.0: Update `django.forms.boundfield` and `django.forms.utils` (#2118) Co-authored-by: Nikita Sobolev Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- django-stubs/forms/boundfield.pyi | 7 +++++-- django-stubs/forms/utils.pyi | 11 +++++++++-- scripts/stubtest/allowlist.txt | 4 ++++ scripts/stubtest/allowlist_todo_django50.txt | 7 ------- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/django-stubs/forms/boundfield.pyi b/django-stubs/forms/boundfield.pyi index e3842b9da..e24c29fd8 100644 --- a/django-stubs/forms/boundfield.pyi +++ b/django-stubs/forms/boundfield.pyi @@ -4,7 +4,7 @@ from typing import Any, overload from django.forms.fields import Field from django.forms.forms import BaseForm from django.forms.renderers import BaseRenderer -from django.forms.utils import ErrorList +from django.forms.utils import ErrorList, RenderableFieldMixin from django.forms.widgets import Widget from django.utils.functional import _StrOrPromise, cached_property from django.utils.safestring import SafeString @@ -12,7 +12,7 @@ from typing_extensions import TypeAlias _AttrsT: TypeAlias = dict[str, str | bool] -class BoundField: +class BoundField(RenderableFieldMixin): form: BaseForm field: Field name: str @@ -21,6 +21,7 @@ class BoundField: html_initial_id: str label: _StrOrPromise help_text: _StrOrPromise + renderer: BaseRenderer def __init__(self, form: BaseForm, field: Field, name: str) -> None: ... @cached_property def subwidgets(self) -> list[BoundWidget]: ... @@ -33,6 +34,8 @@ class BoundField: def __getitem__(self, idx: slice) -> list[BoundWidget]: ... @property def errors(self) -> ErrorList: ... + @property + def template_name(self) -> str: ... def as_widget( self, widget: Widget | None = ..., attrs: _AttrsT | None = ..., only_initial: bool = ... ) -> SafeString: ... diff --git a/django-stubs/forms/utils.pyi b/django-stubs/forms/utils.pyi index 6533e7b0c..a2a5c4653 100644 --- a/django-stubs/forms/utils.pyi +++ b/django-stubs/forms/utils.pyi @@ -26,8 +26,15 @@ class RenderableMixin: context: dict[str, Any] | None = ..., renderer: BaseRenderer | type[BaseRenderer] | None = ..., ) -> SafeString: ... - __str__ = render - __html__ = render + # This is a lie, but this is how it is supposed to be used, + # in reallity it is `__str__ = __html__ = render`: + def __str__(self) -> SafeString: ... + def __html__(self) -> SafeString: ... + +class RenderableFieldMixin(RenderableMixin): + def as_field_group(self) -> SafeString: ... + def as_hidden(self) -> SafeString: ... + def as_widget(self) -> SafeString: ... class RenderableFormMixin(RenderableMixin): def as_p(self) -> SafeString: ... diff --git a/scripts/stubtest/allowlist.txt b/scripts/stubtest/allowlist.txt index ff300149e..34b39e6a4 100644 --- a/scripts/stubtest/allowlist.txt +++ b/scripts/stubtest/allowlist.txt @@ -402,3 +402,7 @@ django.contrib.auth.models.AbstractUser.Meta django.contrib.auth.models.PermissionsMixin.Meta django.contrib.flatpages.forms.FlatpageForm.Meta django.contrib.sessions.base_session.AbstractBaseSession.Meta + +# Custom __str__ that we don't want to overcomplicate: +django.forms.utils.RenderableMixin.__str__ +django.forms.utils.RenderableMixin.__html__ diff --git a/scripts/stubtest/allowlist_todo_django50.txt b/scripts/stubtest/allowlist_todo_django50.txt index de4288373..0cb18fa3d 100644 --- a/scripts/stubtest/allowlist_todo_django50.txt +++ b/scripts/stubtest/allowlist_todo_django50.txt @@ -23,8 +23,6 @@ django.contrib.gis.db.models.Prefetch.get_current_querysets django.contrib.gis.db.models.Q.identity django.contrib.gis.db.models.When.allowed_default django.contrib.gis.forms.BaseForm._html_output -django.contrib.gis.forms.BoundField.get_context -django.contrib.gis.forms.BoundField.template_name django.contrib.gis.forms.ClearableFileInput.checked django.contrib.gis.forms.fields_for_model django.contrib.gis.geos.prototypes.io.DEFAULT_TRIM_VALUE @@ -93,16 +91,11 @@ django.db.models.sql.query.Query.join django.db.models.sql.query.Query.resolve_lookup_value django.db.models.sql.query.Query.setup_joins django.forms.BaseForm._html_output -django.forms.BoundField.get_context -django.forms.BoundField.template_name django.forms.ClearableFileInput.checked -django.forms.boundfield.BoundField.get_context -django.forms.boundfield.BoundField.template_name django.forms.fields_for_model django.forms.forms.BaseForm._html_output django.forms.models.fields_for_model django.forms.renderers.BaseRenderer.field_template_name django.forms.renderers.Jinja2DivFormRenderer.__init__ -django.forms.utils.RenderableFieldMixin django.forms.widgets.ClearableFileInput.checked django.template.autoreload