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
This commit is contained in:
Alex Dehnert
2017-12-15 00:05:28 -05:00
committed by Jelle Zijlstra
parent c5439668fe
commit 8404a62231
2 changed files with 7 additions and 6 deletions

View File

@@ -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

View File

@@ -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: ...