From b5bddc479d6928eec5287cbb3328a7dfe8f007d2 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Fri, 20 Jul 2018 17:13:32 +0200 Subject: [PATCH] Add werkzeug annotations (#2339) * Annotate werkzeug.http * Annotate werkzeug.datastructures.Authorization --- third_party/2and3/werkzeug/datastructures.pyi | 42 +++--- third_party/2and3/werkzeug/http.pyi | 124 +++++++++++++----- 2 files changed, 116 insertions(+), 50 deletions(-) diff --git a/third_party/2and3/werkzeug/datastructures.pyi b/third_party/2and3/werkzeug/datastructures.pyi index 127fdf981..3327964ea 100644 --- a/third_party/2and3/werkzeug/datastructures.pyi +++ b/third_party/2and3/werkzeug/datastructures.pyi @@ -1,5 +1,6 @@ -from typing import Any -from collections import Container, Iterable, Mapping, MutableSet +import collections +from typing import Any, Optional, Mapping, Dict +from collections import Container, Iterable, MutableSet def is_immutable(self): ... def iter_multi_items(mapping): ... @@ -124,7 +125,7 @@ class OrderedMultiDict(MultiDict): def popitem(self): ... def popitemlist(self): ... -class Headers(Mapping): +class Headers(collections.Mapping): def __init__(self, defaults=None): ... def __getitem__(self, key, _get_mode=False): ... def __eq__(self, other): ... @@ -330,20 +331,29 @@ class ContentRange: def __nonzero__(self): ... __bool__ = ... # type: Any -class Authorization(ImmutableDictMixin, dict): - type = ... # type: Any - def __init__(self, auth_type, data=None): ... - username = ... # type: Any - password = ... # type: Any - realm = ... # type: Any - nonce = ... # type: Any - uri = ... # type: Any - nc = ... # type: Any - cnonce = ... # type: Any - response = ... # type: Any - opaque = ... # type: Any +class Authorization(ImmutableDictMixin, Dict[str, Any]): + type: str + def __init__(self, auth_type: str, data: Optional[Mapping[str, Any]] = ...) -> None: ... @property - def qop(self): ... + def username(self) -> Optional[str]: ... + @property + def password(self) -> Optional[str]: ... + @property + def realm(self) -> Optional[str]: ... + @property + def nonce(self) -> Optional[str]: ... + @property + def uri(self) -> Optional[str]: ... + @property + def nc(self) -> Optional[str]: ... + @property + def cnonce(self) -> Optional[str]: ... + @property + def response(self) -> Optional[str]: ... + @property + def opaque(self) -> Optional[str]: ... + @property + def qop(self) -> Optional[str]: ... class WWWAuthenticate(UpdateDictMixin, dict): on_update = ... # type: Any diff --git a/third_party/2and3/werkzeug/http.pyi b/third_party/2and3/werkzeug/http.pyi index 33334f836..2e078ff84 100644 --- a/third_party/2and3/werkzeug/http.pyi +++ b/third_party/2and3/werkzeug/http.pyi @@ -1,36 +1,92 @@ -from typing import Any +import sys +from datetime import datetime, timedelta +from typing import ( + Dict, Text, Union, Tuple, Any, Optional, Mapping, Iterable, Callable, List, Type, + TypeVar, Protocol, overload, SupportsInt, +) +from wsgiref.types import WSGIEnvironment -HTTP_STATUS_CODES = ... # type: Any +from .datastructures import ( + Headers, Accept, RequestCacheControl, HeaderSet, Authorization, WWWAuthenticate, + IfRange, Range, ContentRange, ETags, TypeConversionDict, +) -def wsgi_to_bytes(data): ... -def bytes_to_wsgi(data): ... -def quote_header_value(value, extra_chars='', allow_token=True): ... -def unquote_header_value(value, is_filename=False): ... -def dump_options_header(header, options): ... -def dump_header(iterable, allow_token=True): ... -def parse_list_header(value): ... -def parse_dict_header(value, cls=...): ... -def parse_options_header(value, multiple=False): ... -def parse_accept_header(value, cls=None): ... -def parse_cache_control_header(value, on_update=None, cls=None): ... -def parse_set_header(value, on_update=None): ... -def parse_authorization_header(value): ... -def parse_www_authenticate_header(value, on_update=None): ... -def parse_if_range_header(value): ... -def parse_range_header(value, make_inclusive=True): ... -def parse_content_range_header(value, on_update=None): ... -def quote_etag(etag, weak=False): ... -def unquote_etag(etag): ... -def parse_etags(value): ... -def generate_etag(data): ... -def parse_date(value): ... -def cookie_date(expires=None): ... -def http_date(timestamp=None): ... -def is_resource_modified(environ, etag=None, data=None, last_modified=None, ignore_if_range=True): ... -def remove_entity_headers(headers, allowed=...): ... -def remove_hop_by_hop_headers(headers): ... -def is_entity_header(header): ... -def is_hop_by_hop_header(header): ... -def parse_cookie(header, charset='', errors='', cls=None): ... -def dump_cookie(key, value='', max_age=None, expires=None, path='', domain=None, secure=False, httponly=False, charset='', sync_expires=True): ... -def is_byte_range_valid(start, stop, length): ... +if sys.version_info < (3,): + _Str = TypeVar('_Str', str, unicode) + _ToBytes = Union[bytes, bytearray, buffer, unicode] + _ETagData = Union[str, unicode, bytearray, buffer, memoryview] +else: + _Str = str + _ToBytes = Union[bytes, bytearray, memoryview, str] + _ETagData = Union[bytes, bytearray, memoryview] + +_T = TypeVar("_T") +_U = TypeVar("_U") + +HTTP_STATUS_CODES: Dict[int, str] + +def wsgi_to_bytes(data: Union[bytes, Text]) -> bytes: ... +def bytes_to_wsgi(data: bytes) -> str: ... +def quote_header_value(value: Any, extra_chars: str = ..., allow_token: bool = ...) -> str: ... +def unquote_header_value(value: _Str, is_filename: bool = ...) -> _Str: ... +def dump_options_header(header: Optional[_Str], options: Mapping[_Str, Any]) -> _Str: ... +def dump_header(iterable: Union[Iterable[Any], Dict[_Str, Any]], allow_token: bool = ...) -> _Str: ... +def parse_list_header(value: _Str) -> List[_Str]: ... +@overload +def parse_dict_header(value: Union[bytes, Text]) -> Dict[Text, Optional[Text]]: ... +@overload +def parse_dict_header(value: Union[bytes, Text], cls: Type[_T]) -> _T: ... +@overload +def parse_options_header(value: None, multiple: bool = ...) -> Tuple[str, Dict[str, Optional[str]]]: ... +@overload +def parse_options_header(value: _Str) -> Tuple[_Str, Dict[_Str, Optional[_Str]]]: ... +# actually returns Tuple[_Str, Dict[_Str, Optional[_Str]], ...] +@overload +def parse_options_header(value: _Str, multiple: bool = ...) -> Tuple[Any, ...]: ... +@overload +def parse_accept_header(value: Optional[Text]) -> Accept: ... +@overload +def parse_accept_header(value: Optional[_Str], cls: Callable[[Optional[_Str]], _T]) -> _T: ... +@overload +def parse_cache_control_header(value: Union[None, bytes, Text], + on_update: Optional[Callable[[RequestCacheControl], Any]] = ...) -> RequestCacheControl: ... +@overload +def parse_cache_control_header(value: Union[None, bytes, Text], on_update: _T, + cls: Callable[[Dict[Text, Optional[Text]], _T], _U]) -> _U: ... +@overload +def parse_cache_control_header(value: Union[None, bytes, Text], *, + cls: Callable[[Dict[Text, Optional[Text]], None], _U]) -> _U: ... +def parse_set_header(value: Text, on_update: Optional[Callable[[HeaderSet], Any]] = ...) -> HeaderSet: ... +def parse_authorization_header(value: Union[None, bytes, Text]) -> Optional[Authorization]: ... +def parse_www_authenticate_header(value: Union[None, bytes, Text], + on_update: Optional[Callable[[WWWAuthenticate], Any]] = ...) -> WWWAuthenticate: ... +def parse_if_range_header(value: Optional[Text]) -> IfRange: ... +def parse_range_header(value: Optional[Text], make_inclusive: bool = ...) -> Optional[Range]: ... +def parse_content_range_header(value: Optional[Text], + on_update: Optional[Callable[[ContentRange], Any]] = ...) -> Optional[ContentRange]: ... +def quote_etag(etag: _Str, weak: bool = ...) -> _Str: ... +def unquote_etag(etag: Optional[_Str]) -> Tuple[Optional[_Str], Optional[_Str]]: ... +def parse_etags(value: Optional[Text]) -> ETags: ... +def generate_etag(data: _ETagData) -> str: ... +def parse_date(value: Optional[str]) -> Optional[datetime]: ... +def cookie_date(expires: Union[None, float, datetime] = ...) -> str: ... +def http_date(timestamp: Union[None, float, datetime] = ...) -> str: ... +def parse_age(value: Optional[SupportsInt] = ...) -> Optional[timedelta]: ... +def dump_age(age: Union[None, timedelta, SupportsInt]) -> Optional[str]: ... +def is_resource_modified(environ: WSGIEnvironment, etag: Optional[Text] = ..., data: Optional[_ETagData] = ..., + last_modified: Union[None, Text, datetime] = ..., ignore_if_range: bool = ...) -> bool: ... +def remove_entity_headers(headers: Union[List[Tuple[Text, Text]], Headers], allowed: Iterable[Text] = ...) -> None: ... +def remove_hop_by_hop_headers(headers: Union[List[Tuple[Text, Text]], Headers]) -> None: ... +def is_entity_header(header: Text) -> bool: ... +def is_hop_by_hop_header(header: Text) -> bool: ... +@overload +def parse_cookie(header: Union[None, WSGIEnvironment, Text, bytes], charset: Text = ..., + errors: Text = ...) -> TypeConversionDict: ... +@overload +def parse_cookie(header: Union[None, WSGIEnvironment, Text, bytes], charset: Text = ..., + errors: Text = ..., cls: Optional[Callable[[Iterable[Tuple[Text, Text]]], _T]] = ...) -> _T: ... +def dump_cookie(key: _ToBytes, value: _ToBytes = ..., max_age: Union[None, float, timedelta] = ..., + expires: Union[None, Text, float, datetime] = ..., path: Union[None, tuple, str, bytes] = ..., + domain: Union[None, str, bytes] = ..., secure: bool = ..., httponly: bool = ..., charset: Text = ..., + sync_expires: bool = ...) -> str: ... +def is_byte_range_valid(start: Optional[int], stop: Optional[int], length: Optional[int]) -> bool: ...