Skip to content

Commit 5229322

Browse files
committed
Make proxy parameter override environment proxy
1 parent 5f85667 commit 5229322

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

news/10685.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make the ``--proxy`` parameter take precedence over environment variables.

src/pip/_internal/cli/req_command.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ def _build_session(
148148
"http": options.proxy,
149149
"https": options.proxy,
150150
}
151+
session.trust_env = False
151152

152153
# Determine if we can prompt the user for authentication or not
153154
session.auth.prompting = not options.no_input

tests/functional/test_proxy.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from typing import Any, Dict, Optional
2+
3+
import proxy
4+
import pytest
5+
from proxy.http.proxy import HttpProxyBasePlugin
6+
7+
from tests.lib import PipTestEnvironment
8+
from tests.lib.path import Path
9+
10+
11+
class AccessLogPlugin(HttpProxyBasePlugin):
12+
def on_access_log(self, context: Dict[str, Any]) -> Optional[Dict[str, Any]]:
13+
print(context)
14+
return super().on_access_log(context)
15+
16+
17+
@pytest.mark.network
18+
def test_proxy_overrides_env(
19+
script: PipTestEnvironment, monkeypatch: pytest.MonkeyPatch
20+
) -> None:
21+
monkeypatch.setenv("http_proxy", "127:0.0.1:8888")
22+
monkeypatch.setenv("https_proxy", "127:0.0.1:8888")
23+
with proxy.Proxy(
24+
port=8899,
25+
), proxy.Proxy(plugins=[AccessLogPlugin], port=8888):
26+
result = script.pip(
27+
"download",
28+
"--proxy",
29+
"http://127.0.0.1:8899",
30+
"--trusted-host",
31+
"127.0.0.1",
32+
"-d",
33+
"pip_downloads",
34+
"INITools==0.1",
35+
)
36+
result.did_create(Path("scratch") / "pip_downloads" / "INITools-0.1.tar.gz")
37+
assert "CONNECT" not in result.stdout

tests/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ virtualenv < 20.0
1111
werkzeug
1212
wheel
1313
tomli-w
14+
proxy.py

0 commit comments

Comments
 (0)