Skip to content

Commit 9e5e969

Browse files
committed
Change remaining type comments to py3.6+ types
1 parent 9a587e1 commit 9e5e969

File tree

16 files changed

+77
-73
lines changed

16 files changed

+77
-73
lines changed

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ enable-extensions = TC, TC2 # only needed for extensions not enabled by default
1919
ignore = E265,E266,E731,E704,
2020
W293, W504,
2121
ANN0 ANN1 ANN2,
22-
TC0, TC1, TC2
22+
# TC0, TC1, TC2
2323
# B,
2424
A,
2525
D,

git/cmd.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -565,11 +565,11 @@ def __init__(self, working_dir: Union[None, PathLike] = None):
565565
.git directory in case of bare repositories."""
566566
super(Git, self).__init__()
567567
self._working_dir = expand_path(working_dir)
568-
self._git_options = () # type: Union[List[str], Tuple[str, ...]]
569-
self._persistent_git_options = [] # type: List[str]
568+
self._git_options: Union[List[str], Tuple[str, ...]] = ()
569+
self._persistent_git_options: List[str] = []
570570

571571
# Extra environment variables to pass to git commands
572-
self._environment = {} # type: Dict[str, str]
572+
self._environment: Dict[str, str] = {}
573573

574574
# cached command slots
575575
self.cat_file_header = None
@@ -603,9 +603,9 @@ def _set_cache_(self, attr: str) -> None:
603603
process_version = self._call_process('version') # should be as default *args and **kwargs used
604604
version_numbers = process_version.split(' ')[2]
605605

606-
self._version_info = tuple(
606+
self._version_info: Tuple[int, int, int, int] = tuple(
607607
int(n) for n in version_numbers.split('.')[:4] if n.isdigit()
608-
) # type: Tuple[int, int, int, int] # type: ignore
608+
) # type: ignore # use typeguard here
609609
else:
610610
super(Git, self)._set_cache_(attr)
611611
# END handle version info
@@ -868,8 +868,8 @@ def _kill_process(pid: int) -> None:
868868

869869
# Wait for the process to return
870870
status = 0
871-
stdout_value = b'' # type: Union[str, bytes]
872-
stderr_value = b'' # type: Union[str, bytes]
871+
stdout_value: Union[str, bytes] = b''
872+
stderr_value: Union[str, bytes] = b''
873873
newline = "\n" if universal_newlines else b"\n"
874874
try:
875875
if output_stream is None:
@@ -1145,7 +1145,7 @@ def _prepare_ref(self, ref: AnyStr) -> bytes:
11451145
# required for command to separate refs on stdin, as bytes
11461146
if isinstance(ref, bytes):
11471147
# Assume 40 bytes hexsha - bin-to-ascii for some reason returns bytes, not text
1148-
refstr = ref.decode('ascii') # type: str
1148+
refstr: str = ref.decode('ascii')
11491149
elif not isinstance(ref, str):
11501150
refstr = str(ref) # could be ref-object
11511151
else:

git/compat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
# ---------------------------------------------------------------------------
3535

3636

37-
is_win = (os.name == 'nt') # type: bool
37+
is_win: bool = (os.name == 'nt')
3838
is_posix = (os.name == 'posix')
3939
is_darwin = (os.name == 'darwin')
4040
defenc = sys.getfilesystemencoding()

git/config.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def get(self, key: str, default: Union[Any, None] = None) -> Union[Any, None]:
208208
def getall(self, key: str) -> Any:
209209
return super(_OMD, self).__getitem__(key)
210210

211-
def items(self) -> List[Tuple[str, Any]]: # type: ignore ## mypy doesn't like overwriting supertype signitures
211+
def items(self) -> List[Tuple[str, Any]]: # type: ignore[override]
212212
"""List of (key, last value for key)."""
213213
return [(k, self[k]) for k in self]
214214

@@ -322,7 +322,7 @@ def __init__(self, file_or_files: Union[None, PathLike, 'BytesIO', Sequence[Unio
322322
self._is_initialized = False
323323
self._merge_includes = merge_includes
324324
self._repo = repo
325-
self._lock = None # type: Union['LockFile', None]
325+
self._lock: Union['LockFile', None] = None
326326
self._acquire_lock()
327327

328328
def _acquire_lock(self) -> None:
@@ -545,13 +545,15 @@ def read(self) -> None:
545545
return None
546546
self._is_initialized = True
547547

548-
files_to_read = [""] # type: List[Union[PathLike, IO]] ## just for types until 3.5 dropped
549-
if isinstance(self._file_or_files, (str)): # replace with PathLike once 3.5 dropped
550-
files_to_read = [self._file_or_files] # for str, as str is a type of Sequence
548+
files_to_read: List[Union[PathLike, IO]] = [""]
549+
if isinstance(self._file_or_files, (str, os.PathLike)):
550+
# for str or Path, as str is a type of Sequence
551+
files_to_read = [self._file_or_files]
551552
elif not isinstance(self._file_or_files, (tuple, list, Sequence)):
552-
files_to_read = [self._file_or_files] # for IO or Path
553-
else:
554-
files_to_read = list(self._file_or_files) # for lists or tuples
553+
# could merge with above isinstance once runtime type known
554+
files_to_read = [self._file_or_files]
555+
else: # for lists or tuples
556+
files_to_read = list(self._file_or_files)
555557
# end assure we have a copy of the paths to handle
556558

557559
seen = set(files_to_read)

git/diff.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,13 +351,13 @@ def __hash__(self) -> int:
351351
return hash(tuple(getattr(self, n) for n in self.__slots__))
352352

353353
def __str__(self) -> str:
354-
h = "%s" # type: str
354+
h: str = "%s"
355355
if self.a_blob:
356356
h %= self.a_blob.path
357357
elif self.b_blob:
358358
h %= self.b_blob.path
359359

360-
msg = '' # type: str
360+
msg: str = ''
361361
line = None # temp line
362362
line_length = 0 # line length
363363
for b, n in zip((self.a_blob, self.b_blob), ('lhs', 'rhs')):
@@ -449,7 +449,7 @@ def _index_from_patch_format(cls, repo: 'Repo', proc: TBD) -> DiffIndex:
449449
:return: git.DiffIndex """
450450

