Skip to content

Commit db2a987

Browse files
committed
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] python/mypy#797
1 parent 58d15d3 commit db2a987

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

third_party/2and3/requests/auth.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Any
44
from . import compat
55
from . import cookies
6+
from . import models
67
from . import utils
78
from . import status_codes
89

@@ -15,7 +16,7 @@ CONTENT_TYPE_FORM_URLENCODED = ... # type: Any
1516
CONTENT_TYPE_MULTI_PART = ... # type: Any
1617

1718
class AuthBase:
18-
def __call__(self, r): ...
19+
def __call__(self, r: models.Request) -> models.Request: ...
1920

2021
class HTTPBasicAuth(AuthBase):
2122
username = ... # type: Any

third_party/2and3/requests/sessions.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ from . import adapters
1616
from . import status_codes
1717

1818
BaseAdapter = adapters.BaseAdapter
19+
AuthBase = auth.AuthBase
1920
OrderedDict = compat.OrderedDict
2021
cookiejar_from_dict = cookies.cookiejar_from_dict
2122
extract_cookies_to_jar = cookies.extract_cookies_to_jar
@@ -60,7 +61,7 @@ _Data = Union[None, bytes, MutableMapping[Text, Text], IO]
6061
class Session(SessionRedirectMixin):
6162
__attrs__ = ... # type: Any
6263
headers = ... # type: Optional[MutableMapping[Text, Text]]
63-
auth = ... # type: Union[None, Tuple[Text, Text], Callable[[Request], Request]]
64+
auth = ... # type: Union[None, Tuple[Text, Text], AuthBase, Callable[[Request], Request]]
6465
proxies = ... # type: Optional[MutableMapping[Text, Text]]
6566
hooks = ... # type: Optional[MutableMapping[Text, Callable[[Request], Any]]]
6667
params = ... # type: Union[None, bytes, MutableMapping[Text, Text]]
@@ -82,7 +83,7 @@ class Session(SessionRedirectMixin):
8283
headers: Optional[MutableMapping[Text, Text]] = ...,
8384
cookies: Union[None, RequestsCookieJar, MutableMapping[Text, Text]] = ...,
8485
files: Optional[MutableMapping[Text, IO]] = ...,
85-
auth: Union[None, Tuple[Text, Text], Callable[[Request], Request]] = ...,
86+
auth: Union[None, Tuple[Text, Text], AuthBase, Callable[[Request], Request]] = ...,
8687
timeout: Union[None, float, Tuple[float, float]] = ...,
8788
allow_redirects: Optional[bool] = ...,
8889
proxies: Optional[MutableMapping[Text, Text]] = ...,

0 commit comments

Comments
 (0)