clean up forms

This commit is contained in:
Maxim Kurnikov
2019-02-20 15:22:46 +03:00
parent 5d8cdbcf29
commit 116aa2c539
10 changed files with 344 additions and 308 deletions

View File

@@ -1,6 +1,5 @@
from typing import Any, Dict, Iterator, List, Optional, Tuple, Type, Union from typing import Any, Dict, Iterator, List, Mapping, Optional, Tuple, Union
from django.db.models.base import Model
from django.forms.utils import ErrorDict from django.forms.utils import ErrorDict
class FieldDoesNotExist(Exception): ... class FieldDoesNotExist(Exception): ...
@@ -31,20 +30,13 @@ class ValidationError(Exception):
message: Any = ... message: Any = ...
code: Any = ... code: Any = ...
params: Any = ... params: Any = ...
def __init__( def __init__(self, message: Any, code: Optional[str] = ..., params: Optional[Mapping[str, Any]] = ...) -> None: ...
self,
message: Any,
code: Optional[str] = ...,
params: Optional[
Union[Dict[str, Union[Tuple[str], Type[Model], Model, str]], Dict[str, Union[int, str]]]
] = ...,
) -> None: ...
@property @property
def message_dict(self) -> Dict[str, List[str]]: ... def message_dict(self) -> Dict[str, List[str]]: ...
@property @property
def messages(self) -> List[str]: ... def messages(self) -> List[str]: ...
def update_error_dict( def update_error_dict(
self, error_dict: Union[Dict[str, List[ValidationError]], ErrorDict] self, error_dict: Mapping[str, Any]
) -> Union[Dict[str, List[ValidationError]], ErrorDict]: ... ) -> Union[Dict[str, List[ValidationError]], ErrorDict]: ...
def __iter__(self) -> Iterator[Union[Tuple[str, List[str]], str]]: ... def __iter__(self) -> Iterator[Union[Tuple[str, List[str]], str]]: ...

View File

@@ -39,10 +39,10 @@ class InMemoryUploadedFile(UploadedFile):
charset: Optional[str], charset: Optional[str],
content_type_extra: Dict[str, str] = ..., content_type_extra: Dict[str, str] = ...,
) -> None: ... ) -> None: ...
def chunks(self, chunk_size: int = None) -> Iterator[bytes]: ... def chunks(self, chunk_size: Optional[int] = ...) -> Iterator[bytes]: ...
def multiple_chunks(self, chunk_size: int = None) -> bool: ... def multiple_chunks(self, chunk_size: Optional[int] = ...) -> bool: ...
class SimpleUploadedFile(InMemoryUploadedFile): class SimpleUploadedFile(InMemoryUploadedFile):
def __init__(self, name: str, content: bytes, content_type: str = "") -> None: ... def __init__(self, name: str, content: Optional[Union[bytes, str]], content_type: str = ...) -> None: ...
@classmethod @classmethod
def from_dict(cls: Any, file_dict: Dict[str, Union[str, bytes]]) -> None: ... def from_dict(cls: Any, file_dict: Dict[str, Union[str, bytes]]) -> None: ...

View File

