Skip to content

Commit 503e6d6

Browse files
authored
Merge pull request #9413 from jdufresne/type-vcs
Complete type annotations for the vcs package
2 parents 0b004a6 + c985454 commit 503e6d6

File tree

5 files changed

+41
-22
lines changed

5 files changed

+41
-22
lines changed

news/f8f0d057-9ed5-4d22-a877-50f2d5adcae0.trivial.rst

Whitespace-only changes.

src/pip/_internal/vcs/bazaar.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# The following comment should be removed at some point in the future.
2-
# mypy: disallow-untyped-defs=False
3-
41
import logging
52
import os
63

@@ -11,7 +8,7 @@
118
from pip._internal.vcs.versioncontrol import RemoteNotFoundError, VersionControl, vcs
129

1310
if MYPY_CHECK_RUNNING:
14-
from typing import Optional, Tuple
11+
from typing import List, Optional, Tuple
1512

1613
from pip._internal.utils.misc import HiddenText
1714
from pip._internal.vcs.versioncontrol import AuthInfo, RevOptions
@@ -31,6 +28,7 @@ class Bazaar(VersionControl):
3128

3229
@staticmethod
3330
def get_base_rev_args(rev):
31+
# type: (str) -> List[str]
3432
return ['-r', rev]
3533

3634
def export(self, location, url):
@@ -107,6 +105,7 @@ def get_revision(cls, location):
107105

108106
@classmethod
109107
def is_commit_id_equal(cls, dest, name):
108+
# type: (str, Optional[str]) -> bool
110109
"""Always assume the versions don't match"""
111110
return False
112111

src/pip/_internal/vcs/git.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# The following comment should be removed at some point in the future.
2-
# mypy: disallow-untyped-defs=False
3-
41
import logging
52
import os.path
63
import re
@@ -22,7 +19,9 @@
2219
)
2320

2421
if MYPY_CHECK_RUNNING:
25-
from typing import Optional, Tuple
22+
from typing import List, Optional, Tuple
23+
24+
from pip._vendor.packaging.version import _BaseVersion
2625

2726
from pip._internal.utils.misc import HiddenText
2827
from pip._internal.vcs.versioncontrol import AuthInfo, RevOptions
@@ -39,6 +38,7 @@
3938

4039

4140
def looks_like_hash(sha):
41+
# type: (str) -> bool
4242
return bool(HASH_REGEX.match(sha))
4343

4444

@@ -56,6 +56,7 @@ class Git(VersionControl):
5656

5757
@staticmethod
5858
def get_base_rev_args(rev):
59+
# type: (str) -> List[str]
5960
return [rev]
6061

6162
def is_immutable_rev_checkout(self, url, dest):
@@ -76,6 +77,7 @@ def is_immutable_rev_checkout(self, url, dest):
7677
return not is_tag_or_branch
7778

