Skip to content

Commit f875dde

Browse files
committed
Mypy fixes
1 parent 9c32553 commit f875dde

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

‎git/objects/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def _set_cache_(self, attr: str) -> None:
8989
"""Retrieve object information"""
9090
if attr == "size":
9191
oinfo = self.repo.odb.info(self.binsha)
92-
self.size = oinfo.size
92+
self.size = oinfo.size # type: int
9393
# assert oinfo.type == self.type, _assertion_msg_format % (self.binsha, oinfo.type, self.type)
9494
else:
9595
super(Object, self)._set_cache_(attr)

‎git/objects/tag.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
if TYPE_CHECKING:
1515
from git.repo import Repo
1616
from git.util import Actor
17+
from .commit import Commit
18+
from .blob import Blob
19+
from .tree import Tree
1720

1821
__all__ = ("TagObject", )
1922

@@ -42,7 +45,7 @@ def __init__(self, repo: 'Repo', binsha: bytes, object: Union[None, base.Object]
4245
authored_date is in, in a format similar to time.altzone"""
4346
super(TagObject, self).__init__(repo, binsha)
4447
if object is not None:
45-
self.object = object
48+
self.object = object # type: Union['Commit', 'Blob', 'Tree', 'TagObject']
4649
if tag is not None:
4750
self.tag = tag
4851
if tagger is not None:
@@ -62,8 +65,9 @@ def _set_cache_(self, attr: str) -> None:
6265

6366
_obj, hexsha = lines[0].split(" ")
6467
_type_token, type_name = lines[1].split(" ")
68+
object_type = get_object_type_by_name(type_name.encode('ascii'))
6569
self.object = \
66-
get_object_type_by_name(type_name.encode('ascii'))(self.repo, hex_to_bin(hexsha))
70+
object_type(self.repo, hex_to_bin(hexsha))
6771

6872
self.tag = lines[2][4:] # tag <tag name>
6973

‎git/objects/util.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from datetime import datetime, timedelta, tzinfo
1919

2020
# typing ------------------------------------------------------------
21-
from typing import Literal, TYPE_CHECKING, Tuple, Union
21+
from typing import Literal, TYPE_CHECKING, Tuple, Type, Union, cast
2222

2323
if TYPE_CHECKING:
2424
from .commit import Commit
@@ -36,7 +36,7 @@
3636
#{ Functions
3737

3838

39-
def mode_str_to_int(modestr: str) -> int:
39+
def mode_str_to_int(modestr: Union[bytes, str]) -> int:
4040
"""
4141
:param modestr: string like 755 or 644 or 100644 - only the last 6 chars will be used
4242
:return:
@@ -46,12 +46,14 @@ def mode_str_to_int(modestr: str) -> int:
4646
for example."""
4747
mode = 0
4848
for iteration, char in enumerate(reversed(modestr[-6:])):
49+
char = cast(Union[str, int], char)
4950
mode += int(char) << iteration * 3
5051
# END for each char
5152
return mode
5253

5354

54-
def get_object_type_by_name(object_type_name: str) -> Union['Commit', 'TagObject', 'Tree', 'Blob']:
55+
def get_object_type_by_name(object_type_name: bytes
56+
) -> Union[Type['Commit'], Type['TagObject'], Type['Tree'], Type['Blob']]:
5557
"""
5658
:return: type suitable to handle the given object type name.
5759
Use the type to create new instances.
@@ -72,7 +74,7 @@ def get_object_type_by_name(object_type_name: str) -> Union['Commit', 'TagObject
7274
from . import tree
7375
return tree.Tree
7476
else:
75-
raise ValueError("Cannot handle unknown object type: %s" % object_type_name)
77+
raise ValueError("Cannot handle unknown object type: %s" % object_type_name.decode())
7678

7779

7880
def utctz_to_altz(utctz: str) -> int:
@@ -116,7 +118,7 @@ def __init__(self, secs_west_of_utc: float, name: Union[None, str] = None) -> No
116118
self._offset = timedelta(seconds=-secs_west_of_utc)
117119
self._name = name or 'fixed'
118120

119-
def __reduce__(self) -> Tuple['tzoffset', Tuple[float, str]]:
121+
def __reduce__(self) -> Tuple[Type['tzoffset'], Tuple[float, str]]:
120122
return tzoffset, (-self._offset.total_seconds(), self._name)
121123

122124
def utcoffset(self, dt) -> timedelta:
@@ -163,18 +165,18 @@ def parse_date(string_date: str) -> Tuple[int, int]:
163165
# git time
164166
try:
165167
if string_date.count(' ') == 1 and string_date.rfind(':') == -1:
166-
timestamp, offset = string_date.split()
168+
timestamp, offset_str = string_date.split()
167169
if timestamp.startswith('@'):
168170
timestamp = timestamp[1:]
169-
timestamp = int(timestamp)
170-
return timestamp, utctz_to_altz(verify_utctz(offset))
171+
timestamp_int = int(timestamp)
172+
return timestamp_int, utctz_to_altz(verify_utctz(offset_str))
171173
else:
172-
offset = "+0000" # local time by default
174+
offset_str = "+0000" # local time by default
173175
if string_date[-5] in '-+':
174-
offset = verify_utctz(string_date[-5:])
176+
offset_str = verify_utctz(string_date[-5:])
175177
string_date = string_date[:-6] # skip space as well
176178
# END split timezone info
177-
offset = utctz_to_altz(offset)
179+
offset = utctz_to_altz(offset_str)
178180

179181
# now figure out the date and time portion - split time
180182
date_formats = []
@@ -235,7 +237,7 @@ def parse_actor_and_date(line: str) -> Tuple[Actor, int, int]:
235237
author Tom Preston-Werner <tom@mojombo.com> 1191999972 -0700
236238
237239
:return: [Actor, int_seconds_since_epoch, int_timezone_offset]"""
238-
actor, epoch, offset = '', 0, 0
240+
actor, epoch, offset = '', '0', '0'
239241
m = _re_actor_epoch.search(line)
240242
if m:
241243
actor, epoch, offset = m.groups()

0 commit comments

Comments
 (0)