From 048d79e2e2b5056abe080142418f999fa9245cc1 Mon Sep 17 00:00:00 2001 From: Noelle Leigh <5957867+noelleleigh@users.noreply.github.com> Date: Wed, 3 Jan 2024 15:04:03 -0500 Subject: [PATCH] Improve django.forms (#221) A collection of small improvements to the `django.forms.forms` and `django.forms.boundfield` modules. The most impactful change is declaring `BaseForm.cleaned_data` to be a `dict`. The other changes are described in the commit messages. --- django-stubs/forms/boundfield.pyi | 4 +++- django-stubs/forms/forms.pyi | 22 ++++++++++------------ django-stubs/forms/models.pyi | 1 - 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/django-stubs/forms/boundfield.pyi b/django-stubs/forms/boundfield.pyi index 9e2993101..25704caab 100644 --- a/django-stubs/forms/boundfield.pyi +++ b/django-stubs/forms/boundfield.pyi @@ -1,3 +1,4 @@ +from collections.abc import Iterable from typing import Any from django.forms.fields import Field @@ -18,9 +19,10 @@ class BoundField: label: str = ... help_text: str = ... def __init__(self, form: BaseForm, field: Field, name: str) -> None: ... + @property def subwidgets(self) -> list[BoundWidget]: ... def __bool__(self) -> bool: ... - def __iter__(self) -> Any: ... + def __iter__(self) -> Iterable[BoundWidget]: ... def __len__(self) -> int: ... def __getitem__( self, idx: int | slice | str diff --git a/django-stubs/forms/forms.pyi b/django-stubs/forms/forms.pyi index d13472c03..ca387f908 100644 --- a/django-stubs/forms/forms.pyi +++ b/django-stubs/forms/forms.pyi @@ -1,4 +1,4 @@ -from collections.abc import Iterator, Mapping, Sequence +from collections.abc import Iterator, Mapping from typing import Any from django.core.exceptions import ValidationError as ValidationError @@ -15,11 +15,9 @@ from django.utils.safestring import SafeText class DeclarativeFieldsMetaclass(MediaDefiningClass): ... class BaseForm: - class Meta: - fields: Sequence[str] = ... _meta: Options[Any] - default_renderer: Any = ... - field_order: Any = ... + default_renderer: type[BaseRenderer] = ... + field_order: list[str] | None = ... use_required_attribute: bool = ... is_bound: bool = ... data: dict[str, Any] = ... @@ -32,7 +30,7 @@ class BaseForm: empty_permitted: bool = ... fields: dict[str, Any] = ... renderer: BaseRenderer = ... - cleaned_data: Any = ... + cleaned_data: dict[str, Any] = ... def __init__( self, data: Mapping[str, Any] | None = ..., @@ -43,9 +41,9 @@ class BaseForm: error_class: type[ErrorList] = ..., label_suffix: str | None = ..., empty_permitted: bool = ..., - field_order: Any | None = ..., + field_order: list[str] | None = ..., use_required_attribute: bool | None = ..., - renderer: Any = ..., + renderer: type[BaseRenderer] | None = ..., ) -> None: ... def order_fields(self, field_order: list[str] | None) -> None: ... def __iter__(self) -> Iterator[BoundField]: ... @@ -60,7 +58,7 @@ class BaseForm: def as_p(self) -> SafeText: ... def non_field_errors(self) -> ErrorList: ... def add_error(self, field: str | None, error: ValidationError | str) -> None: ... - def has_error(self, field: Any, code: Any | None = ...) -> Any: ... + def has_error(self, field: str, code: str | None = ...) -> bool: ... def full_clean(self) -> None: ... def clean(self) -> dict[str, Any]: ... def has_changed(self) -> bool: ... @@ -68,9 +66,9 @@ class BaseForm: def changed_data(self) -> list[str]: ... @property def media(self) -> Media: ... - def is_multipart(self) -> Any: ... - def hidden_fields(self) -> Any: ... - def visible_fields(self) -> Any: ... + def is_multipart(self) -> bool: ... + def hidden_fields(self) -> list[BoundField]: ... + def visible_fields(self) -> list[BoundField]: ... def get_initial_for_field(self, field: Field, field_name: str) -> Any: ... def _html_output( self, diff --git a/django-stubs/forms/models.pyi b/django-stubs/forms/models.pyi index a7ee4f6cc..2fe8863a5 100644 --- a/django-stubs/forms/models.pyi +++ b/django-stubs/forms/models.pyi @@ -280,7 +280,6 @@ class ModelChoiceField(ChoiceField): class ModelMultipleChoiceField(ModelChoiceField): disabled: bool - empty_label: None help_text: str required: bool show_hidden_initial: bool