451451
## FIXME: Here SLURPING raw, need to re-phrase header-regexes linewise.
452-
text_list = [] # type: List[bytes]
452+
text_list: List[bytes] = []
453453
handle_process_output(proc, text_list.append, None, finalize_process, decode_streams=False)
454454

455455
# for now, we have to bake the stream

git/index/base.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def __init__(self, repo: 'Repo', file_path: Union[PathLike, None] = None) -> Non
123123
self.repo = repo
124124
self.version = self._VERSION
125125
self._extension_data = b''
126-
self._file_path = file_path or self._index_path() # type: PathLike
126+
self._file_path: PathLike = file_path or self._index_path()
127127

128128
def _set_cache_(self, attr: str) -> None:
129129
if attr == "entries":
@@ -136,7 +136,7 @@ def _set_cache_(self, attr: str) -> None:
136136
ok = True
137137
except OSError:
138138
# in new repositories, there may be no index, which means we are empty
139-
self.entries = {} # type: Dict[Tuple[PathLike, StageType], IndexEntry]
139+
self.entries: Dict[Tuple[PathLike, StageType], IndexEntry] = {}
140140
return None
141141
finally:
142142
if not ok:
@@ -266,7 +266,7 @@ def merge_tree(self, rhs: Treeish, base: Union[None, Treeish] = None) -> 'IndexF
266266
# -i : ignore working tree status
267267
# --aggressive : handle more merge cases
268268
# -m : do an actual merge
269-
args = ["--aggressive", "-i", "-m"] # type: List[Union[Treeish, str]]
269+
args: List[Union[Treeish, str]] = ["--aggressive", "-i", "-m"]
270270
if base is not None:
271271
args.append(base)
272272
args.append(rhs)
@@ -288,14 +288,14 @@ def new(cls, repo: 'Repo', *tree_sha: Union[str, Tree]) -> 'IndexFile':
288288
New IndexFile instance. Its path will be undefined.
289289
If you intend to write such a merged Index, supply an alternate file_path
290290
to its 'write' method."""
291-
tree_sha_bytes = [to_bin_sha(str(t)) for t in tree_sha] # List[bytes]
291+
tree_sha_bytes: List[bytes] = [to_bin_sha(str(t)) for t in tree_sha]
292292
base_entries = aggressive_tree_merge(repo.odb, tree_sha_bytes)
293293

294294
inst = cls(repo)
295295
# convert to entries dict
296-
entries = dict(zip(
296+
entries: Dict[Tuple[PathLike, int], IndexEntry] = dict(zip(
297297
((e.path, e.stage) for e in base_entries),
298-
(IndexEntry.from_base(e) for e in base_entries))) # type: Dict[Tuple[PathLike, int], IndexEntry]
298+
(IndexEntry.from_base(e) for e in base_entries)))
299299

300300
inst.entries = entries
301301
return inst
@@ -338,7 +338,7 @@ def from_tree(cls, repo: 'Repo', *treeish: Treeish, **kwargs: Any) -> 'IndexFile
338338
if len(treeish) == 0 or len(treeish) > 3:
339339
raise ValueError("Please specify between 1 and 3 treeish, got %i" % len(treeish))
340340

341-
arg_list = [] # type: List[Union[Treeish, str]]
341+
arg_list: List[Union[Treeish, str]] = []
342342
# ignore that working tree and index possibly are out of date
343343
if len(treeish) > 1:
344344
# drop unmerged entries when reading our index and merging
@@ -445,7 +445,7 @@ def _write_path_to_stdin(self, proc: 'Popen', filepath: PathLike, item: TBD, fma
445445
we will close stdin to break the pipe."""
446446

