Repository: psf/requests
File: requests/sessions.py (Python)

Code before:

        return

    def rebuild_proxies(self, prepared_request, proxies):
        """
        This method re-evaluates the proxy configuration by considering the
        environment variables. If we are redirected to a URL covered by
        NO_PROXY, we strip the proxy configuration. Otherwise, we set missing
        proxy keys for this URL (in case they were stripped by a previous
        redirect).

        This method also replaces the Proxy-Authorization header where
        necessary.
        """
        headers = prepared_request.headers
        url = prepared_request.url
        new_proxies = {}

        if not should_bypass_proxies(url):
            environ_proxies = get_environ_proxies(url)
            scheme = urlparse(url).scheme

            try:
                new_proxies.setdefault(scheme, environ_proxies[scheme])
            except KeyError:
                pass

        if 'Proxy-Authorization' in headers:
            del headers['Proxy-Authorization']

        try:
            username, password = get_auth_from_url(new_proxies[scheme])
            if username and password:
                headers['Proxy-Authorization'] = _basic_auth_str(
                    username, password
                )
        except KeyError:
            pass

        return new_proxies


class Session(SessionRedirectMixin):
    """A Requests session.

    Provides cookie persistence, connection-pooling, and configuration.

    Basic Usage::

      >>> import requests
      >>> s = requests.Session()

Review comment (suggestion):
The `KeyError` is for `environ_proxies` right? Meaning we only want to set the default for `scheme` if `environ_proxies[scheme]` exists, right? Would something along these lines be amenable?

``` python
environ_proxies = get_environ_proxies(url)
scheme = urlparse(url).scheme

proxy = environ_proxies.get(scheme)
if proxy:
   new_proxies.setdefault(scheme, proxy)
```


Code after:

        return

    def rebuild_proxies(self, prepared_request, proxies):
        """
        This method re-evaluates the proxy configuration by considering the
        environment variables. If we are redirected to a URL covered by
        NO_PROXY, we strip the proxy configuration. Otherwise, we set missing
        proxy keys for this URL (in case they were stripped by a previous
        redirect).

        This method also replaces the Proxy-Authorization header where
        necessary.
        """
        headers = prepared_request.headers
        url = prepared_request.url
        new_proxies = {}

        if not should_bypass_proxies(url):
            environ_proxies = get_environ_proxies(url)
            scheme = urlparse(url).scheme

            proxy = environ_proxies.get(scheme)

            if proxy:
                new_proxies.setdefault(scheme, environ_proxies[scheme])

        if 'Proxy-Authorization' in headers:
            del headers['Proxy-Authorization']

        try:
            username, password = get_auth_from_url(new_proxies[scheme])
        except KeyError:
            username, password = None, None

        if username and password:
            headers['Proxy-Authorization'] = _basic_auth_str(username, password)

        return new_proxies


class Session(SessionRedirectMixin):
    """A Requests session.

    Provides cookie persistence, connection-pooling, and configuration.

    Basic Usage::

      >>> import requests
      >>> s = requests.Session()
      >>> s.get('http://httpbin.org/get')