Skip to content

Commit 688bc1e

Browse files
committed
Move InstallRequirement.from_req to constructors module
1 parent a5a07fe commit 688bc1e

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

src/pip/_internal/req/constructors.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
is_archive_file, is_url, path_to_url, url_to_path,
2323
)
2424
from pip._internal.exceptions import InstallationError
25+
from pip._internal.models.index import PyPI, TestPyPI
2526
from pip._internal.models.link import Link
2627
from pip._internal.req.req_install import InstallRequirement
2728
from pip._internal.utils.misc import is_installable_dir
@@ -270,3 +271,28 @@ def install_req_from_line(
270271
constraint=constraint,
271272
extras=extras,
272273
)
274+
275+
276+
def install_req_from_req(
277+
req, comes_from=None, isolated=False, wheel_cache=None
278+
):
279+
try:
280+
req = Requirement(req)
281+
except InvalidRequirement:
282+
raise InstallationError("Invalid requirement: '%s'" % req)
283+
284+
domains_not_allowed = [
285+
PyPI.file_storage_domain,
286+
TestPyPI.file_storage_domain,
287+
]
288+
if req.url and comes_from.link.netloc in domains_not_allowed:
289+
# Explicitly disallow pypi packages that depend on external urls
290+
raise InstallationError(
291+
"Packages installed from PyPI cannot depend on packages "
292+
"which are not also hosted on PyPI.\n"
293+
"%s depends on %s " % (comes_from.name, req)
294+
)
295+
296+
return InstallRequirement(
297+
req, comes_from, isolated=isolated, wheel_cache=wheel_cache
298+
)

src/pip/_internal/req/req_install.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from distutils.util import change_root
1010

1111
from pip._vendor import pkg_resources, six
12-
from pip._vendor.packaging.requirements import InvalidRequirement, Requirement
12+
from pip._vendor.packaging.requirements import Requirement
1313
from pip._vendor.packaging.utils import canonicalize_name
1414
from pip._vendor.packaging.version import Version
1515
from pip._vendor.packaging.version import parse as parse_version
@@ -21,7 +21,6 @@
2121
from pip._internal.locations import (
2222
PIP_DELETE_MARKER_FILENAME, running_under_virtualenv,
2323
)
24-
from pip._internal.models.index import PyPI, TestPyPI
2524
from pip._internal.models.link import Link
2625
from pip._internal.pyproject import load_pyproject_toml
2726
from pip._internal.req.req_uninstall import UninstallPathSet
@@ -125,29 +124,6 @@ def __init__(self, req, comes_from, source_dir=None, editable=False,
125124
# but after loading this flag should be treated as read only.
126125
self.use_pep517 = None
127126

128-
# Constructors
129-
# TODO: Move these out of this class into custom methods.
130-
@classmethod
131-
def from_req(cls, req, comes_from=None, isolated=False, wheel_cache=None):
132-
try:
133-
req = Requirement(req)
134-
except InvalidRequirement:
135-
raise InstallationError("Invalid requirement: '%s'" % req)
136-
137-
domains_not_allowed = [
138-
PyPI.file_storage_domain,
139-
TestPyPI.file_storage_domain,
140-
]
141-
if req.url and comes_from.link.netloc in domains_not_allowed:
142-
# Explicitly disallow pypi packages that depend on external urls
143-
raise InstallationError(
144-
"Packages installed from PyPI cannot depend on packages "
145-
"which are not also hosted on PyPI.\n"
146-
"%s depends on %s " % (comes_from.name, req)
147-
)
148-
149-
return cls(req, comes_from, isolated=isolated, wheel_cache=wheel_cache)
150-
151127
def __str__(self):
152128
if self.req:
153129
s = str(self.req)

src/pip/_internal/resolve.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
BestVersionAlreadyInstalled, DistributionNotFound, HashError, HashErrors,
1919
UnsupportedPythonVersion,
2020
)
21-
from pip._internal.req.req_install import InstallRequirement
21+
from pip._internal.req.constructors import install_req_from_req
2222
from pip._internal.utils.logging import indent_log
2323
from pip._internal.utils.misc import dist_in_usersite, ensure_dir
2424
from pip._internal.utils.packaging import check_dist_requires_python
@@ -268,7 +268,7 @@ def _resolve_one(self, requirement_set, req_to_install):
268268
more_reqs = []
269269

270270
def add_req(subreq, extras_requested):
271-
sub_install_req = InstallRequirement.from_req(
271+
sub_install_req = install_req_from_req(
272272
str(subreq),
273273
req_to_install,
274274
isolated=self.isolated,

0 commit comments

Comments
 (0)