-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Regression in requests hooks stubs #7776
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
Comments
I think the changes to the stub that means this no longer type-check were made slightly further back, in I'm not familiar with with the |
The change is correct; requests allows hooks to either be a list or a function (which it'll turn into a list): Unfortunately, this makes typing an issue; what if you set a function as a hook and then call s.hooks['response'] = lambda ...
s.hooks['response'].append(...) are perfectly valid uses of the requests API separately, but will fail when used together. This is more of a question for the requests API design, but my two cents are to revert #7094 and say "if you're using requests + typeshed, you MUST operate on the list" (cc @UncleOwen). To completely override all previously set hooks, this would then require the user doing explicitly s.hooks['response'] = [lambda ...] which I think is a fine limitation EDIT: OH! Actually, this is completely broken for sessions, because |
Indeed, it's an immutable We could make it mutable when accessed through |
Because that's how you're supposed to do it for sessions: https://docs.python-requests.org/en/latest/user/advanced/#event-hooks
Changing |
That mutates it through the attribute, but doesn't specify it as an argument when constructing the |
|
Ah, so the two types are already different. Indeed, we just need to make |
Fixes #7776 Mutating hooks, as in `session.hooks['response'] = ...`, should work. Reassigning it like `session.hooks = ...` is probably a bad idea, so it will always be a `dict`.
Fixes #7776 Mutating hooks, as in `session.hooks['response'] = ...`, should work. Reassigning it like `session.hooks = ...` is probably a bad idea, so it will always be a `dict`.
@Akuli thank you for the fix ! :) |
Since f330e12 it's not possible to add new hooks to
requests.Session
.According to the documentation, the recommended way to add a hook to every request is by adding them to
requests.Session.hook
:This snippet now fails with the following error:
Example project
source: https://gitlab.com/cki-project/cki-lib/-/blob/main/cki_lib/session.py#L47
CI job: https://gitlab.com/cki-project/cki-lib/-/jobs/2407190024
The text was updated successfully, but these errors were encountered: