Skip to content

Commit 8899c25

Browse files
authored
Merge pull request #10396 from jdufresne/more-test-mypy
Complete typing of some tests/lib/* files
2 parents 7aaea4e + 34a3838 commit 8899c25

File tree

8 files changed

+89
-62
lines changed

8 files changed

+89
-62
lines changed

news/06d03f6d-b858-4973-af59-2a8bbad0f6dc.trivial.rst

Whitespace-only changes.

src/pip/_internal/cli/base_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def handle_pip_version_check(self, options: Values) -> None:
8888
def run(self, options: Values, args: List[Any]) -> int:
8989
raise NotImplementedError
9090

91-
def parse_args(self, args: List[str]) -> Tuple[Any, Any]:
91+
def parse_args(self, args: List[str]) -> Tuple[Values, List[str]]:
9292
# factored out for testability
9393
return self.parser.parse_args(args)
9494

tests/lib/direct_url.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import re
2+
from typing import Optional
23

34
from pip._internal.models.direct_url import DIRECT_URL_METADATA_NAME, DirectUrl
5+
from tests.lib import TestPipResult
46

57

6-
def get_created_direct_url(result, pkg):
8+
def get_created_direct_url(result: TestPipResult, pkg: str) -> Optional[DirectUrl]:
79
direct_url_metadata_re = re.compile(
810
pkg + r"-[\d\.]+\.dist-info." + DIRECT_URL_METADATA_NAME + r"$"
911
)

tests/lib/filesystem.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
import sys
77
from functools import partial
88
from itertools import chain
9+
from typing import Iterator, List, Set
910

1011
from .path import Path
1112

1213

13-
def make_socket_file(path):
14+
def make_socket_file(path: str) -> None:
1415
# Socket paths are limited to 108 characters (sometimes less) so we
1516
# chdir before creating it and use a relative path name.
1617
cwd = os.getcwd()
@@ -22,7 +23,7 @@ def make_socket_file(path):
2223
os.chdir(cwd)
2324

2425

25-
def make_unreadable_file(path):
26+
def make_unreadable_file(path: str) -> None:
2627
Path(path).touch()
2728
os.chmod(path, 0o000)
2829
if sys.platform == "win32":
@@ -34,8 +35,8 @@ def make_unreadable_file(path):
3435
subprocess.check_call(args)
3536

3637

37-
def get_filelist(base):
38-
def join(dirpath, dirnames, filenames):
38+
def get_filelist(base: str) -> Set[str]:
39+
def join(dirpath: str, dirnames: List[str], filenames: List[str]) -> Iterator[str]:
3940
relative_dirpath = os.path.relpath(dirpath, base)
4041
join_dirpath = partial(os.path.join, relative_dirpath)
4142
return chain(

tests/lib/index.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
from typing import Optional
2+
13
from pip._internal.models.candidate import InstallationCandidate
24
from pip._internal.models.link import Link
35

46

5-
def make_mock_candidate(version, yanked_reason=None, hex_digest=None):
7+
def make_mock_candidate(
8+
version: str, yanked_reason: Optional[str] = None, hex_digest: Optional[str] = None
9+
) -> InstallationCandidate:
610
url = f"https://example.com/pkg-{version}.tar.gz"
711
if hex_digest is not None:
812
assert len(hex_digest) == 64

tests/lib/options_helpers.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
"""Provides helper classes for testing option handling in pip
22
"""
33

4+
from optparse import Values
5+
from typing import List, Tuple
6+
47
from pip._internal.cli import cmdoptions
58
from pip._internal.cli.base_command import Command
69
from pip._internal.commands import CommandInfo, commands_dict
710

811

912
class FakeCommand(Command):
10-
def main(self, args):
13+
def main( # type: ignore[override]
14+
self, args: List[str]
15+
) -> Tuple[Values, List[str]]:
1116
index_opts = cmdoptions.make_option_group(
1217
cmdoptions.index_group,
1318
self.parser,
@@ -17,12 +22,12 @@ def main(self, args):
1722

1823

1924
class AddFakeCommandMixin:
20-
def setup(self):
25+
def setup(self) -> None:
2126
commands_dict["fake"] = CommandInfo(
2227
"tests.lib.options_helpers",
2328
"FakeCommand",
2429
"fake summary",
2530
)
2631

27-
def teardown(self):
32+
def teardown(self) -> None:
2833
commands_dict.pop("fake")

tests/lib/path.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
# flake8: noqa
21
# Author: Aziz Köksal
32
import glob
43
import os
5-
6-
try:
7-
from os import supports_fd
8-
except ImportError:
9-
supports_fd = set()
4+
from typing import Iterable, Iterator, Union
105

116

127
class Path(str):
@@ -20,12 +15,12 @@ class Path(str):
2015
# Separator in the PATH environment variable.
2116
pathsep = os.pathsep
2217

23-
def __new__(cls, *paths):
18+
def __new__(cls, *paths: str) -> "Path":
2419
if len(paths):
2520
return super().__new__(cls, os.path.join(*paths))
2621
return super().__new__(cls)
2722

28-
def __div__(self, path):
23+
def __div__(self, path: str) -> "Path":
2924
"""
3025
Joins this path with another path.
3126
@@ -36,7 +31,7 @@ def __div__(self, path):
3631

3732
__truediv__ = __div__
3833

39-
def __rdiv__(self, path):
34+
def __rdiv__(self, path: str) -> "Path":
4035
"""
4136
Joins this path with another path.
4237
@@ -46,7 +41,7 @@ def __rdiv__(self, path):
4641

4742
__rtruediv__ = __rdiv__
4843

49-
def __idiv__(self, path):
44+
def __idiv__(self, path: str) -> "Path":
5045
"""
5146
Like __div__ but also assigns to the variable.
5247
@@ -56,52 +51,52 @@ def __idiv__(self, path):
5651

5752
__itruediv__ = __idiv__
5853

59-
def __add__(self, path):
54+
def __add__(self, path: str) -> "Path":
6055
"""
6156
>>> Path('/home/a') + 'bc.d'
6257
'/home/abc.d'
6358
"""
6459
return Path(str(self) + path)
6560

66-
def __radd__(self, path):
61+
def __radd__(self, path: str) -> "Path":
6762
"""
6863
>>> '/home/a' + Path('bc.d')
6964
'/home/abc.d'
7065
"""
7166
return Path(path + str(self))
7267

73-
def __repr__(self):
68+
def __repr__(self) -> str:
7469
return "Path({inner})".format(inner=str.__repr__(self))
7570

7671
@property
77-
def name(self):
72+
def name(self) -> str:
7873
"""
7974
'/home/a/bc.d' -> 'bc.d'
8075
"""
8176
return os.path.basename(self)
8277

8378
@property
84-
def stem(self):
79+
def stem(self) -> str:
8580
"""
8681
'/home/a/bc.d' -> 'bc'
8782
"""
8883
return Path(os.path.splitext(self)[0]).name
8984

9085
@property
91-
def suffix(self):
86+
def suffix(self) -> str:
9287
"""
9388
'/home/a/bc.d' -> '.d'
9489
"""
9590
return Path(os.path.splitext(self)[1])
9691

97-
def resolve(self):
92+
def resolve(self) -> "Path":
9893
"""
9994
Resolves symbolic links.
10095
"""
10196
return Path(os.path.realpath(self))
10297

10398
@property
104-
def parent(self):
99+
def parent(self) -> "Path":
105100
"""
106101
Returns the parent directory of this path.
107102
@@ -111,13 +106,18 @@ def parent(self):
111106
"""
112107
return Path(os.path.dirname(self))
113108

114-
def exists(self):
109+
def exists(self) -> bool:
115110
"""
116111
Returns True if the path exists.
117112
"""
118113
return os.path.exists(self)
119114

120-
def mkdir(self, mode=0x1FF, exist_ok=False, parents=False): # 0o777
115+
def mkdir(
116+
self,
117+
mode: int = 0o777,
118+
exist_ok: bool = False,
119+
parents: bool = False,
120+
) -> None:
121121
"""
122122
Creates a directory, if it doesn't exist already.
123123
@@ -131,32 +131,32 @@ def mkdir(self, mode=0x1FF, exist_ok=False, parents=False): # 0o777
131131
if not exist_ok or not os.path.isdir(self):
132132
raise
133133

134-
def unlink(self):
134+
def unlink(self) -> None:
135135
"""
136136
Removes a file.
137137
"""
138-
return os.remove(self)
138+
os.remove(self)
139139

140-
def rmdir(self):
140+
def rmdir(self) -> None:
141141
"""
142142
Removes a directory.
143143
"""
144-
return os.rmdir(self)
144+
os.rmdir(self)
145145

146-
def rename(self, to):
146+
def rename(self, to: str) -> None:
147147
"""
148148
Renames a file or directory. May throw an OSError.
149149
"""
150-
return os.rename(self, to)
150+
os.rename(self, to)
151151

152-
def glob(self, pattern):
152+
def glob(self, pattern: str) -> Iterator["Path"]:
153153
return (Path(i) for i in glob.iglob(self.joinpath(pattern)))
154154

155-
def joinpath(self, *parts):
155+
def joinpath(self, *parts: str) -> "Path":
156156
return Path(self, *parts)
157157

158158
# TODO: Remove after removing inheritance from str.
159-
def join(self, *parts):
159+
def join(self, parts: Iterable[str]) -> str:
160160
raise RuntimeError("Path.join is invalid, use joinpath instead.")
161161

162162
def read_bytes(self) -> bytes:
@@ -167,23 +167,23 @@ def write_bytes(self, content: bytes) -> None:
167167
with open(self, "wb") as f:
168168
f.write(content)
169169

170-
def read_text(self):
170+
def read_text(self) -> str:
171171
with open(self, "r") as fp:
172172
return fp.read()
173173

174-
def write_text(self, content):
174+
def write_text(self, content: str) -> None:
175175
with open(self, "w") as fp:
176176
fp.write(content)
177177

178-
def touch(self):
178+
def touch(self) -> None:
179179
with open(self, "a") as fp:
180-
path = fp.fileno() if os.utime in supports_fd else self
181-
os.utime(path, None) # times is not optional on Python 2.7
180+
path: Union[int, str] = fp.fileno() if os.utime in os.supports_fd else self
181+
os.utime(path)
182182

183-
def symlink_to(self, target):
183+
def symlink_to(self, target: str) -> None:
184184
os.symlink(target, self)
185185

186-
def stat(self):
186+
def stat(self) -> os.stat_result:
187187
return os.stat(self)
188188

189189

0 commit comments

Comments
 (0)