-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
requests: add more accurate stubs for certain fields of the Session object #1504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the detailed comments and documentation links!
@@ -16,6 +16,7 @@ from . import adapters | |||
from . import status_codes | |||
|
|||
BaseAdapter = adapters.BaseAdapter | |||
AuthBase = auth.AuthBase |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem to exist at runtime.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's probably me guessing the wrong way to handle an error I was getting. :/ I initially tried using type: Union[None, Tuple[Text, Text], auth.AuthBase, Callable[[Request], Request]]
, which gave me an error (Invalid type "requests.sessions.Session.auth"
). My fix was presumably wrong -- do you know what the right one is? Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's weird, I would have expected that code to work. I don't have more time to look right now though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, that's because you were doing that in the context of a class that also has an auth
attribute. You can work around it by making an alias like _auth
referring to the auth
module.
timeout: Union[None, float, Tuple[float, float]] = ..., | ||
allow_redirects: Optional[bool] = ..., | ||
proxies: Optional[MutableMapping[Text, Text]] = ..., | ||
hooks: Optional[MutableMapping[Text, Callable[[Request], Any]]] = ..., | ||
stream: Optional[bool] = ..., | ||
verify: Optional[bool] = ..., | ||
verify: Union[None, bool, Text] = ..., |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does a PathLike work too in Python > 3.6?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh, I'm not sure. I think it gets passed to some normal filesystem functions within requests[1] and then eventually gets used by urllib3 to call the stdlib's ssl.SSLContext.load_verify_locations as the first or second argument[2]. If I believe typeshed[3], it looks like that's just a str. I don't have Python > 3.6 conveniently available, so I haven't tried actually passing PathLike and seeing if it works.
[1] https://github.com/requests/requests/blob/master/requests/adapters.py#L224
[2] https://github.com/shazow/urllib3/blob/master/urllib3/util/ssl_.py#L311
[3] https://github.com/python/typeshed/blob/master/stdlib/3/ssl.pyi#L206
This adds more relaxed and accurate stubs for Session.verify and Session.auth, as described in the respective commit messages.