mirror of
https://github.com/davidhalter/django-stubs.git
synced 2026-01-26 21:09:02 +08:00
* Make module declaration precise. * Make settings match real file. * Replace `include` with import. * Make types more specific. * Replace `WSGIRequest` with `HttpRequest` where possible. * Replace all `OrderedDict` occurrences with plain `Dict` (it is not used in Django 3.2 and later) * Add fake datastructures for convenience: _PropertyDescriptor and _ListOrTuple now can live here. Added _IndexableCollection (often useful as alias for 'sequence or queryset') * Actualize other datastructures. * Rework MultiValueDict to reflect the fact that some methods can return empty list instead of value. * Deprecate SafeText in favor of SafeString. * Minor improvements to utils * Disallow using str in TimeFormat and DateFormat, drop removed fmt `B` * Do not let classproperty expect classmethod, make return value covariant. * Sync with real file. * Improve types for timezone. * Sync deprecated, new and removed features in translation utils. * Drop removed files, sync huge deprecations. * Fix incompatible decorators (properties, contextmanagers) * Rework pagination. * Sync validators with real code. Add _ValidatorCallable for any external use (field validation etc.) * Add shared type definitions (for fields of both forms and models). Actualize model fields. Mark keyword-only args explicitly in stubs (where code uses **kwargs). Disallow bytes for verbose_name. * Make all checks return Sequence[CheckMessage] or subclass to be covariant. * Add bidirectional references between backend.base and other files. Replace some Any's with specific types. * Actualize db.migrations: remove removed methods, replace "None" annotations in wrong places, improve some wrong annotations. * Actualize db.utils to match real file. * Replace FileResponse and TemplateResponse with HttpResponse(Base) where needed: at least HttpResponseNotModified/HttpResponseRedirect can be returned instead of it, so annotation was wrong. * Replace Any in forms where possible. Actualize class bases and method arguments. * Improve typing of serializers. * Actualize views, rename variable bound to Model to _M for consistency. * Make types of file-related code consistent. Disallow using bytes as path, because many methods expect str-only paths. Make File inherit from IO[AnyStr] instead of IO[Any]: it makes impossible to instantiate file of union type, but allows precise types for some methods. * Minor improvements: stop using None as annotation in wrong places, replace obvious Any's with precise types, actualize methods (missing/renamed/signature changed). * Allow less specific containers, replace Any's with specific types. * Improve types for requests and responses. * Use AbstractBaseUser instead of User in auth. * Use broader type for permission_required * Use wider container types. Add 'type: ignore' to avoid issues with mypy.stubtest. * Disallow using backend class as argument (it is passed to import_string). * Add required methods to PasseordValidator. * Allow using Path instance as argument. * Actualize methods. * Add 'type: ignore' to avoid issues with mypy.stubtest. * Replace Any's with specific types and BaseForm with ModelForm. * Actualize contrib.postgres * Remove render_to_response, add 'get_absolute_url' to corresponding protocol. * Actualize signers. * Use precise types for handlers. Disallow str as stream type for LimitedStream. * Exact types for ValidationError * Replace wrong used Union with Sequence. * Actualize static handlers. * More specific types for admin. Fixes #874. * Improve types and replace 'Tags' with str (it isn't Enum, so annotation was wrong). * Replace Any with specific types, actualize signatures. * Add async variants of handlers and clients. Use fake class to distinguish between request types in RequestFactory and AsyncRequestFactory. * Fix signature, minor improvements. * Actualize signatures and class names, replace Any with more specific types. * Fix signature. * Add some missing methods to Collector * Combinable rarely returns Self type: almost always it's CombinedExpression. * No Random in source anymore. * Drop removed SimpleCol. * Replace _OutputField with Field: nothing in docs about strings. * Introduce reusable types, add missing methods. Remove strange types (probably created by stubgen). Remove RawQuery from Compiler: it obviously doesn't work with RawQuery. * Use literal constants. * Actualize base classes. * Callable is not accepted by get_field. * Add precise types. * Use property and broader containers where possible. Add missing methods. * Actualize indexes. * More specific types for signals. * Fix signatures, drop missing methods. * Actualize window functions to match source. * Actualize text functions, add missing methods, use type aliases for consistency. * Add missing property decorators, methods and attributes. Use type aliases. Remove absent YearComparisonLookup and any SafeText references (they aren't related to lookups at all). * Use bound TypeVar, mark all BuiltinLookup descendants as generic explicitly. Remove strange Union from Lookup.__init__ * Apply type alias, fix base class and argument name. * Actualize BaseExpression methods. * Fix imports. * Add missing class and fix incompatible bases. * Use same types in __init__ and attribute. * OrderBy accepts F or Expression. * Non-expressions are converted to Values. * Add missing attributes. * Add missing methods, fix 'None' argument type. * Define properties where possible, remove 'None' argument annotations, remove inadequate type in make_immutable_fields_list. * Remove absent QueryWrapper. Replace some Any with precise types. * Fix wrong types and actualize signatures. Deny ManagerDescriptor.__get__ on model instances. * Use more specific types. * Arity can be None in subclasses. * Reformat with black * Make DeletionMixin generic. * Fix wrong type variable in _RequestFactory. * Fix variable name in signature. * Disallow returning None from Form.clean() * Allow again returning None from Form.clean * Drop some unused imports. * Add tests for MultiValueDict. * Add tests for utils.timezone. * Fix #834. * Add more files to import_all test * Allow None for `context_object_name` * Fix CI * Fix test to work on python 3.8
133 lines
4.6 KiB
Python
133 lines
4.6 KiB
Python
import sys
|
|
from typing import (
|
|
Any,
|
|
Collection,
|
|
Dict,
|
|
Generic,
|
|
Iterable,
|
|
Iterator,
|
|
List,
|
|
Mapping,
|
|
MutableSet,
|
|
Optional,
|
|
Tuple,
|
|
TypeVar,
|
|
Union,
|
|
overload,
|
|
)
|
|
|
|
if sys.version_info < (3, 8):
|
|
from typing_extensions import Protocol
|
|
else:
|
|
from typing import Protocol
|
|
|
|
_K = TypeVar("_K")
|
|
_V = TypeVar("_V")
|
|
_Z = TypeVar("_Z")
|
|
_I = TypeVar("_I", covariant=True)
|
|
|
|
# Unfortunately, there's often check `if isinstance(var, (list, tuple))` in django
|
|
# codebase. So we need sometimes to declare exactly list or tuple.
|
|
_ListOrTuple = Union[List[_K], Tuple[_K, ...], Tuple[()]]
|
|
|
|
class _PropertyDescriptor(Generic[_K, _V]):
|
|
"""
|
|
This helper property descriptor allows defining asynmetric getter/setters
|
|
which mypy currently doesn't support with either:
|
|
|
|
class HttpResponse:
|
|
@property
|
|
def content(...): ...
|
|
@property.setter
|
|
def content(...): ...
|
|
|
|
or:
|
|
|
|
class HttpResponse:
|
|
def _get_content(...): ...
|
|
def _set_content(...): ...
|
|
content = property(_get_content, _set_content)
|
|
"""
|
|
|
|
def __get__(self, instance: Any, owner: Optional[Any]) -> _V: ...
|
|
def __set__(self, instance: Any, value: _K) -> None: ...
|
|
|
|
_IC = TypeVar("_IC", bound="_IndexableCollection")
|
|
|
|
class _IndexableCollection(Protocol[_I], Collection[_I]):
|
|
@overload
|
|
def __getitem__(self, index: int) -> _I: ...
|
|
@overload
|
|
def __getitem__(self: _IC, index: slice) -> _IC: ...
|
|
|
|
class OrderedSet(MutableSet[_K]):
|
|
dict: Dict[_K, None] = ...
|
|
def __init__(self, iterable: Optional[Iterable[_K]] = ...) -> None: ...
|
|
def __contains__(self, item: object) -> bool: ...
|
|
def __iter__(self) -> Iterator[_K]: ...
|
|
def __bool__(self) -> bool: ...
|
|
def __len__(self) -> int: ...
|
|
def add(self, item: _K) -> None: ...
|
|
def remove(self, item: _K) -> None: ...
|
|
def discard(self, item: _K) -> None: ...
|
|
|
|
class MultiValueDictKeyError(KeyError): ...
|
|
|
|
_D = TypeVar("_D", bound="MultiValueDict")
|
|
|
|
class MultiValueDict(Dict[_K, _V]):
|
|
@overload
|
|
def __init__(self, key_to_list_mapping: Mapping[_K, Optional[List[_V]]] = ...) -> None: ...
|
|
@overload
|
|
def __init__(self, key_to_list_mapping: Iterable[Tuple[_K, List[_V]]] = ...) -> None: ...
|
|
@overload
|
|
def get(self, key: _K) -> Optional[_V]: ...
|
|
@overload
|
|
def get(self, key: _K, default: _Z = ...) -> Union[_V, _Z]: ...
|
|
def getlist(self, key: _K, default: _Z = ...) -> Union[List[_V], _Z]: ...
|
|
def setlist(self, key: _K, list_: List[_V]) -> None: ...
|
|
def setdefault(self, key: _K, default: _V = ...) -> _V: ...
|
|
def setlistdefault(self, key: _K, default_list: Optional[List[_V]] = ...) -> List[_V]: ...
|
|
def appendlist(self, key: _K, value: _V) -> None: ...
|
|
def items(self) -> Iterator[Tuple[_K, Union[_V, List[object]]]]: ... # type: ignore
|
|
def lists(self) -> Iterable[Tuple[_K, List[_V]]]: ...
|
|
def dict(self) -> Dict[_K, Union[_V, List[object]]]: ...
|
|
def copy(self: _D) -> _D: ...
|
|
def __getitem__(self, key: _K) -> Union[_V, List[object]]: ... # type: ignore
|
|
def __setitem__(self, key: _K, value: _V) -> None: ...
|
|
# These overrides are needed to convince mypy that this isn't an abstract class
|
|
def __delitem__(self, item: _K) -> None: ...
|
|
def __len__(self) -> int: ...
|
|
def __iter__(self) -> Iterator[_K]: ...
|
|
# Fake to make `values` work properly
|
|
def values(self) -> Iterator[Union[_V, List[object]]]: ... # type: ignore[override]
|
|
|
|
class ImmutableList(Tuple[_V, ...]):
|
|
warning: str = ...
|
|
def __new__(cls, *args: Any, warning: str = ..., **kwargs: Any) -> ImmutableList: ...
|
|
def complain(self, *args: Any, **kwargs: Any) -> None: ...
|
|
|
|
class _ItemCallable(Protocol[_V]):
|
|
"""Don't mess with arguments when assigning in class body in stub"""
|
|
|
|
def __call__(self, __value: _V) -> _V: ...
|
|
|
|
class DictWrapper(Dict[str, _V]):
|
|
func: _ItemCallable[_V] = ...
|
|
prefix: str = ...
|
|
@overload
|
|
def __init__(self, data: Mapping[str, _V], func: _ItemCallable[_V], prefix: str) -> None: ...
|
|
@overload
|
|
def __init__(self, data: Iterable[Tuple[str, _V]], func: _ItemCallable[_V], prefix: str) -> None: ...
|
|
def __getitem__(self, key: str) -> _V: ...
|
|
|
|
_T = TypeVar("_T", bound="CaseInsensitiveMapping")
|
|
|
|
class CaseInsensitiveMapping(Mapping[str, _V]):
|
|
_store: Dict[str, Tuple[str, _V]]
|
|
def __init__(self, data: Union[Mapping[str, _V], Iterable[Tuple[str, _V]]]) -> None: ...
|
|
def __getitem__(self, key: str) -> _V: ...
|
|
def __len__(self) -> int: ...
|
|
def __iter__(self) -> Iterator[str]: ...
|
|
def copy(self: _T) -> _T: ...
|