From 8404a6223108e3dea640561e91141777f92809c8 Mon Sep 17 00:00:00 2001 From: Alex Dehnert Date: Fri, 15 Dec 2017 00:05:28 -0500 Subject: [PATCH] requests: add more accurate stubs for certain fields of the Session object (#1504) * requests: allow strings in Session.verify Per the documentation[1] (and actual usage), the verify parameter can be a string or a bool. [1] http://docs.python-requests.org/en/master/user/advanced/#ssl-cert-verification * requests: explicitly support AuthBase for Session.auth According to the documentation, the auth parameter should be an AuthBase subclass[1]. In practice, the actual requirement seems to just be a Callable[[Request], Request], and AuthBase implements that with the __call__ method. However, mypy isn't currently smart enough to infer that __call__ implies Callable[2]. In the interim (and perhaps also to add clearer errors), explicitly support AuthBase. Additionally, this adds typing of AuthBase.__call__, to match the Callable[[Request], Request] declaration used elsewhere. [1] http://docs.python-requests.org/en/master/user/advanced/#custom-authentication [2] https://github.com/python/mypy/issues/797 --- third_party/2and3/requests/auth.pyi | 3 ++- third_party/2and3/requests/sessions.pyi | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/third_party/2and3/requests/auth.pyi b/third_party/2and3/requests/auth.pyi index b73aa7047..09a6df76b 100644 --- a/third_party/2and3/requests/auth.pyi +++ b/third_party/2and3/requests/auth.pyi @@ -3,6 +3,7 @@ from typing import Any, Text, Union from . import compat from . import cookies +from . import models from . import utils from . import status_codes @@ -17,7 +18,7 @@ CONTENT_TYPE_MULTI_PART = ... # type: Any def _basic_auth_str(username: Union[bytes, Text], password: Union[bytes, Text]) -> str: ... class AuthBase: - def __call__(self, r): ... + def __call__(self, r: models.Request) -> models.Request: ... class HTTPBasicAuth(AuthBase): username = ... # type: Any diff --git a/third_party/2and3/requests/sessions.pyi b/third_party/2and3/requests/sessions.pyi index 9f5c0d8b4..b2be8440b 100644 --- a/third_party/2and3/requests/sessions.pyi +++ b/third_party/2and3/requests/sessions.pyi @@ -2,7 +2,7 @@ from typing import Any, Union, MutableMapping, Text, Optional, IO, Tuple, Callable from . import adapters -from . import auth +from . import auth as _auth from . import compat from . import cookies from . import models @@ -61,12 +61,12 @@ _Hooks = MutableMapping[Text, Callable[[Response], Any]] class Session(SessionRedirectMixin): __attrs__ = ... # type: Any headers = ... # type: MutableMapping[Text, Text] - auth = ... # type: Union[None, Tuple[Text, Text], Callable[[Request], Request]] + auth = ... # type: Union[None, Tuple[Text, Text], _auth.AuthBase, Callable[[Request], Request]] proxies = ... # type: MutableMapping[Text, Text] hooks = ... # type: _Hooks params = ... # type: Union[bytes, MutableMapping[Text, Text]] stream = ... # type: bool - verify = ... # type: bool + verify = ... # type: Union[None, bool, Text] cert = ... # type: Union[None, Text, Tuple[Text, Text]] max_redirects = ... # type: int trust_env = ... # type: bool @@ -83,13 +83,13 @@ class Session(SessionRedirectMixin): headers: Optional[MutableMapping[Text, Text]] = ..., cookies: Union[None, RequestsCookieJar, MutableMapping[Text, Text]] = ..., files: Optional[MutableMapping[Text, IO]] = ..., - auth: Union[None, Tuple[Text, Text], Callable[[Request], Request]] = ..., + auth: Union[None, Tuple[Text, Text], _auth.AuthBase, Callable[[Request], Request]] = ..., timeout: Union[None, float, Tuple[float, float]] = ..., allow_redirects: Optional[bool] = ..., proxies: Optional[MutableMapping[Text, Text]] = ..., hooks: Optional[_Hooks] = ..., stream: Optional[bool] = ..., - verify: Optional[bool] = ..., + verify: Union[None, bool, Text] = ..., cert: Union[Text, Tuple[Text, Text], None] = ..., json: Optional[MutableMapping] = ..., ) -> Response: ...