447447
fprogress(filepath, False, item)
448-
rval = None # type: Union[None, str]
448+
rval: Union[None, str] = None
449449

450450
if proc.stdin is not None:
451451
try:
@@ -492,7 +492,7 @@ def unmerged_blobs(self) -> Dict[PathLike, List[Tuple[StageType, Blob]]]:
492492
are at stage 3 will not have a stage 3 entry.
493493
"""
494494
is_unmerged_blob = lambda t: t[0] != 0
495-
path_map = {} # type: Dict[PathLike, List[Tuple[TBD, Blob]]]
495+
path_map: Dict[PathLike, List[Tuple[TBD, Blob]]] = {}
496496
for stage, blob in self.iter_blobs(is_unmerged_blob):
497497
path_map.setdefault(blob.path, []).append((stage, blob))
498498
# END for each unmerged blob
@@ -624,8 +624,8 @@ def _store_path(self, filepath: PathLike, fprogress: Callable) -> BaseIndexEntry
624624
st = os.lstat(filepath) # handles non-symlinks as well
625625
if S_ISLNK(st.st_mode):
626626
# in PY3, readlink is string, but we need bytes. In PY2, it's just OS encoded bytes, we assume UTF-8
627-
open_stream = lambda: BytesIO(force_bytes(os.readlink(filepath),
628-
encoding=defenc)) # type: Callable[[], BinaryIO]
627+
open_stream: Callable[[], BinaryIO] = lambda: BytesIO(force_bytes(os.readlink(filepath),
628+
encoding=defenc))
629629
else:
630630
open_stream = lambda: open(filepath, 'rb')
631631
with open_stream() as stream:
@@ -1160,7 +1160,7 @@ def handle_stderr(proc: 'Popen[bytes]', iter_checked_out_files: Iterable[PathLik
11601160
proc = self.repo.git.checkout_index(args, **kwargs)
11611161
# FIXME: Reading from GIL!
11621162
make_exc = lambda: GitCommandError(("git-checkout-index",) + tuple(args), 128, proc.stderr.read())
1163-
checked_out_files = [] # type: List[PathLike]
1163+
checked_out_files: List[PathLike] = []
11641164

11651165
for path in paths:
11661166
co_path = to_native_path_linux(self._to_relative_path(path))

git/index/fun.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ def run_commit_hook(name: str, index: 'IndexFile', *args: str) -> None:
9999
except Exception as ex:
100100
raise HookExecutionError(hp, ex) from ex
101101
else:
102-
stdout_list = [] # type: List[str]
103-
stderr_list = [] # type: List[str]
102+
stdout_list: List[str] = []
103+
stderr_list: List[str] = []
104104
handle_process_output(cmd, stdout_list.append, stderr_list.append, finalize_process)
105105
stdout = ''.join(stdout_list)
106106
stderr = ''.join(stderr_list)
@@ -151,8 +151,8 @@ def write_cache(entries: Sequence[Union[BaseIndexEntry, 'IndexEntry']], stream:
151151
beginoffset = tell()
152152
write(entry[4]) # ctime
153153
write(entry[5]) # mtime
154-
path_str = entry[3] # type: str
155-
path = force_bytes(path_str, encoding=defenc)
154+
path_str: str = entry[3]
155+
path: bytes = force_bytes(path_str, encoding=defenc)
156156
plen = len(path) & CE_NAMEMASK # path length
157157
assert plen == len(path), "Path %s too long to fit into index" % entry[3]
158158
flags = plen | (entry[2] & CE_NAMEMASK_INV) # clear possible previous values
@@ -210,7 +210,7 @@ def read_cache(stream: IO[bytes]) -> Tuple[int, Dict[Tuple[PathLike, int], 'Inde
210210
* content_sha is a 20 byte sha on all cache file contents"""
211211
version, num_entries = read_header(stream)
212212
count = 0
213-
entries = {} # type: Dict[Tuple[PathLike, int], 'IndexEntry']
213+
entries: Dict[Tuple[PathLike, int], 'IndexEntry'] = {}
214214

