Skip to content

Commit

Permalink
Improve django.forms (#221)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
noelleleigh authored Jan 3, 2024
1 parent 5896169 commit 048d79e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
4 changes: 3 additions & 1 deletion django-stubs/forms/boundfield.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections.abc import Iterable
from typing import Any

from django.forms.fields import Field
Expand All @@ -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
Expand Down
22 changes: 10 additions & 12 deletions django-stubs/forms/forms.pyi
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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] = ...
Expand All @@ -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 = ...,
Expand All @@ -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]: ...
Expand All @@ -60,17 +58,17 @@ 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: ...
@property
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,
Expand Down
1 change: 0 additions & 1 deletion django-stubs/forms/models.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ class ModelChoiceField(ChoiceField):

class ModelMultipleChoiceField(ModelChoiceField):
disabled: bool
empty_label: None
help_text: str
required: bool
show_hidden_initial: bool
Expand Down

0 comments on commit 048d79e

Please sign in to comment.