Skip to content

Commit 06d21db

Browse files
authored
Merge pull request #12563 from pradyunsg/cache-user-agent
2 parents 31437d6 + 4f2ccfc commit 06d21db

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/pip/_internal/network/session.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import email.utils
6+
import functools
67
import io
78
import ipaddress
89
import json
@@ -106,6 +107,7 @@ def looks_like_ci() -> bool:
106107
return any(name in os.environ for name in CI_ENVIRONMENT_VARIABLES)
107108

108109

110+
@functools.lru_cache(maxsize=1)
109111
def user_agent() -> str:
110112
"""
111113
Return a string representing the user agent.

tests/unit/test_network_session.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@
1010

1111
from pip import __version__
1212
from pip._internal.models.link import Link
13-
from pip._internal.network.session import CI_ENVIRONMENT_VARIABLES, PipSession
13+
from pip._internal.network.session import (
14+
CI_ENVIRONMENT_VARIABLES,
15+
PipSession,
16+
user_agent,
17+
)
1418

1519

1620
def get_user_agent() -> str:
21+
# These tests are testing the computation of the user agent, so we want to
22+
# avoid reusing cached values.
23+
user_agent.cache_clear()
1724
return PipSession().headers["User-Agent"]
1825

1926

@@ -58,7 +65,7 @@ def test_user_agent__ci(
5865

5966
def test_user_agent_user_data(monkeypatch: pytest.MonkeyPatch) -> None:
6067
monkeypatch.setenv("PIP_USER_AGENT_USER_DATA", "some_string")
61-
assert "some_string" in PipSession().headers["User-Agent"]
68+
assert "some_string" in get_user_agent()
6269

6370

6471
class TestPipSession:

0 commit comments

Comments
 (0)