Skip to content

Commit

Permalink
Merge pull request #12563 from pradyunsg/cache-user-agent
Browse files Browse the repository at this point in the history
  • Loading branch information
uranusjr authored Apr 9, 2024
2 parents 31437d6 + 4f2ccfc commit 06d21db
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/pip/_internal/network/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import email.utils
import functools
import io
import ipaddress
import json
Expand Down Expand Up @@ -106,6 +107,7 @@ def looks_like_ci() -> bool:
return any(name in os.environ for name in CI_ENVIRONMENT_VARIABLES)


@functools.lru_cache(maxsize=1)
def user_agent() -> str:
"""
Return a string representing the user agent.
Expand Down
11 changes: 9 additions & 2 deletions tests/unit/test_network_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@

from pip import __version__
from pip._internal.models.link import Link
from pip._internal.network.session import CI_ENVIRONMENT_VARIABLES, PipSession
from pip._internal.network.session import (
CI_ENVIRONMENT_VARIABLES,
PipSession,
user_agent,
)


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


Expand Down Expand Up @@ -58,7 +65,7 @@ def test_user_agent__ci(

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


class TestPipSession:
Expand Down

0 comments on commit 06d21db

Please sign in to comment.