@@ -1,12 +1,8 @@
import decimal from datetime import datetime, timedelta
from datetime import date, datetime, time, timedelta
from decimal import Decimal from decimal import Decimal
from typing import Any, Callable, Dict, List, Optional, Tuple, Union, Type from typing import Any, Callable, List, Optional, Pattern, Sequence, Type, Union
from uuid import UUID
from django.core.files.base import File
from django.core.validators import BaseValidator from django.core.validators import BaseValidator
from django.db.models.fields.files import FieldFile
from django.forms.boundfield import BoundField from django.forms.boundfield import BoundField
from django.forms.forms import BaseForm from django.forms.forms import BaseForm
from django.forms.widgets import Widget from django.forms.widgets import Widget
@@ -31,16 +27,16 @@ class Field:
self, self,
*, *,
required: bool = ..., required: bool = ...,
widget: Optional[Any] = ..., widget: Optional[Union[Widget, Type[Widget]]] = ...,
label: Optional[Any] = ..., label: Optional[Any] = ...,
initial: Optional[Any] = ..., initial: Optional[Any] = ...,
help_text: str = ..., help_text: str = ...,
error_messages: Optional[Any] = ..., error_messages: Optional[Any] = ...,
show_hidden_initial: bool = ..., show_hidden_initial: bool = ...,
validators: Any = ..., validators: Sequence[Any] = ...,
localize: bool = ..., localize: bool = ...,
disabled: bool = ..., disabled: bool = ...,
label_suffix: Optional[Any] = ... label_suffix: Optional[Any] = ...,
) -> None: ... ) -> None: ...
def prepare_value(self, value: Any) -> Any: ... def prepare_value(self, value: Any) -> Any: ...
def to_python(self, value: Optional[Any]) -> Optional[Any]: ... def to_python(self, value: Optional[Any]) -> Optional[Any]: ...
@@ -49,60 +45,60 @@ class Field:
def clean(self, value: Any) -> Any: ... def clean(self, value: Any) -> Any: ...
def bound_data(self, data: Any, initial: Any) -> Any: ... def bound_data(self, data: Any, initial: Any) -> Any: ...
def widget_attrs(self, widget: Widget) -> Any: ... def widget_attrs(self, widget: Widget) -> Any: ...
def has_changed(self, initial: Any, data: Optional[str]) -> bool: ... def has_changed(self, initial: Any, data: Any) -> bool: ...
def get_bound_field(self, form: BaseForm, field_name: str) -> BoundField: ... def get_bound_field(self, form: BaseForm, field_name: str) -> BoundField: ...
def __deepcopy__(self, memo: Dict[Any, Any]) -> Field: ...
class CharField(Field): class CharField(Field):
disabled: bool
error_messages: Dict[str, str]
required: bool
show_hidden_initial: bool
max_length: Optional[Union[int, str]] = ... max_length: Optional[Union[int, str]] = ...
min_length: Optional[Union[int, str]] = ... min_length: Optional[Union[int, str]] = ...
strip: bool = ... strip: bool = ...
empty_value: Optional[str] = ... empty_value: Optional[str] = ...
def __init__( def __init__(
self, self,
*,
max_length: Optional[Any] = ..., max_length: Optional[Any] = ...,
min_length: Optional[Any] = ..., min_length: Optional[Any] = ...,
strip: bool = ..., strip: bool = ...,
empty_value: str = ..., empty_value: Optional[str] = ...,
**kwargs: Any required: bool = ...,
widget: Optional[Union[Widget, Type[Widget]]] = ...,
label: Optional[Any] = ...,
initial: Optional[Any] = ...,
help_text: str = ...,
error_messages: Optional[Any] = ...,
show_hidden_initial: bool = ...,
validators: Sequence[Any] = ...,
localize: bool = ...,
disabled: bool = ...,
label_suffix: Optional[Any] = ...,
) -> None: ... ) -> None: ...
class IntegerField(Field): class IntegerField(Field):
disabled: bool
error_messages: Dict[str, str]
max_value: Optional[Any] max_value: Optional[Any]
min_value: Optional[Any] min_value: Optional[Any]
required: bool
show_hidden_initial: bool
default_error_messages: Any = ...
re_decimal: Any = ... re_decimal: Any = ...
def __init__(self, *, max_value: Optional[Any] = ..., min_value: Optional[Any] = ..., **kwargs: Any) -> None: ... def __init__(
self,
max_value: Optional[Any] = ...,
min_value: Optional[Any] = ...,
required: bool = ...,
widget: Optional[Union[Widget, Type[Widget]]] = ...,
label: Optional[Any] = ...,
initial: Optional[Any] = ...,
help_text: str = ...,
error_messages: Optional[Any] = ...,
show_hidden_initial: bool = ...,
validators: Sequence[Any] = ...,
localize: bool = ...,
disabled: bool = ...,
label_suffix: Optional[Any] = ...,
) -> None: ...
class FloatField(IntegerField): class FloatField(IntegerField):
disabled: bool
error_messages: Dict[str, str]
max_value: Optional[float]
min_value: Optional[float]
required: bool
show_hidden_initial: bool
default_error_messages: Any = ...
def validate(self, value: Optional[float]) -> None: ... def validate(self, value: Optional[float]) -> None: ...
class DecimalField(IntegerField): class DecimalField(IntegerField):
decimal_places: Optional[int] decimal_places: Optional[int]
disabled: bool
error_messages: Dict[str, str]
max_digits: Optional[int] max_digits: Optional[int]
max_value: Optional[Union[decimal.Decimal, int]]
min_value: Optional[Union[decimal.Decimal, int]]
required: bool
show_hidden_initial: bool
default_error_messages: Any = ...
def __init__( def __init__(
self, self,
*, *,
@@ -110,118 +106,95 @@ class DecimalField(IntegerField):
min_value: Optional[Any] = ..., min_value: Optional[Any] = ...,
max_digits: Optional[Any] = ..., max_digits: Optional[Any] = ...,
decimal_places: Optional[Any] = ..., decimal_places: Optional[Any] = ...,
**kwargs: Any required: bool = ...,
widget: Optional[Union[Widget, Type[Widget]]] = ...,
label: Optional[Any] = ...,
initial: Optional[Any] = ...,
help_text: str = ...,
error_messages: Optional[Any] = ...,
show_hidden_initial: bool = ...,
validators: Sequence[Any] = ...,
localize: bool = ...,
disabled: bool = ...,
label_suffix: Optional[Any] = ...,
) -> None: ... ) -> None: ...
def validate(self, value: Optional[Decimal]) -> None: ... def validate(self, value: Optional[Decimal]) -> None: ...
class BaseTemporalField(Field): class BaseTemporalField(Field):
input_formats: Any = ... input_formats: Any = ...
def __init__(self, *, input_formats: Optional[Any] = ..., **kwargs: Any) -> None: ... def __init__(
def strptime(self, value: Any, format: Any) -> Any: ... self,
input_formats: Optional[Any] = ...,
required: bool = ...,
widget: Optional[Union[Widget, Type[Widget]]] = ...,
label: Optional[Any] = ...,
initial: Optional[Any] = ...,
help_text: str = ...,
error_messages: Optional[Any] = ...,
show_hidden_initial: bool = ...,
validators: Sequence[Any] = ...,
localize: bool = ...,
disabled: bool = ...,
label_suffix: Optional[Any] = ...,
) -> None: ...
def strptime(self, value: Any, format: str) -> Any: ...
class DateField(BaseTemporalField): class DateField(BaseTemporalField): ...
disabled: bool class TimeField(BaseTemporalField): ...
error_messages: Dict[str, str] class DateTimeField(BaseTemporalField): ...
required: bool
show_hidden_initial: bool
input_formats: Any = ...
default_error_messages: Any = ...
def strptime(self, value: str, format: str) -> date: ...
class TimeField(BaseTemporalField):
disabled: bool
error_messages: Dict[str, str]
required: bool
show_hidden_initial: bool
input_formats: Any = ...
default_error_messages: Any = ...
def strptime(self, value: str, format: str) -> time: ...
class DateTimeField(BaseTemporalField):
disabled: bool
error_messages: Dict[str, str]
required: bool
show_hidden_initial: bool
input_formats: Any = ...
default_error_messages: Any = ...
def prepare_value(self, value: Optional[datetime]) -> Optional[datetime]: ...
def strptime(self, value: str, format: str) -> datetime: ...
class DurationField(Field): class DurationField(Field):
disabled: bool
required: bool
show_hidden_initial: bool
default_error_messages: Any = ...
def prepare_value(self, value: Optional[Union[timedelta, str]]) -> Optional[str]: ... def prepare_value(self, value: Optional[Union[timedelta, str]]) -> Optional[str]: ...
class RegexField(CharField): class RegexField(CharField):
disabled: bool regex: str = ...
empty_value: str def __init__(
error_messages: Dict[str, str] self,
max_length: Optional[int] regex: Union[str, Pattern],
min_length: Optional[int] max_length: Optional[Any] = ...,
required: bool min_length: Optional[Any] = ...,
show_hidden_initial: bool strip: bool = ...,
strip: bool empty_value: Optional[str] = ...,
def __init__(self, regex: str, **kwargs: Any) -> None: ... required: bool = ...,
regex: Any = ... widget: Optional[Union[Widget, Type[Widget]]] = ...,
label: Optional[Any] = ...,
initial: Optional[Any] = ...,
help_text: str = ...,
error_messages: Optional[Any] = ...,
show_hidden_initial: bool = ...,
validators: Sequence[Any] = ...,
localize: bool = ...,
disabled: bool = ...,
label_suffix: Optional[Any] = ...,
) -> None: ...
class EmailField(CharField): class EmailField(CharField): ...
disabled: bool
empty_value: Optional[str]
error_messages: Dict[str, str]
max_length: Optional[int]
min_length: Optional[int]
required: bool
show_hidden_initial: bool
strip: bool
default_validators: Any = ...
def __init__(self, **kwargs: Any) -> None: ...
class FileField(Field): class FileField(Field):
disabled: bool
required: bool
show_hidden_initial: bool
default_error_messages: Any = ...
max_length: Optional[int] = ... max_length: Optional[int] = ...
allow_empty_file: bool = ... allow_empty_file: bool = ...
def __init__(self, *, max_length: Optional[Any] = ..., allow_empty_file: bool = ..., **kwargs: Any) -> None: ... def __init__(
def bound_data(self, data: Any, initial: Optional[FieldFile]) -> Optional[Union[File, str]]: ... self,
max_length: Optional[Any] = ...,
allow_empty_file: bool = ...,
required: bool = ...,
widget: Optional[Union[Widget, Type[Widget]]] = ...,
label: Optional[Any] = ...,
initial: Optional[Any] = ...,
help_text: str = ...,
error_messages: Optional[Any] = ...,
show_hidden_initial: bool = ...,
validators: Sequence[Any] = ...,
localize: bool = ...,
disabled: bool = ...,
label_suffix: Optional[Any] = ...,
) -> None: ...
def clean(self, data: Any, initial: Optional[Any] = ...): ...
class ImageField(FileField): class ImageField(FileField): ...
allow_empty_file: bool class URLField(CharField): ...
disabled: bool class BooleanField(Field): ...
max_length: Optional[int] class NullBooleanField(BooleanField): ...
required: bool
show_hidden_initial: bool
default_validators: Any = ...
default_error_messages: Any = ...
class URLField(CharField):
disabled: bool
empty_value: Optional[str]
error_messages: Dict[str, str]
max_length: Optional[int]
min_length: Optional[int]
required: bool
show_hidden_initial: bool
strip: bool
default_error_messages: Any = ...
default_validators: Any = ...
def __init__(self, **kwargs: Any) -> None: ...
class BooleanField(Field):
disabled: bool
error_messages: Dict[str, str]
required: bool
show_hidden_initial: bool
def validate(self, value: bool) -> None: ...
class NullBooleanField(BooleanField):
disabled: bool
required: bool
show_hidden_initial: bool
def validate(self, value: Optional[bool]) -> None: ...
class CallableChoiceIterator: class CallableChoiceIterator:
choices_func: Callable = ... choices_func: Callable = ...
@@ -229,125 +202,191 @@ class CallableChoiceIterator:
def __iter__(self) -> None: ... def __iter__(self) -> None: ...
class ChoiceField(Field): class ChoiceField(Field):
disabled: bool
error_messages: Dict[str, str]
required: bool
show_hidden_initial: bool
default_error_messages: Any = ...
choices: Any = ... choices: Any = ...
def __init__(self, *, choices: Any = ..., **kwargs: Any) -> None: ... def __init__(
def validate(self, value: Any) -> None: ... self,
choices: Any = ...,
required: bool = ...,
widget: Optional[Union[Widget, Type[Widget]]] = ...,
label: Optional[Any] = ...,
initial: Optional[Any] = ...,
help_text: str = ...,
error_messages: Optional[Any] = ...,
show_hidden_initial: bool = ...,
validators: Sequence[Any] = ...,
localize: bool = ...,
disabled: bool = ...,
label_suffix: Optional[Any] = ...,
) -> None: ...
def valid_value(self, value: str) -> bool: ... def valid_value(self, value: str) -> bool: ...
class TypedChoiceField(ChoiceField): class TypedChoiceField(ChoiceField):
disabled: bool coerce: Union[Callable, Type[Any]] = ...
required: bool
show_hidden_initial: bool
coerce: Union[Callable, Type[Union[bool, float, str]]] = ...
empty_value: Optional[str] = ... empty_value: Optional[str] = ...
def __init__(self, *, coerce: Any = ..., empty_value: str = ..., **kwargs: Any) -> None: ... def __init__(
self,
coerce: Any = ...,
empty_value: Optional[str] = ...,
choices: Any = ...,
required: bool = ...,
widget: Optional[Union[Widget, Type[Widget]]] = ...,
label: Optional[Any] = ...,
initial: Optional[Any] = ...,
help_text: str = ...,
error_messages: Optional[Any] = ...,
show_hidden_initial: bool = ...,
validators: Sequence[Any] = ...,
localize: bool = ...,
disabled: bool = ...,
label_suffix: Optional[Any] = ...,
) -> None: ...
class MultipleChoiceField(ChoiceField): class MultipleChoiceField(ChoiceField): ...
disabled: bool
error_messages: Dict[str, str]
required: bool
show_hidden_initial: bool
hidden_widget: Any = ...
default_error_messages: Any = ...
def validate(self, value: List[str]) -> None: ...
class TypedMultipleChoiceField(MultipleChoiceField): class TypedMultipleChoiceField(MultipleChoiceField):
disabled: bool
required: bool
show_hidden_initial: bool
coerce: Union[Callable, Type[float]] = ... coerce: Union[Callable, Type[float]] = ...
empty_value: Optional[List[Any]] = ... empty_value: Optional[List[Any]] = ...
def __init__(self, *, coerce: Any = ..., **kwargs: Any) -> None: ... def __init__(
def validate(self, value: List[str]) -> None: ... self,
coerce: Any = ...,
empty_value: Optional[str] = ...,
choices: Any = ...,
required: bool = ...,
widget: Optional[Union[Widget, Type[Widget]]] = ...,
label: Optional[Any] = ...,
initial: Optional[Any] = ...,
help_text: str = ...,
error_messages: Optional[Any] = ...,
show_hidden_initial: bool = ...,
validators: Sequence[Any] = ...,
localize: bool = ...,
disabled: bool = ...,
label_suffix: Optional[Any] = ...,
) -> None: ...
class ComboField(Field): class ComboField(Field):
disabled: bool
required: bool
show_hidden_initial: bool
fields: Any = ... fields: Any = ...
def __init__(self, fields: List[CharField], **kwargs: Any) -> None: ... def __init__(
self,
fields: Sequence[Field],
required: bool = ...,
widget: Optional[Union[Widget, Type[Widget]]] = ...,
label: Optional[Any] = ...,
initial: Optional[Any] = ...,
help_text: str = ...,
error_messages: Optional[Any] = ...,
show_hidden_initial: bool = ...,
validators: Sequence[Any] = ...,
localize: bool = ...,
disabled: bool = ...,
label_suffix: Optional[Any] = ...,
) -> None: ...
class MultiValueField(Field): class MultiValueField(Field):
disabled: bool
required: bool
show_hidden_initial: bool
default_error_messages: Any = ...
require_all_fields: bool = ... require_all_fields: bool = ...
fields: Any = ... fields: Any = ...
def __init__(self, fields: Tuple[Field, Field], *, require_all_fields: bool = ..., **kwargs: Any) -> None: ... def __init__(
def validate(self, value: Union[datetime, str]) -> None: ... self,
fields: Sequence[Field],
require_all_fields: bool = ...,
required: bool = ...,
widget: Optional[Union[Widget, Type[Widget]]] = ...,
label: Optional[Any] = ...,
initial: Optional[Any] = ...,
help_text: str = ...,
error_messages: Optional[Any] = ...,
show_hidden_initial: bool = ...,
validators: Sequence[Any] = ...,
localize: bool = ...,
disabled: bool = ...,
label_suffix: Optional[Any] = ...,
) -> None: ...
def compress(self, data_list: Any) -> Any: ... def compress(self, data_list: Any) -> Any: ...
class FilePathField(ChoiceField): class FilePathField(ChoiceField):
allow_files: bool allow_files: bool
allow_folders: bool allow_folders: bool
disabled: bool
match: Optional[str] match: Optional[str]
path: str path: str
recursive: bool recursive: bool
required: bool
show_hidden_initial: bool
choices: Any = ...
match_re: Any = ... match_re: Any = ...
def __init__( def __init__(
self, self,
path: str, path: str,
*,
match: Optional[Any] = ..., match: Optional[Any] = ...,
recursive: bool = ..., recursive: bool = ...,
allow_files: bool = ..., allow_files: bool = ...,
allow_folders: bool = ..., allow_folders: bool = ...,
**kwargs: Any choices: Any = ...,
required: bool = ...,
widget: Optional[Union[Widget, Type[Widget]]] = ...,
label: Optional[Any] = ...,
initial: Optional[Any] = ...,
help_text: str = ...,
error_messages: Optional[Any] = ...,
show_hidden_initial: bool = ...,
validators: Sequence[Any] = ...,
localize: bool = ...,
disabled: bool = ...,
label_suffix: Optional[Any] = ...,
) -> None: ... ) -> None: ...
class SplitDateTimeField(MultiValueField): class SplitDateTimeField(MultiValueField):
disabled: bool
require_all_fields: bool
required: bool
show_hidden_initial: bool
hidden_widget: Any = ...
default_error_messages: Any = ...
def __init__( def __init__(
self, *, input_date_formats: Optional[Any] = ..., input_time_formats: Optional[Any] = ..., **kwargs: Any self,
input_date_formats: Optional[Any] = ...,
input_time_formats: Optional[Any] = ...,
fields: Sequence[Field] = ...,
require_all_fields: bool = ...,
required: bool = ...,
widget: Optional[Union[Widget, Type[Widget]]] = ...,
label: Optional[Any] = ...,
initial: Optional[Any] = ...,
help_text: str = ...,
error_messages: Optional[Any] = ...,
show_hidden_initial: bool = ...,
validators: Sequence[Any] = ...,
localize: bool = ...,
disabled: bool = ...,
label_suffix: Optional[Any] = ...,
) -> None: ... ) -> None: ...
def compress(self, data_list: List[Optional[datetime]]) -> Optional[datetime]: ... def compress(self, data_list: List[Optional[datetime]]) -> Optional[datetime]: ...
class GenericIPAddressField(CharField): class GenericIPAddressField(CharField):
disabled: bool
empty_value: str
error_messages: Dict[str, str]
max_length: None
min_length: None
required: bool
show_hidden_initial: bool
strip: bool
unpack_ipv4: bool = ... unpack_ipv4: bool = ...
default_validators: List[Callable] = ... def __init__(
def __init__(self, *, protocol: str = ..., unpack_ipv4: bool = ..., **kwargs: Any) -> None: ... self,
protocol: str = ...,
unpack_ipv4: bool = ...,
required: bool = ...,
widget: Optional[Union[Widget, Type[Widget]]] = ...,
label: Optional[Any] = ...,
initial: Optional[Any] = ...,
help_text: str = ...,
error_messages: Optional[Any] = ...,
show_hidden_initial: bool = ...,
validators: Sequence[Any] = ...,
localize: bool = ...,
disabled: bool = ...,
label_suffix: Optional[Any] = ...,
) -> None: ...
class SlugField(CharField): class SlugField(CharField):
disabled: bool
empty_value: str
max_length: Optional[int]
min_length: None
required: bool
show_hidden_initial: bool
strip: bool
allow_unicode: bool = ... allow_unicode: bool = ...
def __init__(self, *, allow_unicode: bool = ..., **kwargs: Any) -> None: ... def __init__(
self,
allow_unicode: bool = ...,
required: bool = ...,
widget: Optional[Union[Widget, Type[Widget]]] = ...,
label: Optional[Any] = ...,
initial: Optional[Any] = ...,
help_text: str = ...,
error_messages: Optional[Any] = ...,
show_hidden_initial: bool = ...,
validators: Sequence[Any] = ...,
localize: bool = ...,
disabled: bool = ...,
label_suffix: Optional[Any] = ...,
) -> None: ...
class UUIDField(CharField): class UUIDField(CharField): ...
disabled: bool
empty_value: str
max_length: None
min_length: None
required: bool
show_hidden_initial: bool
strip: bool
default_error_messages: Any = ...
def prepare_value(self, value: UUID) -> str: ...