215215
read = stream.read
216216
tell = stream.tell

git/objects/submodule/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,8 @@ def add(cls, repo: 'Repo', name: str, path: PathLike, url: Union[str, None] = No
475475
sm._branch_path = br.path
476476

477477
# we deliberately assume that our head matches our index !
478-
sm.binsha = mrepo.head.commit.binsha # type: ignore
478+
if mrepo:
479+
sm.binsha = mrepo.head.commit.binsha
479480
index.add([sm], write=True)
480481

481482
return sm

git/objects/tag.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __init__(self, repo: 'Repo', binsha: bytes,
5151
authored_date is in, in a format similar to time.altzone"""
5252
super(TagObject, self).__init__(repo, binsha)
5353
if object is not None:
54-
self.object = object # type: Union['Commit', 'Blob', 'Tree', 'TagObject']
54+
self.object: Union['Commit', 'Blob', 'Tree', 'TagObject'] = object
5555
if tag is not None:
5656
self.tag = tag
5757
if tagger is not None:
@@ -67,7 +67,7 @@ def _set_cache_(self, attr: str) -> None:
6767
"""Cache all our attributes at once"""
6868
if attr in TagObject.__slots__:
6969
ostream = self.repo.odb.stream(self.binsha)
70-
lines = ostream.read().decode(defenc, 'replace').splitlines() # type: List[str]
70+
lines: List[str] = ostream.read().decode(defenc, 'replace').splitlines()
7171

7272
_obj, hexsha = lines[0].split(" ")
7373
_type_token, type_name = lines[1].split(" ")

git/objects/tree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def cache(self) -> TreeModifier:
298298
See the ``TreeModifier`` for more information on how to alter the cache"""
299299
return TreeModifier(self._cache)
300300

301-
def traverse(self, # type: ignore # overrides super()
301+
def traverse(self, # type: ignore[override]
302302
predicate: Callable[[Union[IndexObjUnion, TraversedTreeTup], int], bool] = lambda i, d: True,
303303
prune: Callable[[Union[IndexObjUnion, TraversedTreeTup], int], bool] = lambda i, d: False,
304304
depth: int = -1,

0 commit comments

Comments
 (0)