Skip to content

Commit a7b6ba6

Browse files
committed
Lazy import pip.network in req_file.py
This notably helps pip freeze which doesn't need the network unless a URL is given as a requirement file.
1 parent 79a91ab commit a7b6ba6

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/pip/_internal/req/req_file.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@
2525
from pip._internal.cli import cmdoptions
2626
from pip._internal.exceptions import InstallationError, RequirementsFileParseError
2727
from pip._internal.models.search_scope import SearchScope
28-
from pip._internal.network.session import PipSession
29-
from pip._internal.network.utils import raise_for_status
3028
from pip._internal.utils.encoding import auto_decode
3129
from pip._internal.utils.urls import get_url_scheme
3230

3331
if TYPE_CHECKING:
3432
from pip._internal.index.package_finder import PackageFinder
33+
from pip._internal.network.session import PipSession
3534

3635
__all__ = ["parse_requirements"]
3736

@@ -133,7 +132,7 @@ def __init__(
133132

134133
def parse_requirements(
135134
filename: str,
136-
session: PipSession,
135+
session: "PipSession",
137136
finder: Optional["PackageFinder"] = None,
138137
options: Optional[optparse.Values] = None,
139138
constraint: bool = False,
@@ -210,7 +209,7 @@ def handle_option_line(
210209
lineno: int,
211210
finder: Optional["PackageFinder"] = None,
212211
options: Optional[optparse.Values] = None,
213-
session: Optional[PipSession] = None,
212+
session: Optional["PipSession"] = None,
214213
) -> None:
215214
if opts.hashes:
216215
logger.warning(
@@ -278,7 +277,7 @@ def handle_line(
278277
line: ParsedLine,
279278
options: Optional[optparse.Values] = None,
280279
finder: Optional["PackageFinder"] = None,
281-
session: Optional[PipSession] = None,
280+
session: Optional["PipSession"] = None,
282281
) -> Optional[ParsedRequirement]:
283282
"""Handle a single parsed requirements line; This can result in
284283
creating/yielding requirements, or updating the finder.
@@ -321,7 +320,7 @@ def handle_line(
321320
class RequirementsFileParser:
322321
def __init__(
323322
self,
324-
session: PipSession,
323+
session: "PipSession",
325324
line_parser: LineParser,
326325
) -> None:
327326
self._session = session
@@ -526,7 +525,7 @@ def expand_env_variables(lines_enum: ReqFileLines) -> ReqFileLines:
526525
yield line_number, line
527526

528527

529-
def get_file_content(url: str, session: PipSession) -> Tuple[str, str]:
528+
def get_file_content(url: str, session: "PipSession") -> Tuple[str, str]:
530529
"""Gets the content of a file; it may be a filename, file: URL, or
531530
http: URL. Returns (location, content). Content is unicode.
532531
Respects # -*- coding: declarations on the retrieved files.
@@ -538,6 +537,9 @@ def get_file_content(url: str, session: PipSession) -> Tuple[str, str]:
538537

539538
# Pip has special support for file:// URLs (LocalFSAdapter).
540539
if scheme in ["http", "https", "file"]:
540+
# Delay importing heavy network modules until absolutely necessary.
541+
from pip._internal.network.utils import raise_for_status
542+
541543
resp = session.get(url)
542544
raise_for_status(resp)
543545
return resp.url, resp.text

0 commit comments

Comments
 (0)