Skip to content

Conversation

@cjac
Copy link

@cjac cjac commented Oct 24, 2025

This commit addresses issue #5000 by ensuring that the no_proxy directive is respected, whether it's provided directly within the proxies dictionary passed to request functions or set as the NO_PROXY environment variable.

Previously, no_proxy in the proxies dictionary was not fully honored, and the NO_PROXY environment variable was not always checked when a proxies dictionary was provided.

This change includes:

  1. src/requests/sessions.py: Modified the Session.send method to check the no_proxy key within the kwargs['proxies'] dictionary. If the request URL matches any pattern in the no_proxy list, the proxies are cleared for that request.
  2. src/requests/utils.py: Updated the select_proxy function to check the NO_PROXY environment variable using should_bypass_proxies before selecting a proxy from the provided proxies dictionary.
  3. tests/test_requests.py: Added new test cases (test_no_proxy_in_proxies_dict, test_no_proxy_star_in_proxies_dict, test_no_proxy_not_matching_in_proxies_dict) to verify that no_proxy within the proxies dictionary works as expected, using mocks to check if the proxy is bypassed.
  4. tests/test_utils.py: Added new test cases (test_select_proxy_with_no_proxy) to ensure the NO_PROXY environment variable is correctly handled by select_proxy.

These changes ensure consistent behavior for proxy bypass logic, regardless of how the proxy settings are configured.

Closes #5000

This commit addresses issue psf#5000 by ensuring that the `no_proxy` directive is respected, whether it's provided directly within the `proxies` dictionary passed to request functions or set as the `NO_PROXY` environment variable.

Previously, `no_proxy` in the `proxies` dictionary was not fully honored, and the `NO_PROXY` environment variable was not always checked when a `proxies` dictionary was provided.

This change includes:

1.  **`src/requests/sessions.py`**: Modified the `Session.send` method to check the `no_proxy` key within the `kwargs['proxies']` dictionary. If the request URL matches any pattern in the `no_proxy` list, the proxies are cleared for that request.
2.  **`src/requests/utils.py`**: Updated the `select_proxy` function to check the `NO_PROXY` environment variable using `should_bypass_proxies` before selecting a proxy from the provided `proxies` dictionary.
3.  **`tests/test_requests.py`**: Added new test cases (`test_no_proxy_in_proxies_dict`, `test_no_proxy_star_in_proxies_dict`, `test_no_proxy_not_matching_in_proxies_dict`) to verify that `no_proxy` within the `proxies` dictionary works as expected, using mocks to check if the proxy is bypassed.
4.  **`tests/test_utils.py`**: Added new test cases (`test_select_proxy_with_no_proxy`) to ensure the `NO_PROXY` environment variable is correctly handled by `select_proxy`.

These changes ensure consistent behavior for proxy bypass logic, regardless of how the proxy settings are configured.

Closes psf#5000
@cjac cjac force-pushed the fix-no-proxy-2025-10-24 branch from ed6f3a0 to a784de5 Compare October 24, 2025 20:58
@cjac cjac marked this pull request as ready for review October 24, 2025 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

no_proxy setting ignores the proxies dictionary

1 participant