7879
def get_git_version(self):
80+
# type: () -> _BaseVersion
7981
VERSION_PFX = 'git version '
8082
version = self.run_command(
8183
['version'], show_stdout=False, stdout_only=True
@@ -92,6 +94,7 @@ def get_git_version(self):
9294

9395
@classmethod
9496
def get_current_branch(cls, location):
97+
# type: (str) -> Optional[str]
9598
"""
9699
Return the current branch, or None if HEAD isn't at a branch
97100
(e.g. detached HEAD).
@@ -130,6 +133,7 @@ def export(self, location, url):
130133

131134
@classmethod
132135
def get_revision_sha(cls, dest, rev):
136+
# type: (str, str) -> Tuple[Optional[str], bool]
133137
"""
134138
Return (sha_or_none, is_branch), where sha_or_none is a commit hash
135139
if the revision names a remote branch or tag, otherwise None.
@@ -149,13 +153,13 @@ def get_revision_sha(cls, dest, rev):
149153
refs = {}
150154
for line in output.strip().splitlines():
151155
try:
152-
sha, ref = line.split()
156+
ref_sha, ref_name = line.split()
153157
except ValueError:
154158
# Include the offending line to simplify troubleshooting if
155159
# this error ever occurs.
156160
raise ValueError(f'unexpected show-ref line: {line!r}')
157161

158-
refs[ref] = sha
162+
refs[ref_name] = ref_sha
159163

160164
branch_ref = f'refs/remotes/origin/{rev}'
161165
tag_ref = f'refs/tags/{rev}'
@@ -170,6 +174,7 @@ def get_revision_sha(cls, dest, rev):
170174

171175
@classmethod
172176
def _should_fetch(cls, dest, rev):
177+
# type: (str, str) -> bool
173178
"""
174179
Return true if rev is a ref or is a commit that we don't have locally.
175180
@@ -238,6 +243,7 @@ def resolve_revision(cls, dest, url, rev_options):
238243

239244
@classmethod
240245
def is_commit_id_equal(cls, dest, name):
246+
# type: (str, Optional[str]) -> bool
241247
"""
242248
Return whether the current commit hash equals the given name.
243249
@@ -340,6 +346,7 @@ def get_remote_url(cls, location):
340346

341347
@classmethod
342348
def has_commit(cls, location, rev):
349+
# type: (str, str) -> bool
343350
"""
344351
Check if rev is a commit that is available in the local repository.
345352
"""
@@ -369,6 +376,7 @@ def get_revision(cls, location, rev=None):
369376

370377
@classmethod
371378
def get_subdirectory(cls, location):
379+
# type: (str) -> Optional[str]
372380
"""
373381
Return the path to setup.py, relative to the repo root.
374382
Return None if setup.py is in the repo root.
@@ -421,6 +429,7 @@ def get_url_rev_and_auth(cls, url):
421429

422430
@classmethod
423431
def update_submodules(cls, location):
432+
# type: (str) -> None
424433
if not os.path.exists(os.path.join(location, '.gitmodules')):
425434
return
426435
cls.run_command(
@@ -430,6 +439,7 @@ def update_submodules(cls, location):
430439

431440
@classmethod
432441
def get_repository_root(cls, location):
442+
# type: (str) -> Optional[str]
433443
loc = super().get_repository_root(location)
434444
if loc:
435445
return loc

src/pip/_internal/vcs/mercurial.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# The following comment should be removed at some point in the future.
2-
# mypy: disallow-untyped-defs=False
3-
41
import configparser
52
import logging
63
import os
@@ -18,6 +15,8 @@
1815
)
1916

2017
if MYPY_CHECK_RUNNING:
18+
from typing import List, Optional
19+
2120
from pip._internal.utils.misc import HiddenText
2221
from pip._internal.vcs.versioncontrol import RevOptions
2322

@@ -35,6 +34,7 @@ class Mercurial(VersionControl):
3534

3635
@staticmethod
3736
def get_base_rev_args(rev):
37+
# type: (str) -> List[str]
3838
return [rev]
3939

4040
def export(self, location, url):
@@ -114,6 +114,7 @@ def get_revision(cls, location):
114114

115115
@classmethod
116116
def get_requirement_revision(cls, location):
117+
# type: (str) -> str
117118
"""
118119
Return the changeset identification hash, as a 40-character
119120
hexadecimal string
@@ -128,11 +129,13 @@ def get_requirement_revision(cls, location):
128129

129130
@classmethod
130131
def is_commit_id_equal(cls, dest, name):
132+
# type: (str, Optional[str]) -> bool
131133
"""Always assume the versions don't match"""
132134
return False
133135

134136
@classmethod
135137
def get_subdirectory(cls, location):
138+
# type: (str) -> Optional[str]
136139
"""
137140
Return the path to setup.py, relative to the repo root.
138141
Return None if setup.py is in the repo root.
@@ -147,6 +150,7 @@ def get_subdirectory(cls, location):
147150

148151
@classmethod
149152
def get_repository_root(cls, location):
153+
# type: (str) -> Optional[str]
150154
loc = super().get_repository_root(location)
151155
if loc:
152156
return loc

src/pip/_internal/vcs/subversion.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# The following comment should be removed at some point in the future.
2-
# mypy: disallow-untyped-defs=False
3-
41
import logging
52
import os
63
import re
@@ -23,7 +20,7 @@
2320

2421

2522
if MYPY_CHECK_RUNNING:
26-
from typing import Optional, Tuple
23+
from typing import List, Optional, Tuple
2724

2825
from pip._internal.utils.misc import HiddenText
2926
from pip._internal.utils.subprocess import CommandArgs
@@ -43,10 +40,12 @@ class Subversion(VersionControl):
4340

4441
@classmethod
4542
def should_add_vcs_url_prefix(cls, remote_url):
43+
# type: (str) -> bool
4644
return True
4745

4846
@staticmethod
4947
def get_base_rev_args(rev):
48+
# type: (str) -> List[str]
5049
return ['-r', rev]
5150

5251
@classmethod
@@ -71,6 +70,7 @@ def get_revision(cls, location):
7170
dirurl, localrev = cls._get_svn_url_rev(base)
7271

7372
if base == location:
73+
assert dirurl is not None
7474
base = dirurl + '/' # save the root url
7575
elif not dirurl or not dirurl.startswith(base):
7676
dirs[:] = []
@@ -80,6 +80,7 @@ def get_revision(cls, location):
8080

8181
@classmethod
8282
def get_netloc_and_auth(cls, netloc, scheme):
83+
# type: (str, str) -> Tuple[str, Tuple[Optional[str], Optional[str]]]
8384
"""
8485
This override allows the auth information to be passed to svn via the
8586
--username and --password options instead of via the URL.
@@ -139,6 +140,7 @@ def get_remote_url(cls, location):
139140

140141
@classmethod
141142
def _get_svn_url_rev(cls, location):
143+
# type: (str) -> Tuple[Optional[str], int]
142144
from pip._internal.exceptions import InstallationError
143145

144146
entries_path = os.path.join(location, cls.dirname, 'entries')
@@ -148,13 +150,14 @@ def _get_svn_url_rev(cls, location):
148150
else: # subversion >= 1.7 does not have the 'entries' file
149151
data = ''
150152

153+
url = None
151154
if (data.startswith('8') or
152155
data.startswith('9') or
153156
data.startswith('10')):
154-
data = list(map(str.splitlines, data.split('\n\x0c\n')))
155-
del data[0][0] # get rid of the '8'
156-
url = data[0][3]
157-
revs = [int(d[9]) for d in data if len(d) > 9 and d[9]] + [0]
157+
entries = list(map(str.splitlines, data.split('\n\x0c\n')))
158+
del entries[0][0] # get rid of the '8'
159+
url = entries[0][3]
160+
revs = [int(d[9]) for d in entries if len(d) > 9 and d[9]] + [0]
158161
elif data.startswith('<?xml'):
159162
match = _svn_xml_url_re.search(data)
160163
if not match:
@@ -175,7 +178,9 @@ def _get_svn_url_rev(cls, location):
175178
show_stdout=False,
176179
stdout_only=True,
177180
)
178-
url = _svn_info_xml_url_re.search(xml).group(1)
181+
match = _svn_info_xml_url_re.search(xml)
182+
assert match is not None
183+
url = match.group(1)
179184
revs = [
180185
int(m.group(1)) for m in _svn_info_xml_rev_re.finditer(xml)
181186
]
@@ -191,6 +196,7 @@ def _get_svn_url_rev(cls, location):
191196

192197
@classmethod
193198
def is_commit_id_equal(cls, dest, name):
199+
# type: (str, Optional[str]) -> bool
194200
"""Always assume the versions don't match"""
195201
return False
196202

0 commit comments

Comments
 (0)