Skip to content

Commit 7eebf11

Browse files
committed
Cast to real responses in test network utils, download, and auth
1 parent ab0bcc3 commit 7eebf11

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

tests/unit/test_network_auth.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import subprocess
66
import sys
77
from collections.abc import Iterable
8-
from typing import Any
8+
from typing import TYPE_CHECKING, Any, cast
99

1010
import pytest
1111

@@ -14,6 +14,11 @@
1414

1515
from tests.lib.requests_mocks import MockConnection, MockRequest, MockResponse
1616

17+
if TYPE_CHECKING:
18+
from requests.models import Response
19+
else:
20+
from pip._vendor.requests.models import Response
21+
1722

1823
@pytest.fixture(autouse=True)
1924
def reset_keyring() -> Iterable[None]:
@@ -320,7 +325,7 @@ def _send(sent_req: MockRequest, **kwargs: Any) -> MockResponse:
320325
resp.status_code = 401
321326
resp.connection = connection
322327

323-
auth.handle_401(resp)
328+
auth.handle_401(cast("Response", resp))
324329

325330
if expect_save:
326331
assert keyring.saved_passwords == [("example.com", creds[0], creds[1])]
@@ -536,7 +541,7 @@ def _send(sent_req: MockRequest, **kwargs: Any) -> MockResponse:
536541
resp.status_code = 401
537542
resp.connection = connection
538543

539-
auth.handle_401(resp)
544+
auth.handle_401(cast("Response", resp))
540545

541546
if expect_save:
542547
assert keyring.saved_passwords == [("example.com", creds[0], creds[1])]

tests/unit/test_network_download.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import sys
55
from pathlib import Path
6+
from typing import TYPE_CHECKING, cast
67
from unittest.mock import MagicMock, call, patch
78

89
import pytest
@@ -21,6 +22,11 @@
2122

2223
from tests.lib.requests_mocks import MockResponse
2324

25+
if TYPE_CHECKING:
26+
from requests.models import Response
27+
else:
28+
from pip._vendor.requests.models import Response
29+
2430

2531
@pytest.mark.parametrize(
2632
"url, headers, from_cache, range_start, expected",
@@ -91,9 +97,9 @@ def test_log_download(
9197
if from_cache:
9298
resp.from_cache = from_cache
9399
link = Link(url)
94-
total_length = _get_http_response_size(resp)
100+
total_length = _get_http_response_size(cast("Response", resp))
95101
_log_download(
96-
resp,
102+
cast("Response", resp),
97103
link,
98104
progress_bar="on",
99105
total_length=total_length,

tests/unit/test_network_utils.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
from typing import TYPE_CHECKING, cast
2+
13
import pytest
24

35
from pip._internal.exceptions import NetworkConnectionError
46
from pip._internal.network.utils import raise_for_status
57

68
from tests.lib.requests_mocks import MockResponse
79

10+
if TYPE_CHECKING:
11+
from requests.models import Response
12+
else:
13+
from pip._vendor.requests.models import Response
14+
815

916
@pytest.mark.parametrize(
1017
"status_code, error_type",
@@ -20,7 +27,7 @@ def test_raise_for_status_raises_exception(status_code: int, error_type: str) ->
2027
resp.url = "http://www.example.com/whatever.tgz"
2128
resp.reason = "Network Error"
2229
with pytest.raises(NetworkConnectionError) as excinfo:
23-
raise_for_status(resp)
30+
raise_for_status(cast("Response", resp))
2431
assert str(excinfo.value) == (
2532
f"{status_code} {error_type}: Network Error for url:"
2633
" http://www.example.com/whatever.tgz"
@@ -33,4 +40,4 @@ def test_raise_for_status_does_not_raises_exception() -> None:
3340
resp.status_code = 201
3441
resp.url = "http://www.example.com/whatever.tgz"
3542
resp.reason = "No error"
36-
raise_for_status(resp)
43+
raise_for_status(cast("Response", resp))

0 commit comments

Comments
 (0)