From a2676ec9720926ba0187b6e9d09a8d77efe96640 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Fri, 17 Aug 2018 17:35:14 +0200 Subject: [PATCH] Improve werkzeug stubs (#2391) --- third_party/2and3/werkzeug/contrib/fixers.pyi | 15 ++++--- third_party/2and3/werkzeug/datastructures.pyi | 43 ++++++++++++++++--- third_party/2and3/werkzeug/wrappers.pyi | 5 ++- 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/third_party/2and3/werkzeug/contrib/fixers.pyi b/third_party/2and3/werkzeug/contrib/fixers.pyi index a45c661c9..cbb31f377 100644 --- a/third_party/2and3/werkzeug/contrib/fixers.pyi +++ b/third_party/2and3/werkzeug/contrib/fixers.pyi @@ -1,4 +1,5 @@ -from typing import Any +from typing import Any, Sequence, Optional, Iterable +from wsgiref.types import WSGIApplication, WSGIEnvironment, StartResponse class CGIRootFix: app = ... # type: Any @@ -13,12 +14,12 @@ class PathInfoFromRequestUriFix: def __init__(self, app): ... def __call__(self, environ, start_response): ... -class ProxyFix: - app = ... # type: Any - num_proxies = ... # type: Any - def __init__(self, app, num_proxies=1): ... - def get_remote_addr(self, forwarded_for): ... - def __call__(self, environ, start_response): ... +class ProxyFix(object): + app: WSGIApplication + num_proxies: int + def __init__(self, app: WSGIApplication, num_proxies: int = ...) -> None: ... + def get_remote_addr(self, forwarded_for: Sequence[str]) -> Optional[str]: ... + def __call__(self, environ: WSGIEnvironment, start_response: StartResponse) -> Iterable[bytes]: ... class HeaderRewriterFix: app = ... # type: Any diff --git a/third_party/2and3/werkzeug/datastructures.pyi b/third_party/2and3/werkzeug/datastructures.pyi index 3327964ea..f031369e2 100644 --- a/third_party/2and3/werkzeug/datastructures.pyi +++ b/third_party/2and3/werkzeug/datastructures.pyi @@ -1,7 +1,12 @@ import collections -from typing import Any, Optional, Mapping, Dict +from typing import Any, Optional, Mapping, Dict, TypeVar, Callable, Union, overload from collections import Container, Iterable, MutableSet +_K = TypeVar("_K") +_V = TypeVar("_V") +_R = TypeVar("_R") +_D = TypeVar("_D") + def is_immutable(self): ... def iter_multi_items(mapping): ... def native_itermethods(names): ... @@ -56,12 +61,19 @@ class UpdateDictMixin: popitem = ... # type: Any update = ... # type: Any -class TypeConversionDict(dict): - def get(self, key, default=None, type=None): ... +class TypeConversionDict(Dict[_K, _V]): + @overload + def get(self, key: _K, *, type: None = ...) -> Optional[_V]: ... + @overload + def get(self, key: _K, default: _D, type: None = ...) -> Union[_V, _D]: ... + @overload + def get(self, key: _K, *, type: Callable[[_V], _R]) -> Optional[_R]: ... + @overload + def get(self, key: _K, default: _D, type: Callable[[_V], _R]) -> Union[_R, _D]: ... -class ImmutableTypeConversionDict(ImmutableDictMixin, TypeConversionDict): - def copy(self): ... - def __copy__(self): ... +class ImmutableTypeConversionDict(ImmutableDictMixin, TypeConversionDict[_K, _V]): + def copy(self) -> TypeConversionDict[_K, _V]: ... + def __copy__(self) -> ImmutableTypeConversionDict[_K, _V]: ... class ViewItems: def __init__(self, multi_dict, method, repr_name, *a, **kw): ... @@ -130,7 +142,24 @@ class Headers(collections.Mapping): def __getitem__(self, key, _get_mode=False): ... def __eq__(self, other): ... def __ne__(self, other): ... - def get(self, key, default=None, type=None, as_bytes=False): ... + @overload + def get(self, key: str, *, type: None = ...) -> Optional[str]: ... + @overload + def get(self, key: str, default: _D, type: None = ...) -> Union[str, _D]: ... + @overload + def get(self, key: str, *, type: Callable[[str], _R]) -> Optional[_R]: ... + @overload + def get(self, key: str, default: _D, type: Callable[[str], _R]) -> Union[_R, _D]: ... + @overload + def get(self, key: str, *, as_bytes: bool) -> Any: ... + @overload + def get(self, key: str, *, type: None, as_bytes: bool) -> Any: ... + @overload + def get(self, key: str, *, type: Callable[[Any], _R], as_bytes: bool) -> Optional[_R]: ... + @overload + def get(self, key: str, default: Any, type: None, as_bytes: bool) -> Any: ... + @overload + def get(self, key: str, default: _D, type: Callable[[Any], _R], as_bytes: bool) -> Union[_R, _D]: ... def getlist(self, key, type=None, as_bytes=False): ... def get_all(self, name): ... def items(self, lower=False): ... diff --git a/third_party/2and3/werkzeug/wrappers.pyi b/third_party/2and3/werkzeug/wrappers.pyi index 183a7a194..44c0579fe 100644 --- a/third_party/2and3/werkzeug/wrappers.pyi +++ b/third_party/2and3/werkzeug/wrappers.pyi @@ -7,7 +7,7 @@ from wsgiref.types import WSGIEnvironment, InputStream from .datastructures import ( Authorization, CombinedMultiDict, EnvironHeaders, Headers, ImmutableMultiDict, - MultiDict, TypeConversionDict, HeaderSet, + MultiDict, ImmutableTypeConversionDict, HeaderSet, Accept, MIMEAccept, CharsetAccept, LanguageAccept, ) @@ -47,7 +47,8 @@ class BaseRequest: form = ... # type: ImmutableMultiDict values = ... # type: CombinedMultiDict files = ... # type: MultiDict - cookies = ... # type: TypeConversionDict + @property + def cookies(self) -> ImmutableTypeConversionDict[str, str]: ... headers = ... # type: EnvironHeaders path = ... # type: Text full_path = ... # type: Text