Skip to content

Commit 02b1855

Browse files
authored
Merge pull request #10188 from harupy/type-annotations-req
2 parents 9ebd05c + 9d96e07 commit 02b1855

File tree

7 files changed

+267
-380
lines changed

7 files changed

+267
-380
lines changed

src/pip/_internal/req/__init__.py

+14-18
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,32 @@
1717

1818

1919
class InstallationResult:
20-
def __init__(self, name):
21-
# type: (str) -> None
20+
def __init__(self, name: str) -> None:
2221
self.name = name
2322

24-
def __repr__(self):
25-
# type: () -> str
23+
def __repr__(self) -> str:
2624
return f"InstallationResult(name={self.name!r})"
2725

2826

2927
def _validate_requirements(
30-
requirements, # type: List[InstallRequirement]
31-
):
32-
# type: (...) -> Iterator[Tuple[str, InstallRequirement]]
28+
requirements: List[InstallRequirement],
29+
) -> Iterator[Tuple[str, InstallRequirement]]:
3330
for req in requirements:
3431
assert req.name, f"invalid to-be-installed requirement: {req}"
3532
yield req.name, req
3633

3734

3835
def install_given_reqs(
39-
requirements, # type: List[InstallRequirement]
40-
install_options, # type: List[str]
41-
global_options, # type: Sequence[str]
42-
root, # type: Optional[str]
43-
home, # type: Optional[str]
44-
prefix, # type: Optional[str]
45-
warn_script_location, # type: bool
46-
use_user_site, # type: bool
47-
pycompile, # type: bool
48-
):
49-
# type: (...) -> List[InstallationResult]
36+
requirements: List[InstallRequirement],
37+
install_options: List[str],
38+
global_options: Sequence[str],
39+
root: Optional[str],
40+
home: Optional[str],
41+
prefix: Optional[str],
42+
warn_script_location: bool,
43+
use_user_site: bool,
44+
pycompile: bool,
45+
) -> List[InstallationResult]:
5046
"""
5147
Install everything in the given list.
5248

src/pip/_internal/req/constructors.py

+46-58
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@
3939
operators = Specifier._operators.keys()
4040

4141

42-
def _strip_extras(path):
43-
# type: (str) -> Tuple[str, Optional[str]]
42+
def _strip_extras(path: str) -> Tuple[str, Optional[str]]:
4443
m = re.match(r'^(.+)(\[[^\]]+\])$', path)
4544
extras = None
4645
if m:
@@ -52,15 +51,13 @@ def _strip_extras(path):
5251
return path_no_extras, extras
5352

5453

55-
def convert_extras(extras):
56-
# type: (Optional[str]) -> Set[str]
54+
def convert_extras(extras: Optional[str]) -> Set[str]:
5755
if not extras:
5856
return set()
5957
return Requirement("placeholder" + extras.lower()).extras
6058

6159

62-
def parse_editable(editable_req):
63-
# type: (str) -> Tuple[Optional[str], str, Set[str]]
60+
def parse_editable(editable_req: str) -> Tuple[Optional[str], str, Set[str]]:
6461
"""Parses an editable requirement into:
6562
- a requirement name
6663
- an URL
@@ -131,8 +128,7 @@ def parse_editable(editable_req):
131128
return package_name, url, set()
132129

133130

134-
def deduce_helpful_msg(req):
135-
# type: (str) -> str
131+
def deduce_helpful_msg(req: str) -> str:
136132
"""Returns helpful msg in case requirements file does not exist,
137133
or cannot be parsed.
138134
@@ -165,24 +161,23 @@ def deduce_helpful_msg(req):
165161
class RequirementParts:
166162
def __init__(
167163
self,
168-
requirement, # type: Optional[Requirement]
169-
link, # type: Optional[Link]
170-
markers, # type: Optional[Marker]
171-
extras, # type: Set[str]
164+
requirement: Optional[Requirement],
165+
link: Optional[Link],
166+
markers: Optional[Marker],
167+
extras: Set[str],
172168
):
173169
self.requirement = requirement
174170
self.link = link
175171
self.markers = markers
176172
self.extras = extras
177173

178174

179-
def parse_req_from_editable(editable_req):
180-
# type: (str) -> RequirementParts
175+
def parse_req_from_editable(editable_req: str) -> RequirementParts:
181176
name, url, extras_override = parse_editable(editable_req)
182177

183178
if name is not None:
184179
try:
185-
req = Requirement(name) # type: Optional[Requirement]
180+
req: Optional[Requirement] = Requirement(name)
186181
except InvalidRequirement:
187182
raise InstallationError(f"Invalid requirement: '{name}'")
188183
else:
@@ -197,15 +192,14 @@ def parse_req_from_editable(editable_req):
197192

198193

199194
def install_req_from_editable(
200-
editable_req, # type: str
201-
comes_from=None, # type: Optional[Union[InstallRequirement, str]]
202-
use_pep517=None, # type: Optional[bool]
203-
isolated=False, # type: bool
204-
options=None, # type: Optional[Dict[str, Any]]
205-
constraint=False, # type: bool
206-
user_supplied=False, # type: bool
207-
):
208-
# type: (...) -> InstallRequirement
195+
editable_req: str,
196+
comes_from: Optional[Union[InstallRequirement, str]] = None,
197+
use_pep517: Optional[bool] = None,
198+
isolated: bool = False,
199+
options: Optional[Dict[str, Any]] = None,
200+
constraint: bool = False,
201+
user_supplied: bool = False,
202+
) -> InstallRequirement:
209203

210204
parts = parse_req_from_editable(editable_req)
211205

@@ -225,8 +219,7 @@ def install_req_from_editable(
225219
)
226220

227221

228-
def _looks_like_path(name):
229-
# type: (str) -> bool
222+
def _looks_like_path(name: str) -> bool:
230223
"""Checks whether the string "looks like" a path on the filesystem.
231224
232225
This does not check whether the target actually exists, only judge from the
@@ -245,8 +238,7 @@ def _looks_like_path(name):
245238
return False
246239

