diff --git a/django-stubs/core/exceptions.pyi b/django-stubs/core/exceptions.pyi index 74060d7..44a904b 100644 --- a/django-stubs/core/exceptions.pyi +++ b/django-stubs/core/exceptions.pyi @@ -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 class FieldDoesNotExist(Exception): ... @@ -31,20 +30,13 @@ class ValidationError(Exception): message: Any = ... code: Any = ... params: Any = ... - def __init__( - self, - message: Any, - code: Optional[str] = ..., - params: Optional[ - Union[Dict[str, Union[Tuple[str], Type[Model], Model, str]], Dict[str, Union[int, str]]] - ] = ..., - ) -> None: ... + def __init__(self, message: Any, code: Optional[str] = ..., params: Optional[Mapping[str, Any]] = ...) -> None: ... @property def message_dict(self) -> Dict[str, List[str]]: ... @property def messages(self) -> List[str]: ... 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]: ... def __iter__(self) -> Iterator[Union[Tuple[str, List[str]], str]]: ... diff --git a/django-stubs/core/files/uploadedfile.pyi b/django-stubs/core/files/uploadedfile.pyi index d991898..824f8b3 100644 --- a/django-stubs/core/files/uploadedfile.pyi +++ b/django-stubs/core/files/uploadedfile.pyi @@ -39,10 +39,10 @@ class InMemoryUploadedFile(UploadedFile): charset: Optional[str], content_type_extra: Dict[str, str] = ..., ) -> None: ... - def chunks(self, chunk_size: int = None) -> Iterator[bytes]: ... - def multiple_chunks(self, chunk_size: int = None) -> bool: ... + def chunks(self, chunk_size: Optional[int] = ...) -> Iterator[bytes]: ... + def multiple_chunks(self, chunk_size: Optional[int] = ...) -> bool: ... 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 def from_dict(cls: Any, file_dict: Dict[str, Union[str, bytes]]) -> None: ... diff --git a/django-stubs/forms/fields.pyi b/django-stubs/forms/fields.pyi index 350db7d..d8ddc3c 100644 --- a/django-stubs/forms/fields.pyi +++ b/django-stubs/forms/fields.pyi @@ -1,12 +1,8 @@ -import decimal -from datetime import date, datetime, time, timedelta +from datetime import datetime, timedelta from decimal import Decimal -from typing import Any, Callable, Dict, List, Optional, Tuple, Union, Type -from uuid import UUID +from typing import Any, Callable, List, Optional, Pattern, Sequence, Type, Union -from django.core.files.base import File from django.core.validators import BaseValidator -from django.db.models.fields.files import FieldFile from django.forms.boundfield import BoundField from django.forms.forms import BaseForm from django.forms.widgets import Widget @@ -31,16 +27,16 @@ class Field: self, *, required: bool = ..., - widget: Optional[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: Any = ..., + validators: Sequence[Any] = ..., localize: bool = ..., disabled: bool = ..., - label_suffix: Optional[Any] = ... + label_suffix: Optional[Any] = ..., ) -> None: ... def prepare_value(self, value: Any) -> Any: ... def to_python(self, value: Optional[Any]) -> Optional[Any]: ... @@ -49,60 +45,60 @@ class Field: def clean(self, value: Any) -> Any: ... def bound_data(self, data: Any, initial: Any) -> 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 __deepcopy__(self, memo: Dict[Any, Any]) -> Field: ... class CharField(Field): - disabled: bool - error_messages: Dict[str, str] - required: bool - show_hidden_initial: bool max_length: Optional[Union[int, str]] = ... min_length: Optional[Union[int, str]] = ... strip: bool = ... empty_value: Optional[str] = ... def __init__( self, - *, max_length: Optional[Any] = ..., min_length: Optional[Any] = ..., strip: bool = ..., - empty_value: str = ..., - **kwargs: Any + empty_value: Optional[str] = ..., + 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 IntegerField(Field): - disabled: bool - error_messages: Dict[str, str] max_value: Optional[Any] min_value: Optional[Any] - required: bool - show_hidden_initial: bool - default_error_messages: 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): - 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: ... class DecimalField(IntegerField): decimal_places: Optional[int] - disabled: bool - error_messages: Dict[str, str] 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__( self, *, @@ -110,118 +106,95 @@ class DecimalField(IntegerField): min_value: Optional[Any] = ..., max_digits: 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: ... def validate(self, value: Optional[Decimal]) -> None: ... class BaseTemporalField(Field): input_formats: Any = ... - def __init__(self, *, input_formats: Optional[Any] = ..., **kwargs: Any) -> None: ... - def strptime(self, value: Any, format: Any) -> Any: ... + def __init__( + 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): - 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) -> 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 DateField(BaseTemporalField): ... +class TimeField(BaseTemporalField): ... +class DateTimeField(BaseTemporalField): ... 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]: ... class RegexField(CharField): - disabled: bool - empty_value: str - error_messages: Dict[str, str] - max_length: Optional[int] - min_length: Optional[int] - required: bool - show_hidden_initial: bool - strip: bool - def __init__(self, regex: str, **kwargs: Any) -> None: ... - regex: Any = ... + regex: str = ... + def __init__( + self, + regex: Union[str, Pattern], + max_length: Optional[Any] = ..., + min_length: Optional[Any] = ..., + strip: bool = ..., + empty_value: Optional[str] = ..., + 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 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 EmailField(CharField): ... class FileField(Field): - disabled: bool - required: bool - show_hidden_initial: bool - default_error_messages: Any = ... max_length: Optional[int] = ... allow_empty_file: bool = ... - def __init__(self, *, max_length: Optional[Any] = ..., allow_empty_file: bool = ..., **kwargs: Any) -> None: ... - def bound_data(self, data: Any, initial: Optional[FieldFile]) -> Optional[Union[File, str]]: ... + def __init__( + 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): - allow_empty_file: bool - disabled: bool - max_length: Optional[int] - 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 ImageField(FileField): ... +class URLField(CharField): ... +class BooleanField(Field): ... +class NullBooleanField(BooleanField): ... class CallableChoiceIterator: choices_func: Callable = ... @@ -229,125 +202,191 @@ class CallableChoiceIterator: def __iter__(self) -> None: ... class ChoiceField(Field): - disabled: bool - error_messages: Dict[str, str] - required: bool - show_hidden_initial: bool - default_error_messages: Any = ... choices: Any = ... - def __init__(self, *, choices: Any = ..., **kwargs: Any) -> None: ... - def validate(self, value: Any) -> None: ... + def __init__( + 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: ... class TypedChoiceField(ChoiceField): - disabled: bool - required: bool - show_hidden_initial: bool - coerce: Union[Callable, Type[Union[bool, float, str]]] = ... + coerce: Union[Callable, Type[Any]] = ... 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): - 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 MultipleChoiceField(ChoiceField): ... class TypedMultipleChoiceField(MultipleChoiceField): - disabled: bool - required: bool - show_hidden_initial: bool coerce: Union[Callable, Type[float]] = ... empty_value: Optional[List[Any]] = ... - def __init__(self, *, coerce: Any = ..., **kwargs: Any) -> None: ... - def validate(self, value: List[str]) -> 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 ComboField(Field): - disabled: bool - required: bool - show_hidden_initial: bool 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): - disabled: bool - required: bool - show_hidden_initial: bool - default_error_messages: Any = ... require_all_fields: bool = ... fields: Any = ... - def __init__(self, fields: Tuple[Field, Field], *, require_all_fields: bool = ..., **kwargs: Any) -> None: ... - def validate(self, value: Union[datetime, str]) -> None: ... + def __init__( + 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: ... class FilePathField(ChoiceField): allow_files: bool allow_folders: bool - disabled: bool match: Optional[str] path: str recursive: bool - required: bool - show_hidden_initial: bool - choices: Any = ... match_re: Any = ... def __init__( self, path: str, - *, match: Optional[Any] = ..., recursive: bool = ..., allow_files: 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: ... class SplitDateTimeField(MultiValueField): - disabled: bool - require_all_fields: bool - required: bool - show_hidden_initial: bool - hidden_widget: Any = ... - default_error_messages: Any = ... 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: ... def compress(self, data_list: List[Optional[datetime]]) -> Optional[datetime]: ... 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 = ... - default_validators: List[Callable] = ... - def __init__(self, *, protocol: str = ..., unpack_ipv4: bool = ..., **kwargs: Any) -> None: ... + def __init__( + 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): - disabled: bool - empty_value: str - max_length: Optional[int] - min_length: None - required: bool - show_hidden_initial: bool - strip: 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): - 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: ... +class UUIDField(CharField): ... diff --git a/django-stubs/forms/forms.pyi b/django-stubs/forms/forms.pyi index 6b18a13..f62448d 100644 --- a/django-stubs/forms/forms.pyi +++ b/django-stubs/forms/forms.pyi @@ -1,48 +1,45 @@ -from collections import OrderedDict 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.files.base import File from django.core.files.uploadedfile import SimpleUploadedFile from django.db.models.query import QuerySet from django.forms.boundfield import BoundField from django.forms.fields import Field +from django.forms.renderers import BaseRenderer from django.forms.utils import ErrorDict, ErrorList from django.forms.widgets import Media, MediaDefiningClass from django.utils.safestring import SafeText class DeclarativeFieldsMetaclass(MediaDefiningClass): - def __new__( - mcs: Type[DeclarativeFieldsMetaclass], name: str, bases: Tuple[Type[BaseForm]], attrs: OrderedDict - ) -> Type[BaseForm]: ... + def __new__(mcs, name: str, bases: Sequence[Type[BaseForm]], attrs: Dict[str, Any]) -> Type[BaseForm]: ... class BaseForm: default_renderer: Any = ... field_order: Any = ... - prefix: Any = ... use_required_attribute: bool = ... - is_bound: Any = ... - data: Any = ... - files: Any = ... + is_bound: bool = ... + data: Dict[str, Any] = ... + files: Optional[Dict[str, Any]] = ... auto_id: Any = ... - initial: Any = ... - error_class: Any = ... - label_suffix: Any = ... - empty_permitted: Any = ... - fields: Any = ... - renderer: Any = ... + initial: Dict[str, Any] = ... + error_class: Type[ErrorList] = ... + prefix: str = ... + label_suffix: str = ... + empty_permitted: bool = ... + fields: Dict[str, Any] = ... + renderer: BaseRenderer = ... def __init__( self, data: Optional[Mapping[str, Any]] = ..., - files: Optional[Mapping[str, File]] = ..., + files: Optional[Mapping[str, Any]] = ..., auto_id: Optional[Union[bool, str]] = ..., prefix: Optional[str] = ..., initial: Optional[Mapping[str, Any]] = ..., error_class: Type[ErrorList] = ..., - label_suffix: None = ..., + label_suffix: Optional[str] = ..., empty_permitted: bool = ..., - field_order: None = ..., + field_order: Optional[Any] = ..., use_required_attribute: Optional[bool] = ..., renderer: Any = ..., ) -> None: ... diff --git a/django-stubs/forms/formsets.pyi b/django-stubs/forms/formsets.pyi index 3a0e4ee..0d98d5d 100644 --- a/django-stubs/forms/formsets.pyi +++ b/django-stubs/forms/formsets.pyi @@ -1,8 +1,4 @@ -import collections -from typing import Any, List, Optional, Union, Dict, Type - -from django.forms.renderers import BaseRenderer -from django.forms.utils import ErrorList +from typing import Any, Dict, Mapping, Optional, Sequence, Sized from django.forms import Form @@ -17,21 +13,9 @@ DEFAULT_MIN_NUM: int = ... DEFAULT_MAX_NUM: int = ... class ManagementForm(Form): - auto_id: Union[bool, str] 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 = ... prefix: Any = ... auto_id: Any = ... @@ -101,4 +85,4 @@ def formset_factory( min_num: Optional[Any] = ..., validate_min: bool = ..., ): ... -def all_valid(formsets: List[Any]) -> bool: ... +def all_valid(formsets: Sequence[Any]) -> bool: ... diff --git a/django-stubs/forms/models.pyi b/django-stubs/forms/models.pyi index 572d40b..213ca9f 100644 --- a/django-stubs/forms/models.pyi +++ b/django-stubs/forms/models.pyi @@ -1,10 +1,12 @@ from collections import OrderedDict 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 uuid import UUID +from django.core.files.base import File from django.core.files.uploadedfile import SimpleUploadedFile +from django.db import models from django.db.models import ForeignKey from django.db.models.base import Model 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.http.request import QueryDict from django.utils.datastructures import MultiValueDict +from typing_extensions import Literal ALL_FIELDS: str @@ -57,21 +60,19 @@ class ModelFormOptions: def __init__(self, options: Optional[type] = ...) -> None: ... class ModelFormMetaclass(DeclarativeFieldsMetaclass): - def __new__( - mcs: Type[ModelFormMetaclass], name: str, bases: Tuple[Type[ModelForm]], attrs: OrderedDict - ) -> Type[ModelForm]: ... + def __new__(mcs, name: str, bases: Sequence[Type[ModelForm]], attrs: Dict[str, Any]) -> Type[ModelForm]: ... class BaseModelForm(BaseForm): instance: Any = ... def __init__( self, - data: Optional[Union[Dict[str, Any], QueryDict]] = ..., - files: Optional[Union[Dict[str, SimpleUploadedFile], MultiValueDict]] = ..., + data: Optional[Dict[str, Any]] = ..., + files: Optional[Dict[str, File]] = ..., auto_id: Union[bool, str] = ..., - prefix: None = ..., - initial: Optional[Union[Dict[str, List[int]], Dict[str, int]]] = ..., + prefix: Optional[str] = ..., + initial: Optional[Dict[str, Any]] = ..., error_class: Type[ErrorList] = ..., - label_suffix: None = ..., + label_suffix: Optional[str] = ..., empty_permitted: bool = ..., instance: Optional[Model] = ..., use_required_attribute: None = ..., @@ -87,16 +88,16 @@ class ModelForm(BaseModelForm): ... def modelform_factory( model: Type[Model], form: Type[ModelForm] = ..., - fields: Optional[Union[List[str], str]] = ..., - exclude: None = ..., - formfield_callback: Optional[str] = ..., - widgets: None = ..., - localized_fields: None = ..., - labels: None = ..., - help_texts: None = ..., - error_messages: None = ..., - field_classes: None = ..., -) -> Any: ... + fields: Optional[Union[Sequence[str], Literal["__all__"]]] = ..., + exclude: Optional[Sequence[str]] = ..., + formfield_callback: Optional[Union[str, Callable[[models.Field], Field]]] = ..., + widgets: Optional[MutableMapping[str, Widget]] = ..., + localized_fields: Optional[Sequence[str]] = ..., + labels: Optional[MutableMapping[str, str]] = ..., + help_texts: Optional[MutableMapping[str, str]] = ..., + error_messages: Optional[MutableMapping[str, Dict[str, Any]]] = ..., + field_classes: Optional[MutableMapping[str, Type[Field]]] = ..., +) -> ModelForm: ... class BaseModelFormSet(BaseFormSet): model: Any = ... @@ -148,13 +149,13 @@ def modelformset_factory( exclude: Optional[Sequence[str]] = ..., widgets: Optional[Dict[str, Any]] = ..., validate_max: bool = ..., - localized_fields: None = ..., + localized_fields: Optional[Sequence[str]] = ..., labels: Optional[Dict[str, str]] = ..., help_texts: Optional[Dict[str, str]] = ..., error_messages: Optional[Dict[str, Dict[str, str]]] = ..., validate_min: bool = ..., - field_classes: Optional[Dict[str, Any]] = ..., -) -> Any: ... + field_classes: Optional[Dict[str, Type[Field]]] = ..., +) -> BaseModelFormSet: ... class BaseInlineFormSet(BaseModelFormSet): instance: Any = ... @@ -192,7 +193,7 @@ def inlineformset_factory( formfield_callback: Optional[Callable] = ..., widgets: Optional[Dict[str, Any]] = ..., validate_max: bool = ..., - localized_fields: None = ..., + localized_fields: Optional[Sequence[str]] = ..., labels: Optional[Dict[str, str]] = ..., help_texts: Optional[Dict[str, str]] = ..., error_messages: Optional[Dict[str, Dict[str, str]]] = ..., diff --git a/django-stubs/forms/utils.pyi b/django-stubs/forms/utils.pyi index 31ea3cc..f48d4f5 100644 --- a/django-stubs/forms/utils.pyi +++ b/django-stubs/forms/utils.pyi @@ -6,11 +6,11 @@ from django.core.exceptions import ValidationError from django.utils.safestring import SafeText def pretty_name(name: str) -> str: ... -def flatatt(attrs: Dict[str, Optional[str]]) -> SafeText: ... +def flatatt(attrs: Dict[str, Any]) -> SafeText: ... class ErrorDict(dict): 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_ul(self) -> str: ... def as_text(self) -> str: ... @@ -19,7 +19,9 @@ class ErrorList(UserList): data: List[Union[ValidationError, str]] error_class: str = ... 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: ... def as_data(self) -> List[ValidationError]: ... def get_json_data(self, escape_html: bool = ...) -> List[Dict[str, str]]: ... diff --git a/django-stubs/forms/widgets.pyi b/django-stubs/forms/widgets.pyi index b4a7482..b929aa3 100644 --- a/django-stubs/forms/widgets.pyi +++ b/django-stubs/forms/widgets.pyi @@ -1,17 +1,16 @@ from datetime import time from decimal import Decimal 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.uploadedfile import SimpleUploadedFile from django.db.models.fields.files import FieldFile -from django.forms.forms import BaseForm from django.forms.renderers import EngineMixin from django.utils.datastructures import MultiValueDict from django.utils.safestring import SafeText +_OptAttrs = Dict[str, str] + class MediaOrderConflictWarning(RuntimeWarning): ... class Media: @@ -32,9 +31,7 @@ class Media: def __add__(self, other: Media) -> Media: ... class MediaDefiningClass(type): - def __new__( - mcs: Type[MediaDefiningClass], name: str, bases: Tuple, attrs: Any - ) -> Type[Union[BaseModelAdmin, BaseForm, Widget]]: ... + def __new__(mcs, name: str, bases: Sequence[Any], attrs: Dict[str, Any]) -> type: ... class Widget: 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]]] = ... ) -> Dict[str, Union[Decimal, float, str]]: ... 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: ... 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: ... def id_for_label(self, id_: str) -> str: ... def use_required_attribute(self, initial: Any) -> bool: ... @@ -80,6 +77,7 @@ class URLInput(Input): ... class PasswordInput(Input): render_value: bool = ... + def __init__(self, attrs: Optional[_OptAttrs] = ..., render_value: bool = ...): ... class HiddenInput(Input): choices: Iterable[Tuple[str, str]] @@ -105,6 +103,7 @@ class DateTimeBaseInput(TextInput): format_key: str = ... supports_microseconds: bool = ... format: Optional[str] = ... + def __init__(self, attrs: Optional[_OptAttrs] = ..., format: Optional[str] = ...): ... class DateInput(DateTimeBaseInput): ... class DateTimeInput(DateTimeBaseInput): ... @@ -112,9 +111,7 @@ class TimeInput(DateTimeBaseInput): ... class CheckboxInput(Input): check_test: Callable = ... - def __init__(self, attrs: Optional[Dict[str, str]] = ..., check_test: Optional[Callable] = ...) -> None: ... - -_OptAttrs = Dict[str, Any] + def __init__(self, attrs: Optional[_OptAttrs] = ..., check_test: Optional[Callable] = ...) -> None: ... class ChoiceWidget(Widget): allow_multiple_selected: bool = ... @@ -165,7 +162,7 @@ class CheckboxSelectMultiple(ChoiceWidget): class MultiWidget(Widget): template_name: str = ... 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 def is_hidden(self) -> bool: ... def decompress(self, value: Any) -> Optional[Any]: ... @@ -209,7 +206,7 @@ class SelectDateWidget(Widget): def __init__( self, attrs: Optional[_OptAttrs] = ..., - years: Optional[Union[Tuple[Union[int, str]], range]] = ..., - months: None = ..., - empty_label: Optional[Union[Tuple[str, str], str]] = ..., + years: Optional[Iterable[Union[int, str]]] = ..., + months: Optional[Dict[int, str]] = ..., + empty_label: Optional[Union[str, Sequence[str]]] = ..., ) -> None: ... diff --git a/django-stubs/views/generic/edit.pyi b/django-stubs/views/generic/edit.pyi index f4325bd..d87c893 100644 --- a/django-stubs/views/generic/edit.pyi +++ b/django-stubs/views/generic/edit.pyi @@ -1,5 +1,6 @@ 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.detail import BaseDetailView, SingleObjectMixin, SingleObjectTemplateResponseMixin from typing_extensions import Literal @@ -10,7 +11,7 @@ from django.http import HttpRequest, HttpResponse class FormMixin(ContextMixin): initial: Dict[str, Any] = ... - form_class: Optional[Type[Form]] = ... + form_class: Optional[Type[BaseForm]] = ... success_url: Optional[Union[str, Callable[..., Any]]] = ... prefix: Optional[str] = ... request: HttpRequest = ... diff --git a/scripts/typecheck_tests.py b/scripts/typecheck_tests.py index 6369b65..8d838e8 100644 --- a/scripts/typecheck_tests.py +++ b/scripts/typecheck_tests.py @@ -32,7 +32,7 @@ IGNORED_ERRORS = { 'Cannot assign to a type', re.compile(r'Cannot assign to class variable "[a-z_]+" via instance'), # forms <-> models plugin support - '"Model" has no attribute', + # '"Model" has no attribute', re.compile(r'Cannot determine type of \'(objects|stuff)\''), # settings re.compile(r'Module has no attribute "[A-Z_]+"'), @@ -54,7 +54,8 @@ IGNORED_ERRORS = { 'ValuesIterable', '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 "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': [ 'Incompatible types in assignment (expression has type "FilteredChildAdmin", variable has type "ChildAdmin")' @@ -166,6 +167,29 @@ IGNORED_ERRORS = { 'fixtures': [ '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': [ 'Argument 1 to "get_object_or_404" has incompatible type "str"; ' + 'expected "Union[Type[], Manager[], QuerySet[]]"', @@ -330,7 +354,6 @@ IGNORED_ERRORS = { 'template_backends': [ 'Incompatible import of "Jinja2" (imported name has type "Type[Jinja2]", local name has type "object")', 'TemplateStringsTests', - 'Incompatible types in assignment (expression has type "None", variable has type Module)' ], 'urlpatterns': [ '"object" has no attribute "__iter__"; maybe "__str__" or "__dir__"? (not iterable)', @@ -440,7 +463,7 @@ TESTS_DIRS = [ 'flatpages_tests', 'force_insert_update', 'foreign_object', - # TODO: 'forms_tests', + 'forms_tests', 'from_db_value', 'generic_inline_admin', 'generic_relations',