Skip to content

Commit e8b735f

Browse files
committed
Move all vendored libraries with stubs into TYPE_CHECKING block
1 parent 63d785c commit e8b735f

File tree

17 files changed

+149
-54
lines changed

17 files changed

+149
-54
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ docs = [
9191
"sphinx-issues"
9292
]
9393

94-
# Libraries that are not required for pip to run,
94+
# Libraries that are not required for pip to run,
9595
# but are used in some optional features.
9696
all-optional = ["keyring"]
9797

src/pip/_internal/commands/install.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import shutil
88
import site
99
from optparse import SUPPRESS_HELP, Values
10+
from typing import TYPE_CHECKING
1011

1112
from pip._vendor.packaging.utils import canonicalize_name
12-
from pip._vendor.requests.exceptions import InvalidProxyURL
1313
from pip._vendor.rich import print_json
1414

1515
# Eagerly import self_outdated_check to avoid crashes. Otherwise,
@@ -60,6 +60,12 @@
6060
)
6161
from pip._internal.wheel_builder import build, should_build_for_install_command
6262

63+
if TYPE_CHECKING:
64+
# Vendored libraries with type stubs
65+
from requests.exceptions import InvalidProxyURL
66+
else:
67+
from pip._vendor.requests.exceptions import InvalidProxyURL
68+
6369
logger = getLogger(__name__)
6470

6571

src/pip/_internal/exceptions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
if TYPE_CHECKING:
2828
from hashlib import _Hash
2929

30-
from pip._vendor.requests.models import Request, Response
30+
# Vendored libraries with type stubs
31+
from requests.models import Request, Response
3132

3233
from pip._internal.metadata import BaseDistribution
3334
from pip._internal.network.download import _FileDownload

src/pip/_internal/index/collector.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@
1818
from html.parser import HTMLParser
1919
from optparse import Values
2020
from typing import (
21+
TYPE_CHECKING,
2122
Callable,
2223
NamedTuple,
2324
Protocol,
2425
)
2526

26-
from pip._vendor import requests
27-
from pip._vendor.requests import Response
28-
from pip._vendor.requests.exceptions import RetryError, SSLError
29-
3027
from pip._internal.exceptions import NetworkConnectionError
3128
from pip._internal.models.link import Link
3229
from pip._internal.models.search_scope import SearchScope
@@ -38,6 +35,15 @@
3835

3936
from .sources import CandidatesFromPage, LinkSource, build_source
4037

38+
if TYPE_CHECKING:
39+
# Vendored libraries with type stubs
40+
import requests
41+
from requests import Response
42+
from requests.exceptions import RetryError, SSLError
43+
else:
44+
from pip._vendor import requests
45+
from pip._vendor.requests.exceptions import RetryError, SSLError
46+
4147
logger = logging.getLogger(__name__)
4248

4349
ResponseHeaders = MutableMapping[str, str]

src/pip/_internal/locations/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pathlib
77
import sys
88
import sysconfig
9-
from typing import Any
9+
from typing import TYPE_CHECKING, Any
1010

