-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
5677: Respect variable precedence in session #5735
base: v2.27.x
Are you sure you want to change the base?
Conversation
hooks = request.hooks | ||
|
||
# Merge with environment variables | ||
settings = self.merge_environment_settings(request.url, proxies, stream, cert) | ||
kwargs.update(settings) |
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.
Personally I don't like to reuse kwargs
too much, but it reduce the amount of changes
I propose the following change at line: Line 530 in 913880c
Replace it by: proxies = proxies or self.proxies This change will make this hierarchy |
I've added a test case, and changed the strategy. Now we are merging the session and request settings before we ever look into the environment, that way the precedence becomes more explicit. Can you test this patch with pypa/pip#9691 @junqfisica ? |
@mateusduboli Yes, your commit 57ddecd, also fixes the problem with pypa/pip#9691. :) |
in fact, the solutions of you two are similar: merging |
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.
Sorry, my test result is that no_proxy
is still valid. There is no problem with your modification.
So why rebuild_proxies()
is here before?
kwargs.setdefault('stream', self.stream) | ||
kwargs.setdefault('verify', self.verify) | ||
kwargs.setdefault('cert', self.cert) | ||
kwargs.setdefault('proxies', self.rebuild_proxies(request, self.proxies)) |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
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.
Before #5681, the send
method was not using the environment variables at all, this was here to ensure that those would be read at some point.
However, this MR also moves the call for merge_environment_settings
to send
, thus rebuilding proxies is not necessary. Before, it was only issued in the request
method.
@CrazyBoyFeng Do we need any additional review on this, so it can be merged? |
I used this fix with |
@sigmavirus24, can you take a look at these changes? |
@nateprewitt, can you maybe take a look at this PR before the next release? We would really appreciate this bugfix included in the next available version of requests. Thank you. |
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.
Hope this fix can be confirmed as soon as possible. It makes a lot of software does not work with custom proxy while the system proxy is set, including pip
.
In addition, it is recommended that type "fix #5677" in the PR description to associate this PR with the issue.
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.
These changes will make the hierarchy request kwargs
-> Session args
-> environment
to be respected. This also solves problems with pip as discussed here
@nateprewitt thanks for taking a look at this! I'm wondering why this is a tagged as a I'm guessing if we not consider it as such it would be less hassle to merge it, could you clarify it better? |
This should be considered as a bug, I think. |
Hi @mateusduboli, the reason we have this marked as a breaking change is we're doing a pretty fundamental change to the Session API here. The last PR (#5888) that was introduced for this had some non-trivial impact that's made Requests 2.26.0 unusable for a number of our dependents. It arguably shouldn't have been merged to begin with. I know we've discussed the session precedence issue more than once previously, but I wasn't able to find actual issue numbers. I think we need to reevaluate how these interfaces actually operate going into a new major version. Until then though, this has been the behavior of |
@CrazyBoyFeng I'm reading over the pip issue more and while I agree this far from optimal, as I've stated above, it's kind of where we are. We would like to fix this but can't currently. For the interim, I'm failing to see why e.g. Would become: def request(self, method, url, *args, **kwargs):
# type: (str, str, *Any, **Any) -> Response
# Allow setting a default timeout on a session
kwargs.setdefault("timeout", self.timeout)
+ if self.proxies:
+ kwargs.setdefault("proxies", self.proxies)
# Dispatch the actual request
return super().request(method, url, *args, **kwargs) |
This change will make the hierarchy request **kwargs -> Session args -> environment to be respected. This also solves problems with pip as discussed [here](pypa/pip#9691 (comment)) and psf#5735
After waiting a long time for a fix on the side of the request that could fix pip install issues with proxy as already discussed in pypa#9691. It seems that such changes as mentioned in psf/requests#5735 will take a while to happen, probably only on version 3 of the request. Therefore, I'm suggesting a change on the pip side as already proposed in [issuecomment-79051687]pypa#9568 (comment) and [issuecomment-939373868]psf/requests#5735 (comment) I think it's important that pip address this issue asap since the newer versions of pip are not working properly behind a proxy.
Session#merge_environment_variables
fromSession#request
toSession#send
to make it consistentSession#send
change variable precedence to (higher precedence first)kwargs
->session args
->environment
.