247240

248-
def _get_url_from_path(path, name):
249-
# type: (str, str) -> Optional[str]
241+
def _get_url_from_path(path: str, name: str) -> Optional[str]:
250242
"""
251243
First, it checks whether a provided path is an installable directory. If it
252244
is, returns the path.
@@ -279,8 +271,7 @@ def _get_url_from_path(path, name):
279271
return path_to_url(path)
280272

281273

282-
def parse_req_from_line(name, line_source):
283-
# type: (str, Optional[str]) -> RequirementParts
274+
def parse_req_from_line(name: str, line_source: Optional[str]) -> RequirementParts:
284275
if is_url(name):
285276
marker_sep = '; '
286277
else:
@@ -329,8 +320,7 @@ def parse_req_from_line(name, line_source):
329320

330321
extras = convert_extras(extras_as_string)
331322

332-
def with_source(text):
333-
# type: (str) -> str
323+
def with_source(text: str) -> str:
334324
if not line_source:
335325
return text
336326
return f'{text} (from {line_source})'
@@ -366,24 +356,23 @@ def _parse_req_string(req_as_string: str) -> Requirement:
366356
return req
367357

368358
if req_as_string is not None:
369-
req = _parse_req_string(req_as_string) # type: Optional[Requirement]
359+
req: Optional[Requirement] = _parse_req_string(req_as_string)
370360
else:
371361
req = None
372362

373363
return RequirementParts(req, link, markers, extras)
374364

375365

376366
def install_req_from_line(
377-
name, # type: str
378-
comes_from=None, # type: Optional[Union[str, InstallRequirement]]
379-
use_pep517=None, # type: Optional[bool]
380-
isolated=False, # type: bool
381-
options=None, # type: Optional[Dict[str, Any]]
382-
constraint=False, # type: bool
383-
line_source=None, # type: Optional[str]
384-
user_supplied=False, # type: bool
385-
):
386-
# type: (...) -> InstallRequirement
367+
name: str,
368+
comes_from: Optional[Union[str, InstallRequirement]] = None,
369+
use_pep517: Optional[bool] = None,
370+
isolated: bool = False,
371+
options: Optional[Dict[str, Any]] = None,
372+
constraint: bool = False,
373+
line_source: Optional[str] = None,
374+
user_supplied: bool = False,
375+
) -> InstallRequirement:
387376
"""Creates an InstallRequirement from a name, which might be a
388377
requirement, directory containing 'setup.py', filename, or URL.
389378
@@ -405,13 +394,12 @@ def install_req_from_line(
405394

406395

407396
def install_req_from_req_string(
408-
req_string, # type: str
409-
comes_from=None, # type: Optional[InstallRequirement]
410-
isolated=False, # type: bool
411-
use_pep517=None, # type: Optional[bool]
412-
user_supplied=False, # type: bool
413-
):
414-
# type: (...) -> InstallRequirement
397+
req_string: str,
398+
comes_from: Optional[InstallRequirement] = None,
399+
isolated: bool = False,
400+
use_pep517: Optional[bool] = None,
401+
user_supplied: bool = False,
402+
) -> InstallRequirement:
415403
try:
416404
req = Requirement(req_string)
417405
except InvalidRequirement:
@@ -440,12 +428,11 @@ def install_req_from_req_string(
440428

441429

442430
def install_req_from_parsed_requirement(
443-
parsed_req, # type: ParsedRequirement
444-
isolated=False, # type: bool
445-
use_pep517=None, # type: Optional[bool]
446-
user_supplied=False, # type: bool
447-
):
448-
# type: (...) -> InstallRequirement
431+
parsed_req: ParsedRequirement,
432+
isolated: bool = False,
433+
use_pep517: Optional[bool] = None,
434+
user_supplied: bool = False,
435+
) -> InstallRequirement:
449436
if parsed_req.is_editable:
450437
req = install_req_from_editable(
451438
parsed_req.requirement,
@@ -470,8 +457,9 @@ def install_req_from_parsed_requirement(
470457
return req
471458

472459

473-
def install_req_from_link_and_ireq(link, ireq):
474-
# type: (Link, InstallRequirement) -> InstallRequirement
460+
def install_req_from_link_and_ireq(
461+
link: Link, ireq: InstallRequirement
462+
) -> InstallRequirement:
475463
return InstallRequirement(
476464
req=ireq.req,
477465
comes_from=ireq.comes_from,

0 commit comments

Comments
 (0)