Mutating Session.proxies does not work

This was originally brought up at https://github.com/kennethreitz/requests/issues/2061. If you change `session.proxies` after calling `session.get` and call `session.get` again, it does not use the updated value. You have to do `session.get(proxies=session.proxies)`. 

Assumedly this is not difficult to fix, since setting the keyword argument does work. I didn't read the code, though. 


OK, I can't reproduce this locally. I made three requests through `mitmproxy`:

``` python
>>> import requests
>>> s = requests.Session()
>>> r = s.get('http://www.google.co.uk/', proxies={'http': 'http://localhost:8080'})
>>> s.proxies['http'] = 'http://localhost:8080'
>>> r = s.get('http://www.google.co.uk/')
>>> s.proxies['http'] = 'http://user:pass@localhost:8080'
>>> r = s.get('http://www.google.co.uk/')
```

The first two requests pass through mitmproxy without difficulty. The last one also does, and additionally provides an auth header.

Can you provide a reproducible code sample?


My case was the first request failed with 407. I don't remember if it was http or https. 


That should be irrelevant, I'd have thought. I'd like a code fragment that can reproduce it, if it's not too hard. =)


I would expect it to be irrelevant as well...


I am using https://github.com/conda/conda/blob/5871df053da8de3b6e6fd82969acac14be367911/conda/fetch.py#L210. I can see if I can cook up a more directed example tomorrow. But using conda a simple `conda search` with a proxy enabled should do the trick. You may need to do `conda search -c asmeurer` to get https. 


Uh...I wonder if `stream=True` causes a problem here.


Oh, unlikely. I actually was using https://github.com/conda/conda/blob/5871df053da8de3b6e6fd82969acac14be367911/conda/fetch.py#L77 when this happened. 


Ah, ok. Well, I think we really need to hit a small, reproducible test case to work out whether or not this is truly a problem down at the connection pool layer. I simply don't think it is.


Stepping through the code, it's not too hard to see what is going on. The proxies are obtained from https://github.com/kennethreitz/requests/blob/6c72509f5bb0e9cd8fad64a44ba99687ed044771/requests/sessions.py#L429, which doesn't pull the proxies from the Session object at all. In fact, I think if I tried setting the proxies on the session object and they weren't picked up from the environment somehow, that it wouldn't use them at all. 


Observe a few lines down: https://github.com/kennethreitz/requests/blob/6c72509f5bb0e9cd8fad64a44ba99687ed044771/requests/sessions.py#L442


It looks like that puts priority on the request setting, but I think that that is the environment proxies by the time it reaches that, so it always uses that. 

That's assuming I read the code correctly :)


Hang on: why is the proxy suddenly ending up in the environment? We're aware of the incorrect precedence (it's in #2018), but I don't understand why it would work on the first request but fail on the second.


@asmeurer is conda looking for proxies on its own? If so I would guess that they're getting the same proxies we are (which is why it would work on the first try -- they come out of the ) and then they're trying to update them but it still pulls from the env


Yes, you can set them in the `.condarc` configuration if you want. 


The proxies are probably ending up there because of https://github.com/conda/conda/blob/8902c5a6956192f64d1679fde7e42b0c4f7b223d/conda/connection.py#L60 (https://github.com/conda/conda/blob/8902c5a6956192f64d1679fde7e42b0c4f7b223d/conda/config.py#L245).


And in case any of that seems really stupid, it's worth reiterating:

![](https://camo.githubusercontent.com/8cc028227cacf0b51ad6a6a13b939559b1c0b200/687474703a2f2f69312e6b796d2d63646e2e636f6d2f70686f746f732f696d616765732f66616365626f6f6b2f3030302f3233342f3736352f6237652e6a7067)


@asmeurer I'm not sure what the issue linked has to do with the proxy problems. That said, I see that you're setting the `proxies` attributes on the Session instance [here](https://github.com/conda/conda/blob/96aaccefddce58850b07f414dce95192e6b1a697/conda/connection.py#L60). The easy solution to this bug is to simply also do `self.trust_env = False`. That will prevent us from loading from the environment each time. You're already loading proxies from the environment in `get_proxy_servers`. The only things you'll lose by setting `trust_env` to `False` are:
- setting `verify` based on `REQUESTS_CA_BUNDLE` or `CURL_CA_BUNDLE`
- using authentication defined in `.netrc`


Oh d'oh. That would be me not copying the url correctly. Try the updated comment. 


`.netrc` seems potentially important. I think my current fix (just passing the proxies through to `get`; I only ever call `get` twice) is an OK workaround. I just opened this issue because it was confusing and I don't think I should have to do it. 


> Oh d'oh. That would be me not copying the url correctly. Try the updated comment.

No worries. I wasn't sure if I was missing something =P

> .netrc seems potentially important

It can be. That's why I mentioned it as a downside :)

>  I just opened this issue because it was confusing and I don't think I should have to do it. 

I agree. I don't think you should have to either. It seems, however, that this is a duplicate of #2018 after all. @Lukasa should we close this in deference to #2018?


Agreed, I think us fixing our proxy source priorities should fix your problem, @asmeurer. =)


Yes, I think so.