From eb6991b008e7afd2b191d19ca4c574661d99eb93 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Wed, 2 Mar 2022 15:02:54 +0000 Subject: [PATCH] Improve forms stubs (#862) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * `default_renderer` may be a renderer class, instance, or `None`: https://docs.djangoproject.com/en/4.0/ref/forms/api/#django.forms.Form.default_renderer * `field_order` should be a list/sequence of field names, or `None`: https://docs.djangoproject.com/en/4.0/ref/forms/api/#django.forms.Form.field_order * `cleaned_data` should be a dict/mapping of field names to values, as returned by `clean()`: https://docs.djangoproject.com/en/4.0/ref/forms/api/#django.forms.Form.cleaned_data . I similarly updated `clean()` to allow arbitrary mappings, since it may be legitimate to use some special mapping class. * `clean()` does not need to return a mapping - https://docs.djangoproject.com/en/4.0/ref/forms/validation/#validating-fields-with-clean : “If your form inherits another that doesn’t return a `cleaned_data` dictionary in its `clean()` method (doing so is optional)...” * `is_multipart` returns a `bool`: https://docs.djangoproject.com/en/4.0/ref/forms/api/#django.forms.Form.is_multipart * `hidden_fields` and `visible_fields` aren't documented, but they return lists of `BoundField` instances: https://github.com/django/django/blob/d8b437b1fbe3bf54822833bea5e19d2142cf3e1f/django/forms/forms.py#L501-L513 --- django-stubs/forms/forms.pyi | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/django-stubs/forms/forms.pyi b/django-stubs/forms/forms.pyi index dcb140c6c..ff0838043 100644 --- a/django-stubs/forms/forms.pyi +++ b/django-stubs/forms/forms.pyi @@ -15,8 +15,8 @@ class DeclarativeFieldsMetaclass(MediaDefiningClass): ... class BaseForm: class Meta: fields: Sequence[str] = ... - default_renderer: Any = ... - field_order: Any = ... + default_renderer: Union[None, BaseRenderer, type[BaseRenderer]] = ... + field_order: Optional[Sequence[str]] = ... use_required_attribute: bool = ... is_bound: bool = ... data: Dict[str, Any] = ... @@ -29,7 +29,7 @@ class BaseForm: empty_permitted: bool = ... fields: Dict[str, Any] = ... renderer: BaseRenderer = ... - cleaned_data: Any = ... + cleaned_data: Mapping[str, Any] = ... def __init__( self, data: Optional[Mapping[str, Any]] = ..., @@ -59,15 +59,15 @@ class BaseForm: def add_error(self, field: Optional[str], error: Union[ValidationError, str]) -> None: ... def has_error(self, field: Any, code: Optional[Any] = ...): ... def full_clean(self) -> None: ... - def clean(self) -> Dict[str, Any]: ... + def clean(self) -> Optional[Mapping[str, Any]]: ... def has_changed(self) -> bool: ... @property def changed_data(self) -> List[str]: ... @property def media(self) -> Media: ... - def is_multipart(self): ... - def hidden_fields(self): ... - def visible_fields(self): ... + 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,