View File

@@ -1,48 +1,45 @@
from collections import OrderedDict
from datetime import datetime from datetime import datetime
from typing import Any, Dict, Iterator, List, Mapping, Optional, Tuple, Type, Union from typing import Any, Dict, Iterator, List, Mapping, Optional, Sequence, Type, Union
from django.core.exceptions import ValidationError as ValidationError from django.core.exceptions import ValidationError as ValidationError
from django.core.files.base import File
from django.core.files.uploadedfile import SimpleUploadedFile from django.core.files.uploadedfile import SimpleUploadedFile
from django.db.models.query import QuerySet from django.db.models.query import QuerySet
from django.forms.boundfield import BoundField from django.forms.boundfield import BoundField
from django.forms.fields import Field from django.forms.fields import Field
from django.forms.renderers import BaseRenderer
from django.forms.utils import ErrorDict, ErrorList from django.forms.utils import ErrorDict, ErrorList
from django.forms.widgets import Media, MediaDefiningClass from django.forms.widgets import Media, MediaDefiningClass
from django.utils.safestring import SafeText from django.utils.safestring import SafeText
class DeclarativeFieldsMetaclass(MediaDefiningClass): class DeclarativeFieldsMetaclass(MediaDefiningClass):
def __new__( def __new__(mcs, name: str, bases: Sequence[Type[BaseForm]], attrs: Dict[str, Any]) -> Type[BaseForm]: ...
mcs: Type[DeclarativeFieldsMetaclass], name: str, bases: Tuple[Type[BaseForm]], attrs: OrderedDict
) -> Type[BaseForm]: ...
class BaseForm: class BaseForm:
default_renderer: Any = ... default_renderer: Any = ...
field_order: Any = ... field_order: Any = ...
prefix: Any = ...
use_required_attribute: bool = ... use_required_attribute: bool = ...
is_bound: Any = ... is_bound: bool = ...
data: Any = ... data: Dict[str, Any] = ...
files: Any = ... files: Optional[Dict[str, Any]] = ...
auto_id: Any = ... auto_id: Any = ...
initial: Any = ... initial: Dict[str, Any] = ...
error_class: Any = ... error_class: Type[ErrorList] = ...
label_suffix: Any = ... prefix: str = ...
empty_permitted: Any = ... label_suffix: str = ...
fields: Any = ... empty_permitted: bool = ...
renderer: Any = ... fields: Dict[str, Any] = ...
renderer: BaseRenderer = ...
def __init__( def __init__(
self, self,
data: Optional[Mapping[str, Any]] = ..., data: Optional[Mapping[str, Any]] = ...,
files: Optional[Mapping[str, File]] = ..., files: Optional[Mapping[str, Any]] = ...,
auto_id: Optional[Union[bool, str]] = ..., auto_id: Optional[Union[bool, str]] = ...,
prefix: Optional[str] = ..., prefix: Optional[str] = ...,
initial: Optional[Mapping[str, Any]] = ..., initial: Optional[Mapping[str, Any]] = ...,
error_class: Type[ErrorList] = ..., error_class: Type[ErrorList] = ...,
label_suffix: None = ..., label_suffix: Optional[str] = ...,
empty_permitted: bool = ..., empty_permitted: bool = ...,
field_order: None = ..., field_order: Optional[Any] = ...,
use_required_attribute: Optional[bool] = ..., use_required_attribute: Optional[bool] = ...,
renderer: Any = ..., renderer: Any = ...,
) -> None: ... ) -> None: ...

