25
25
from pip ._internal .models .link import Link
26
26
from pip ._internal .req .req_install import InstallRequirement
27
27
from pip ._internal .utils .misc import is_installable_dir
28
+ from pip ._internal .utils .typing import MYPY_CHECK_RUNNING
28
29
from pip ._internal .vcs import vcs
29
30
from pip ._internal .wheel import Wheel
30
31
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
+
31
39
__all__ = [
32
40
"install_req_from_editable" , "install_req_from_line" ,
33
41
"parse_editable"
38
46
39
47
40
48
def _strip_extras (path ):
49
+ # type: (str) -> Tuple[str, Optional[str]]
41
50
m = re .match (r'^(.+)(\[[^\]]+\])$' , path )
42
51
extras = None
43
52
if m :
@@ -50,6 +59,7 @@ def _strip_extras(path):
50
59
51
60
52
61
def parse_editable (editable_req ):
62
+ # type: (str) -> Tuple[Optional[str], str, Optional[Set[str]]]
53
63
"""Parses an editable requirement into:
54
64
- a requirement name
55
65
- an URL
@@ -115,6 +125,7 @@ def parse_editable(editable_req):
115
125
116
126
117
127
def deduce_helpful_msg (req ):
128
+ # type: (str) -> str
118
129
"""Returns helpful msg in case requirements file does not exist,
119
130
or cannot be parsed.
120
131
@@ -135,7 +146,7 @@ def deduce_helpful_msg(req):
135
146
" the packages specified within it."
136
147
except RequirementParseError :
137
148
logger .debug ("Cannot parse '%s' as requirements \
138
- file" % (req ), exc_info = 1 )
149
+ file" % (req ), exc_info = True )
139
150
else :
140
151
msg += " File '%s' does not exist." % (req )
141
152
return msg
@@ -145,9 +156,15 @@ def deduce_helpful_msg(req):
145
156
146
157
147
158
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
150
166
):
167
+ # type: (...) -> InstallRequirement
151
168
name , url , extras_override = parse_editable (editable_req )
152
169
if url .startswith ('file:' ):
153
170
source_dir = url_to_path (url )
@@ -175,9 +192,15 @@ def install_req_from_editable(
175
192
176
193
177
194
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
180
202
):
203
+ # type: (...) -> InstallRequirement
181
204
"""Creates an InstallRequirement from a name, which might be a
182
205
requirement, directory containing 'setup.py', filename, or URL.
183
206
"""
@@ -186,24 +209,24 @@ def install_req_from_line(
186
209
else :
187
210
marker_sep = ';'
188
211
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 :
192
215
markers = None
193
216
else :
194
- markers = Marker (markers )
217
+ markers = Marker (markers_as_string )
195
218
else :
196
219
markers = None
197
220
name = name .strip ()
198
- req = None
221
+ req_as_string = None
199
222
path = os .path .normpath (os .path .abspath (name ))
200
223
link = None
201
- extras = None
224
+ extras_as_string = None
202
225
203
226
if is_url (name ):
204
227
link = Link (name )
205
228
else :
206
- p , extras = _strip_extras (path )
229
+ p , extras_as_string = _strip_extras (path )
207
230
looks_like_dir = os .path .isdir (p ) and (
208
231
os .path .sep in name or
209
232
(os .path .altsep is not None and os .path .altsep in name ) or
@@ -234,34 +257,37 @@ def install_req_from_line(
234
257
# wheel file
235
258
if link .is_wheel :
236
259
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 )
238
261
else :
239
262
# set the req to the egg fragment. when it's not there, this
240
263
# will become an 'unnamed' requirement
241
- req = link .egg_fragment
264
+ req_as_string = link .egg_fragment
242
265
243
266
# a requirement specifier
244
267
else :
245
- req = name
268
+ req_as_string = name
246
269
247
- if extras :
248
- extras = Requirement ("placeholder" + extras .lower ()).extras
270
+ if extras_as_string :
271
+ extras = Requirement ("placeholder" + extras_as_string .lower ()).extras
249
272
else :
250
273
extras = ()
251
- if req is not None :
274
+ if req_as_string is not None :
252
275
try :
253
- req = Requirement (req )
276
+ req = Requirement (req_as_string )
254
277
except InvalidRequirement :
255
- if os .path .sep in req :
278
+ if os .path .sep in req_as_string :
256
279
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 )):
259
283
add_msg = "= is not a valid operator. Did you mean == ?"
260
284
else :
261
285
add_msg = ""
262
286
raise InstallationError (
263
- "Invalid requirement: '%s'\n %s" % (req , add_msg )
287
+ "Invalid requirement: '%s'\n %s" % (req_as_string , add_msg )
264
288
)
289
+ else :
290
+ req = None
265
291
266
292
return InstallRequirement (
267
293
req , comes_from , link = link , markers = markers ,
@@ -273,12 +299,16 @@ def install_req_from_line(
273
299
)
274
300
275
301
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]
279
308
):
309
+ # type: (...) -> InstallRequirement
280
310
try :
281
- req = Requirement (req )
311
+ req = Requirement (req_string )
282
312
except InvalidRequirement :
283
313
raise InstallationError ("Invalid requirement: '%s'" % req )
284
314
0 commit comments