1111
from pip._internal.models.scheme import SCHEME_KEYS, Scheme
1212
from pip._internal.utils.compat import WINDOWS
@@ -131,8 +131,13 @@ def _looks_like_red_hat_scheme() -> bool:
131131
(fortunately?) done quite unconditionally, so we create a default command
132132
object without any configuration to detect this.
133133
"""
134-
from distutils.command.install import install
135-
from distutils.dist import Distribution
134+
if TYPE_CHECKING:
135+
# Vendored libraries with type stubs
136+
from setuptools._distutils.command.install import install
137+
from setuptools._distutils.dist import Distribution
138+
else:
139+
from distutils.command.install import install
140+
from distutils.dist import Distribution
136141

137142
cmd: Any = install(Distribution())
138143
cmd.finalize_options()

src/pip/_internal/locations/_distutils.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,28 @@
1919
import logging
2020
import os
2121
import sys
22-
from distutils.cmd import Command as DistutilsCommand
2322
from distutils.command.install import SCHEME_KEYS
24-
from distutils.command.install import install as distutils_install_command
25-
from distutils.sysconfig import get_python_lib
23+
from typing import TYPE_CHECKING
2624

2725
from pip._internal.models.scheme import Scheme
2826
from pip._internal.utils.compat import WINDOWS
2927
from pip._internal.utils.virtualenv import running_under_virtualenv
3028

3129
from .base import get_major_minor_version
3230

31+
if TYPE_CHECKING:
32+
# Vendored libraries with type stubs
33+
from setuptools._distutils.cmd import Command as DistutilsCommand
34+
from setuptools._distutils.command.install import (
35+
install as distutils_install_command,
36+
)
37+
from setuptools._distutils.dist import Distribution # noqa: F401
38+
from setuptools._distutils.sysconfig import get_python_lib
39+
else:
40+
from distutils.command.install import install as distutils_install_command
41+
from distutils.sysconfig import get_python_lib
42+
43+
3344
logger = logging.getLogger(__name__)
3445

3546

@@ -46,7 +57,8 @@ def distutils_scheme(
4657
"""
4758
Return a distutils install scheme
4859
"""
49-
from distutils.dist import Distribution
60+
if not TYPE_CHECKING:
61+
from distutils.dist import Distribution
5062

5163
dist_args: dict[str, str | list[str]] = {"name": dist_name}
5264
if isolated:

src/pip/_internal/network/auth.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717
from functools import cache
1818
from os.path import commonprefix
1919
from pathlib import Path
20-
from typing import Any, NamedTuple
21-
22-
from pip._vendor.requests.auth import AuthBase, HTTPBasicAuth
23-
from pip._vendor.requests.models import Request, Response
24-
from pip._vendor.requests.utils import get_netrc_auth
20+
from typing import TYPE_CHECKING, Any, NamedTuple
2521

2622
from pip._internal.utils.logging import getLogger
2723
from pip._internal.utils.misc import (
@@ -33,6 +29,16 @@
3329
)
3430
from pip._internal.vcs.versioncontrol import AuthInfo
3531

32+
if TYPE_CHECKING:
33+
# Vendored libraries with type stubs
34+
from requests.auth import AuthBase, HTTPBasicAuth
35+
from requests.models import Request, Response
36+
from requests.utils import get_netrc_auth
37+
else:
38+
from pip._vendor.requests.auth import AuthBase, HTTPBasicAuth
39+
from pip._vendor.requests.utils import get_netrc_auth
40+
41+
3642
logger = getLogger(__name__)
3743

3844
KEYRING_DISABLED = False

src/pip/_internal/network/cache.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
from collections.abc import Generator
88
from contextlib import contextmanager
99
from datetime import datetime
10-
from typing import Any, BinaryIO, Callable
10+
from typing import TYPE_CHECKING, Any, BinaryIO, Callable
1111

1212
from pip._vendor.cachecontrol.cache import SeparateBodyBaseCache
1313
from pip._vendor.cachecontrol.caches import SeparateBodyFileCache
14-
from pip._vendor.requests.models import Response
1514

1615
from pip._internal.utils.filesystem import adjacent_tmp_file, replace
1716
from pip._internal.utils.misc import ensure_dir
1817

18+
if TYPE_CHECKING:
19+
# Vendored libraries with type stubs
20+
from requests.models import Response
21+
1922

2023
def is_from_cache(response: Response) -> bool:
2124
return getattr(response, "from_cache", False)

src/pip/_internal/network/download.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@
99
from collections.abc import Iterable, Mapping
1010
from dataclasses import dataclass
1111
from http import HTTPStatus
12-
from typing import BinaryIO
13-
14-
from pip._vendor.requests import PreparedRequest
15-
from pip._vendor.requests.models import Response
16-
from pip._vendor.urllib3 import HTTPResponse as URLlib3Response
17-
from pip._vendor.urllib3._collections import HTTPHeaderDict
18-
from pip._vendor.urllib3.exceptions import ReadTimeoutError
12+
from typing import TYPE_CHECKING, BinaryIO
1913

2014
from pip._internal.cli.progress_bars import BarType, get_download_progress_renderer
2115
from pip._internal.exceptions import IncompleteDownloadError, NetworkConnectionError
@@ -26,6 +20,19 @@
2620
from pip._internal.network.utils import HEADERS, raise_for_status, response_chunks
2721
from pip._internal.utils.misc import format_size, redact_auth_from_url, splitext
2822

23+
if TYPE_CHECKING:
24+
# Vendored libraries with type stubs
25+
from requests import PreparedRequest
26+
from requests.models import Response
27+
from urllib3 import HTTPResponse as URLlib3Response
28+
from urllib3._collections import HTTPHeaderDict
29+
from urllib3.exceptions import ReadTimeoutError
30+
else:
31+
from pip._vendor.requests import PreparedRequest
32+
from pip._vendor.urllib3 import HTTPResponse as URLlib3Response
33+
from pip._vendor.urllib3._collections import HTTPHeaderDict
34+
from pip._vendor.urllib3.exceptions import ReadTimeoutError
35+
2936
logger = logging.getLogger(__name__)
3037

3138

src/pip/_internal/network/lazy_wheel.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@
88
from collections.abc import Generator
99
from contextlib import contextmanager
1010
from tempfile import NamedTemporaryFile
11-
from typing import Any
11+
from typing import TYPE_CHECKING, Any
1212
from zipfile import BadZipFile, ZipFile
1313

1414
from pip._vendor.packaging.utils import canonicalize_name
15-
from pip._vendor.requests.models import CONTENT_CHUNK_SIZE, Response
1615

1716
from pip._internal.metadata import BaseDistribution, MemoryWheel, get_wheel_distribution
1817
from pip._internal.network.session import PipSession
1918
from pip._internal.network.utils import HEADERS, raise_for_status, response_chunks
2019

20+
if TYPE_CHECKING:
21+
# Vendored libraries with type stubs
22+
from requests.models import CONTENT_CHUNK_SIZE, Response
23+
else:
24+
from pip._vendor.requests.models import CONTENT_CHUNK_SIZE
25+
2126

2227
class HTTPRangeRequestUnsupported(Exception):
2328
pass

0 commit comments

Comments
 (0)