View File

@@ -1,8 +1,4 @@
import collections from typing import Any, Dict, Mapping, Optional, Sequence, Sized
from typing import Any, List, Optional, Union, Dict, Type
from django.forms.renderers import BaseRenderer
from django.forms.utils import ErrorList
from django.forms import Form from django.forms import Form
@@ -17,21 +13,9 @@ DEFAULT_MIN_NUM: int = ...
DEFAULT_MAX_NUM: int = ... DEFAULT_MAX_NUM: int = ...
class ManagementForm(Form): class ManagementForm(Form):
auto_id: Union[bool, str]
cleaned_data: Dict[str, Optional[int]] cleaned_data: Dict[str, Optional[int]]
data: Dict[str, Union[List[int], int, str]]
empty_permitted: bool
error_class: Type[ErrorList]
fields: collections.OrderedDict
files: Dict[Any, Any]
initial: Dict[str, int]
is_bound: bool
label_suffix: str
prefix: str
renderer: BaseRenderer
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
class BaseFormSet: class BaseFormSet(Sized, Mapping[str, Any]):
is_bound: Any = ... is_bound: Any = ...
prefix: Any = ... prefix: Any = ...
auto_id: Any = ... auto_id: Any = ...
@@ -101,4 +85,4 @@ def formset_factory(
min_num: Optional[Any] = ..., min_num: Optional[Any] = ...,
validate_min: bool = ..., validate_min: bool = ...,
): ... ): ...
def all_valid(formsets: List[Any]) -> bool: ... def all_valid(formsets: Sequence[Any]) -> bool: ...

