Skip to content

Commit 05eb7d8

Browse files
mkurnikovpradyunsg
authored andcommitted
Add type annotations for pip._internal.req (#6063)
1 parent 410072b commit 05eb7d8

File tree

7 files changed

+315
-103
lines changed

7 files changed

+315
-103
lines changed

src/pip/_internal/req/__init__.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
from .req_set import RequirementSet
77
from .req_file import parse_requirements
88
from pip._internal.utils.logging import indent_log
9+
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
910

11+
if MYPY_CHECK_RUNNING:
12+
from typing import List, Sequence # noqa: F401
1013

1114
__all__ = [
1215
"RequirementSet", "InstallRequirement",
@@ -16,8 +19,13 @@
1619
logger = logging.getLogger(__name__)
1720

1821

19-
def install_given_reqs(to_install, install_options, global_options=(),
20-
*args, **kwargs):
22+
def install_given_reqs(
23+
to_install, # type: List[InstallRequirement]
24+
install_options, # type: List[str]
25+
global_options=(), # type: Sequence[str]
26+
*args, **kwargs
27+
):
28+
# type: (...) -> List[InstallRequirement]
2129
"""
2230
Install everything in the given list.
2331

src/pip/_internal/req/constructors.py

+57-27
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,17 @@
2525
from pip._internal.models.link import Link
2626
from pip._internal.req.req_install import InstallRequirement
2727
from pip._internal.utils.misc import is_installable_dir
28+
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
2829
from pip._internal.vcs import vcs
2930
from pip._internal.wheel import Wheel
3031

32+
if MYPY_CHECK_RUNNING:
33+
from typing import ( # noqa: F401
34+
Optional, Tuple, Set, Any, Mapping, Union, Text
35+
)
36+
from pip._internal.cache import WheelCache # noqa: F401
37+
38+
3139
__all__ = [
3240
"install_req_from_editable", "install_req_from_line",
3341
"parse_editable"
@@ -38,6 +46,7 @@
3846

3947

4048
def _strip_extras(path):
49+
# type: (str) -> Tuple[str, Optional[str]]
4150
m = re.match(r'^(.+)(\[[^\]]+\])$', path)
4251
extras = None
4352
if m:
@@ -50,6 +59,7 @@ def _strip_extras(path):
5059

5160

5261
def parse_editable(editable_req):
62+
# type: (str) -> Tuple[Optional[str], str, Optional[Set[str]]]
5363
"""Parses an editable requirement into:
5464
- a requirement name
5565
- an URL
@@ -115,6 +125,7 @@ def parse_editable(editable_req):
115125

116126

117127
def deduce_helpful_msg(req):
128+
# type: (str) -> str
118129
"""Returns helpful msg in case requirements file does not exist,
119130
or cannot be parsed.
120131
@@ -135,7 +146,7 @@ def deduce_helpful_msg(req):
135146
" the packages specified within it."
136147
except RequirementParseError:
137148
logger.debug("Cannot parse '%s' as requirements \
138-
file" % (req), exc_info=1)
149+
file" % (req), exc_info=True)
139150
else:
140151
msg += " File '%s' does not exist." % (req)
141152
return msg
@@ -145,9 +156,15 @@ def deduce_helpful_msg(req):
145156

146157

147158
def install_req_from_editable(
148-
editable_req, comes_from=None, use_pep517=None, isolated=False,
149-
options=None, wheel_cache=None, constraint=False
159+
editable_req, # type: str
160+
comes_from=None, # type: Optional[str]
161+
use_pep517=None, # type: Optional[bool]
162+
isolated=False, # type: bool
163+
options=None, # type: Optional[Mapping[Text, Any]]
164+
wheel_cache=None, # type: Optional[WheelCache]
165+
constraint=False # type: bool
150166
):
167+
# type: (...) -> InstallRequirement
151168
name, url, extras_override = parse_editable(editable_req)
152169
if url.startswith('file:'):
153170
source_dir = url_to_path(url)
@@ -175,9 +192,15 @@ def install_req_from_editable(
175192

176193

177194
def install_req_from_line(
178-
name, comes_from=None, use_pep517=None, isolated=False, options=None,
179-
wheel_cache=None, constraint=False
195+
name, # type: str
196+
comes_from=None, # type: Optional[Union[str, InstallRequirement]]
197+
use_pep517=None, # type: Optional[bool]
198+
isolated=False, # type: bool
199+
options=None, # type: Optional[Mapping[Text, Any]]
200+
wheel_cache=None, # type: Optional[WheelCache]
201+
constraint=False # type: bool
180202
):
203+
# type: (...) -> InstallRequirement
181204
"""Creates an InstallRequirement from a name, which might be a
182205
requirement, directory containing 'setup.py', filename, or URL.
183206
"""
@@ -186,24 +209,24 @@ def install_req_from_line(
186209
else:
187210
marker_sep = ';'
188211
if marker_sep in name:
189-
name, markers = name.split(marker_sep, 1)
190-
markers = markers.strip()
191-
if not markers:
212+
name, markers_as_string = name.split(marker_sep, 1)
213+
markers_as_string = markers_as_string.strip()
214+
if not markers_as_string:
192215
markers = None
193216
else:
194-
markers = Marker(markers)
217+
markers = Marker(markers_as_string)
195218
else:
196219
markers = None
197220
name = name.strip()
198-
req = None
221+
req_as_string = None
199222
path = os.path.normpath(os.path.abspath(name))
200223
link = None
201-
extras = None
224+
extras_as_string = None
202225

203226
if is_url(name):
204227
link = Link(name)
205228
else:
206-
p, extras = _strip_extras(path)
229+
p, extras_as_string = _strip_extras(path)
207230
looks_like_dir = os.path.isdir(p) and (
208231
os.path.sep in name or
209232
(os.path.altsep is not None and os.path.altsep in name) or
@@ -234,34 +257,37 @@ def install_req_from_line(
234257
# wheel file
235258
if link.is_wheel:
236259
wheel = Wheel(link.filename) # can raise InvalidWheelFilename
237-
req = "%s==%s" % (wheel.name, wheel.version)
260+
req_as_string = "%s==%s" % (wheel.name, wheel.version)
238261
else:
239262
# set the req to the egg fragment. when it's not there, this
240263
# will become an 'unnamed' requirement
241-
req = link.egg_fragment
264+
req_as_string = link.egg_fragment
242265

243266
# a requirement specifier
244267
else:
245-
req = name
268+
req_as_string = name
246269

247-
if extras:
248-
extras = Requirement("placeholder" + extras.lower()).extras
270+
if extras_as_string:
271+
extras = Requirement("placeholder" + extras_as_string.lower()).extras
249272
else:
250273
extras = ()
251-
if req is not None:
274+
if req_as_string is not None:
252275
try:
253-
req = Requirement(req)
276+
req = Requirement(req_as_string)
254277
except InvalidRequirement:
255-
if os.path.sep in req:
278+
if os.path.sep in req_as_string:
256279
add_msg = "It looks like a path."
257-
add_msg += deduce_helpful_msg(req)
258-
elif '=' in req and not any(op in req for op in operators):
280+
add_msg += deduce_helpful_msg(req_as_string)
281+
elif ('=' in req_as_string and
282+
not any(op in req_as_string for op in operators)):
259283
add_msg = "= is not a valid operator. Did you mean == ?"
260284
else:
261285
add_msg = ""
262286
raise InstallationError(
263-
"Invalid requirement: '%s'\n%s" % (req, add_msg)
287+
"Invalid requirement: '%s'\n%s" % (req_as_string, add_msg)
264288
)
289+
else:
290+
req = None
265291

266292
return InstallRequirement(
267293
req, comes_from, link=link, markers=markers,
@@ -273,12 +299,16 @@ def install_req_from_line(
273299
)
274300

275301

276-
def install_req_from_req(
277-
req, comes_from=None, isolated=False, wheel_cache=None,
278-
use_pep517=None
302+
def install_req_from_req_string(
303+
req_string, # type: str
304+
comes_from=None, # type: Optional[InstallRequirement]
305+
isolated=False, # type: bool
306+
wheel_cache=None, # type: Optional[WheelCache]
307+
use_pep517=None # type: Optional[bool]
279308
):
309+
# type: (...) -> InstallRequirement
280310
try:
281-
req = Requirement(req)
311+
req = Requirement(req_string)
282312
except InvalidRequirement:
283313
raise InstallationError("Invalid requirement: '%s'" % req)
284314

0 commit comments

Comments
 (0)