View File

@@ -1,10 +1,12 @@
from collections import OrderedDict from collections import OrderedDict
from datetime import date, datetime from datetime import date, datetime
from typing import Any, Callable, Dict, Iterator, List, Optional, Tuple, Type, Union, Sequence from typing import Any, Callable, Dict, Iterator, List, Optional, Tuple, Type, Union, Sequence, MutableMapping
from unittest.mock import MagicMock from unittest.mock import MagicMock
from uuid import UUID from uuid import UUID
from django.core.files.base import File
from django.core.files.uploadedfile import SimpleUploadedFile from django.core.files.uploadedfile import SimpleUploadedFile
from django.db import models
from django.db.models import ForeignKey from django.db.models import ForeignKey
from django.db.models.base import Model from django.db.models.base import Model
from django.db.models.manager import Manager from django.db.models.manager import Manager
@@ -17,6 +19,7 @@ from django.forms.utils import ErrorList
from django.forms.widgets import Input, Widget, Select from django.forms.widgets import Input, Widget, Select
from django.http.request import QueryDict from django.http.request import QueryDict
from django.utils.datastructures import MultiValueDict from django.utils.datastructures import MultiValueDict
from typing_extensions import Literal
ALL_FIELDS: str ALL_FIELDS: str
@@ -57,21 +60,19 @@ class ModelFormOptions:
def __init__(self, options: Optional[type] = ...) -> None: ... def __init__(self, options: Optional[type] = ...) -> None: ...
class ModelFormMetaclass(DeclarativeFieldsMetaclass): class ModelFormMetaclass(DeclarativeFieldsMetaclass):
def __new__( def __new__(mcs, name: str, bases: Sequence[Type[ModelForm]], attrs: Dict[str, Any]) -> Type[ModelForm]: ...
mcs: Type[ModelFormMetaclass], name: str, bases: Tuple[Type[ModelForm]], attrs: OrderedDict
) -> Type[ModelForm]: ...
class BaseModelForm(BaseForm): class BaseModelForm(BaseForm):
instance: Any = ... instance: Any = ...
def __init__( def __init__(
self, self,
data: Optional[Union[Dict[str, Any], QueryDict]] = ..., data: Optional[Dict[str, Any]] = ...,
files: Optional[Union[Dict[str, SimpleUploadedFile], MultiValueDict]] = ..., files: Optional[Dict[str, File]] = ...,
auto_id: Union[bool, str] = ..., auto_id: Union[bool, str] = ...,
prefix: None = ..., prefix: Optional[str] = ...,
initial: Optional[Union[Dict[str, List[int]], Dict[str, int]]] = ..., initial: Optional[Dict[str, Any]] = ...,
error_class: Type[ErrorList] = ..., error_class: Type[ErrorList] = ...,
label_suffix: None = ..., label_suffix: Optional[str] = ...,
empty_permitted: bool = ..., empty_permitted: bool = ...,
instance: Optional[Model] = ..., instance: Optional[Model] = ...,
use_required_attribute: None = ..., use_required_attribute: None = ...,
@@ -87,16 +88,16 @@ class ModelForm(BaseModelForm): ...
def modelform_factory( def modelform_factory(
model: Type[Model], model: Type[Model],
form: Type[ModelForm] = ..., form: Type[ModelForm] = ...,
fields: Optional[Union[List[str], str]] = ..., fields: Optional[Union[Sequence[str], Literal["__all__"]]] = ...,
exclude: None = ..., exclude: Optional[Sequence[str]] = ...,
formfield_callback: Optional[str] = ..., formfield_callback: Optional[Union[str, Callable[[models.Field], Field]]] = ...,
widgets: None = ..., widgets: Optional[MutableMapping[str, Widget]] = ...,
localized_fields: None = ..., localized_fields: Optional[Sequence[str]] = ...,
labels: None = ..., labels: Optional[MutableMapping[str, str]] = ...,
help_texts: None = ..., help_texts: Optional[MutableMapping[str, str]] = ...,
error_messages: None = ..., error_messages: Optional[MutableMapping[str, Dict[str, Any]]] = ...,
field_classes: None = ..., field_classes: Optional[MutableMapping[str, Type[Field]]] = ...,
) -> Any: ... ) -> ModelForm: ...
class BaseModelFormSet(BaseFormSet): class BaseModelFormSet(BaseFormSet):
model: Any = ... model: Any = ...
@@ -148,13 +149,13 @@ def modelformset_factory(
exclude: Optional[Sequence[str]] = ..., exclude: Optional[Sequence[str]] = ...,
widgets: Optional[Dict[str, Any]] = ..., widgets: Optional[Dict[str, Any]] = ...,
validate_max: bool = ..., validate_max: bool = ...,
localized_fields: None = ..., localized_fields: Optional[Sequence[str]] = ...,
labels: Optional[Dict[str, str]] = ..., labels: Optional[Dict[str, str]] = ...,
help_texts: Optional[Dict[str, str]] = ..., help_texts: Optional[Dict[str, str]] = ...,
error_messages: Optional[Dict[str, Dict[str, str]]] = ..., error_messages: Optional[Dict[str, Dict[str, str]]] = ...,
validate_min: bool = ..., validate_min: bool = ...,
field_classes: Optional[Dict[str, Any]] = ..., field_classes: Optional[Dict[str, Type[Field]]] = ...,
) -> Any: ... ) -> BaseModelFormSet: ...
class BaseInlineFormSet(BaseModelFormSet): class BaseInlineFormSet(BaseModelFormSet):
instance: Any = ... instance: Any = ...
@@ -192,7 +193,7 @@ def inlineformset_factory(
formfield_callback: Optional[Callable] = ..., formfield_callback: Optional[Callable] = ...,
widgets: Optional[Dict[str, Any]] = ..., widgets: Optional[Dict[str, Any]] = ...,
validate_max: bool = ..., validate_max: bool = ...,
localized_fields: None = ..., localized_fields: Optional[Sequence[str]] = ...,
labels: Optional[Dict[str, str]] = ..., labels: Optional[Dict[str, str]] = ...,
help_texts: Optional[Dict[str, str]] = ..., help_texts: Optional[Dict[str, str]] = ...,
error_messages: Optional[Dict[str, Dict[str, str]]] = ..., error_messages: Optional[Dict[str, Dict[str, str]]] = ...,

View File

@@ -6,11 +6,11 @@ from django.core.exceptions import ValidationError
from django.utils.safestring import SafeText from django.utils.safestring import SafeText
def pretty_name(name: str) -> str: ... def pretty_name(name: str) -> str: ...
def flatatt(attrs: Dict[str, Optional[str]]) -> SafeText: ... def flatatt(attrs: Dict[str, Any]) -> SafeText: ...
class ErrorDict(dict): class ErrorDict(dict):
def as_data(self) -> Dict[str, List[ValidationError]]: ... def as_data(self) -> Dict[str, List[ValidationError]]: ...
def get_json_data(self, escape_html: bool = ...) -> Dict[str, List[Dict[str, str]]]: ... def get_json_data(self, escape_html: bool = ...) -> Dict[str, Any]: ...
def as_json(self, escape_html: bool = ...) -> str: ... def as_json(self, escape_html: bool = ...) -> str: ...
def as_ul(self) -> str: ... def as_ul(self) -> str: ...
def as_text(self) -> str: ... def as_text(self) -> str: ...
@@ -19,7 +19,9 @@ class ErrorList(UserList):
data: List[Union[ValidationError, str]] data: List[Union[ValidationError, str]]
error_class: str = ... error_class: str = ...
def __init__( def __init__(
self, initlist: Optional[Union[ErrorList, Sequence[str]]] = ..., error_class: Optional[str] = ... self,
initlist: Optional[Union[ErrorList, Sequence[Union[str, Exception]]]] = ...,
error_class: Optional[str] = ...,
) -> None: ... ) -> None: ...
def as_data(self) -> List[ValidationError]: ... def as_data(self) -> List[ValidationError]: ...
def get_json_data(self, escape_html: bool = ...) -> List[Dict[str, str]]: ... def get_json_data(self, escape_html: bool = ...) -> List[Dict[str, str]]: ...

View File

@@ -1,17 +1,16 @@
from datetime import time from datetime import time
from decimal import Decimal from decimal import Decimal
from itertools import chain from itertools import chain
from typing import Any, Callable, Dict, Iterator, List, Optional, Set, Tuple, Type, Union, Iterable, Sequence from typing import Any, Callable, Dict, Iterable, Iterator, List, Optional, Sequence, Set, Tuple, Type, Union
from django.contrib.admin.options import BaseModelAdmin
from django.core.files.base import File from django.core.files.base import File
from django.core.files.uploadedfile import SimpleUploadedFile
from django.db.models.fields.files import FieldFile from django.db.models.fields.files import FieldFile
from django.forms.forms import BaseForm
from django.forms.renderers import EngineMixin from django.forms.renderers import EngineMixin
from django.utils.datastructures import MultiValueDict from django.utils.datastructures import MultiValueDict
from django.utils.safestring import SafeText from django.utils.safestring import SafeText
_OptAttrs = Dict[str, str]
class MediaOrderConflictWarning(RuntimeWarning): ... class MediaOrderConflictWarning(RuntimeWarning): ...
class Media: class Media:
@@ -32,9 +31,7 @@ class Media:
def __add__(self, other: Media) -> Media: ... def __add__(self, other: Media) -> Media: ...
class MediaDefiningClass(type): class MediaDefiningClass(type):
def __new__( def __new__(mcs, name: str, bases: Sequence[Any], attrs: Dict[str, Any]) -> type: ...
mcs: Type[MediaDefiningClass], name: str, bases: Tuple, attrs: Any
) -> Type[Union[BaseModelAdmin, BaseForm, Widget]]: ...
class Widget: class Widget:
needs_multipart_form: bool = ... needs_multipart_form: bool = ...
@@ -61,10 +58,10 @@ class Widget:
self, base_attrs: Dict[str, Union[float, str]], extra_attrs: Optional[Dict[str, Union[bool, str]]] = ... self, base_attrs: Dict[str, Union[float, str]], extra_attrs: Optional[Dict[str, Union[bool, str]]] = ...
) -> Dict[str, Union[Decimal, float, str]]: ... ) -> Dict[str, Union[Decimal, float, str]]: ...
def value_from_datadict( def value_from_datadict(
self, data: dict, files: Union[Dict[str, SimpleUploadedFile], MultiValueDict], name: str self, data: dict, files: Union[Dict[str, Iterable[Any]], MultiValueDict], name: str
) -> Any: ... ) -> Any: ...
def value_omitted_from_data( def value_omitted_from_data(
self, data: Dict[str, Any], files: Union[Dict[str, SimpleUploadedFile], MultiValueDict], name: str self, data: Dict[str, Any], files: Union[Dict[str, Iterable[Any]], MultiValueDict], name: str
) -> bool: ... ) -> bool: ...
def id_for_label(self, id_: str) -> str: ... def id_for_label(self, id_: str) -> str: ...
def use_required_attribute(self, initial: Any) -> bool: ... def use_required_attribute(self, initial: Any) -> bool: ...
@@ -80,6 +77,7 @@ class URLInput(Input): ...
class PasswordInput(Input): class PasswordInput(Input):
render_value: bool = ... render_value: bool = ...
def __init__(self, attrs: Optional[_OptAttrs] = ..., render_value: bool = ...): ...
class HiddenInput(Input): class HiddenInput(Input):
choices: Iterable[Tuple[str, str]] choices: Iterable[Tuple[str, str]]
@@ -105,6 +103,7 @@ class DateTimeBaseInput(TextInput):
format_key: str = ... format_key: str = ...
supports_microseconds: bool = ... supports_microseconds: bool = ...
format: Optional[str] = ... format: Optional[str] = ...
def __init__(self, attrs: Optional[_OptAttrs] = ..., format: Optional[str] = ...): ...
class DateInput(DateTimeBaseInput): ... class DateInput(DateTimeBaseInput): ...
class DateTimeInput(DateTimeBaseInput): ... class DateTimeInput(DateTimeBaseInput): ...
@@ -112,9 +111,7 @@ class TimeInput(DateTimeBaseInput): ...
class CheckboxInput(Input): class CheckboxInput(Input):
check_test: Callable = ... check_test: Callable = ...
def __init__(self, attrs: Optional[Dict[str, str]] = ..., check_test: Optional[Callable] = ...) -> None: ... def __init__(self, attrs: Optional[_OptAttrs] = ..., check_test: Optional[Callable] = ...) -> None: ...
_OptAttrs = Dict[str, Any]
class ChoiceWidget(Widget): class ChoiceWidget(Widget):
allow_multiple_selected: bool = ... allow_multiple_selected: bool = ...
@@ -165,7 +162,7 @@ class CheckboxSelectMultiple(ChoiceWidget):
class MultiWidget(Widget): class MultiWidget(Widget):
template_name: str = ... template_name: str = ...
widgets: List[Widget] = ... widgets: List[Widget] = ...
def __init__(self, widgets: Sequence[Widget], attrs: Optional[_OptAttrs] = ...) -> None: ... def __init__(self, widgets: Sequence[Union[Widget, Type[Widget]]], attrs: Optional[_OptAttrs] = ...) -> None: ...
@property @property
def is_hidden(self) -> bool: ... def is_hidden(self) -> bool: ...
def decompress(self, value: Any) -> Optional[Any]: ... def decompress(self, value: Any) -> Optional[Any]: ...
@@ -209,7 +206,7 @@ class SelectDateWidget(Widget):
def __init__( def __init__(
self, self,
attrs: Optional[_OptAttrs] = ..., attrs: Optional[_OptAttrs] = ...,
years: Optional[Union[Tuple[Union[int, str]], range]] = ..., years: Optional[Iterable[Union[int, str]]] = ...,
months: None = ..., months: Optional[Dict[int, str]] = ...,
empty_label: Optional[Union[Tuple[str, str], str]] = ..., empty_label: Optional[Union[str, Sequence[str]]] = ...,
) -> None: ... ) -> None: ...

View File

@@ -1,5 +1,6 @@
from typing import Any, Callable, Dict, Optional, Sequence, Type, Union from typing import Any, Callable, Dict, Optional, Sequence, Type, Union
from django.forms.forms import BaseForm
from django.views.generic.base import ContextMixin, TemplateResponseMixin, View from django.views.generic.base import ContextMixin, TemplateResponseMixin, View
from django.views.generic.detail import BaseDetailView, SingleObjectMixin, SingleObjectTemplateResponseMixin from django.views.generic.detail import BaseDetailView, SingleObjectMixin, SingleObjectTemplateResponseMixin
from typing_extensions import Literal from typing_extensions import Literal
@@ -10,7 +11,7 @@ from django.http import HttpRequest, HttpResponse
class FormMixin(ContextMixin): class FormMixin(ContextMixin):
initial: Dict[str, Any] = ... initial: Dict[str, Any] = ...
form_class: Optional[Type[Form]] = ... form_class: Optional[Type[BaseForm]] = ...
success_url: Optional[Union[str, Callable[..., Any]]] = ... success_url: Optional[Union[str, Callable[..., Any]]] = ...
prefix: Optional[str] = ... prefix: Optional[str] = ...
request: HttpRequest = ... request: HttpRequest = ...

View File

@@ -32,7 +32,7 @@ IGNORED_ERRORS = {
'Cannot assign to a type', 'Cannot assign to a type',
re.compile(r'Cannot assign to class variable "[a-z_]+" via instance'), re.compile(r'Cannot assign to class variable "[a-z_]+" via instance'),
# forms <-> models plugin support # forms <-> models plugin support
'"Model" has no attribute', # '"Model" has no attribute',
re.compile(r'Cannot determine type of \'(objects|stuff)\''), re.compile(r'Cannot determine type of \'(objects|stuff)\''),
# settings # settings
re.compile(r'Module has no attribute "[A-Z_]+"'), re.compile(r'Module has no attribute "[A-Z_]+"'),
@@ -54,7 +54,8 @@ IGNORED_ERRORS = {
'ValuesIterable', 'ValuesIterable',
'Value of type "Optional[Dict[str, Any]]" is not indexable', 'Value of type "Optional[Dict[str, Any]]" is not indexable',
'Argument 1 to "len" has incompatible type "Optional[List[_Record]]"; expected "Sized"', 'Argument 1 to "len" has incompatible type "Optional[List[_Record]]"; expected "Sized"',
'Argument 1 to "loads" has incompatible type "Union[bytes, str, None]"; expected "Union[str, bytes, bytearray]"' 'Argument 1 to "loads" has incompatible type "Union[bytes, str, None]"; expected "Union[str, bytes, bytearray]"',
'Incompatible types in assignment (expression has type "None", variable has type Module)'
], ],
'admin_changelist': [ 'admin_changelist': [
'Incompatible types in assignment (expression has type "FilteredChildAdmin", variable has type "ChildAdmin")' 'Incompatible types in assignment (expression has type "FilteredChildAdmin", variable has type "ChildAdmin")'
@@ -166,6 +167,29 @@ IGNORED_ERRORS = {
'fixtures': [ 'fixtures': [
'Incompatible types in assignment (expression has type "int", target has type "Iterable[str]")' 'Incompatible types in assignment (expression has type "int", target has type "Iterable[str]")'
], ],
'forms_tests': [
'List item 0 has incompatible type "Jinja2"; expected "DjangoTemplates"',
'Not enough arguments for format string',
'Argument after ** must be a mapping, not "object"',
'"media" undefined in superclass',
'expression has type "None", base class "TestFormParent"',
'variable has type "SongForm"',
'"full_clean" of "BaseForm" does not return a value',
'No overload variant of "zip" matches argument types "Tuple[str, str, str]", "object"',
'note:',
'Incompatible types in assignment (expression has type "GetDateShowHiddenInitial", variable has type "GetDate")',
re.compile(r'Incompatible types in assignment \(expression has type "[a-zA-Z]+Field", '
r'base class "BaseForm" defined the type as "Dict\[str, Any\]"\)'),
'List or tuple expected as variable arguments',
'Argument 1 to "__init__" of "MultiWidget" has incompatible type "List[object]"; '
+ 'expected "Sequence[Union[Widget, Type[Widget]]]"',
'Argument 1 to "issubclass" has incompatible type "ModelFormMetaclass"; expected "type"',
'Incompatible types in assignment (expression has type "List[str]", target has type "str")',
'Incompatible types in assignment (expression has type "TestForm", variable has type "Person")',
'Incompatible types in assignment (expression has type "Type[Textarea]", '
+ 'base class "Field" defined the type as "Widget")',
'Incompatible types in assignment (expression has type "SimpleUploadedFile", variable has type "BinaryIO")'
],
'get_object_or_404': [ 'get_object_or_404': [
'Argument 1 to "get_object_or_404" has incompatible type "str"; ' 'Argument 1 to "get_object_or_404" has incompatible type "str"; '
+ 'expected "Union[Type[<nothing>], Manager[<nothing>], QuerySet[<nothing>]]"', + 'expected "Union[Type[<nothing>], Manager[<nothing>], QuerySet[<nothing>]]"',
@@ -330,7 +354,6 @@ IGNORED_ERRORS = {
'template_backends': [ 'template_backends': [
'Incompatible import of "Jinja2" (imported name has type "Type[Jinja2]", local name has type "object")', 'Incompatible import of "Jinja2" (imported name has type "Type[Jinja2]", local name has type "object")',
'TemplateStringsTests', 'TemplateStringsTests',
'Incompatible types in assignment (expression has type "None", variable has type Module)'
], ],
'urlpatterns': [ 'urlpatterns': [
'"object" has no attribute "__iter__"; maybe "__str__" or "__dir__"? (not iterable)', '"object" has no attribute "__iter__"; maybe "__str__" or "__dir__"? (not iterable)',
@@ -440,7 +463,7 @@ TESTS_DIRS = [
'flatpages_tests', 'flatpages_tests',
'force_insert_update', 'force_insert_update',
'foreign_object', 'foreign_object',
# TODO: 'forms_tests', 'forms_tests',
'from_db_value', 'from_db_value',
'generic_inline_admin', 'generic_inline_admin',
'generic_relations', 'generic_relations',