18
18
from datetime import datetime , timedelta , tzinfo
19
19
20
20
# typing ------------------------------------------------------------
21
- from typing import Literal , TYPE_CHECKING , Tuple , Union
21
+ from typing import Literal , TYPE_CHECKING , Tuple , Type , Union , cast
22
22
23
23
if TYPE_CHECKING :
24
24
from .commit import Commit
36
36
#{ Functions
37
37
38
38
39
- def mode_str_to_int (modestr : str ) -> int :
39
+ def mode_str_to_int (modestr : Union [ bytes , str ] ) -> int :
40
40
"""
41
41
:param modestr: string like 755 or 644 or 100644 - only the last 6 chars will be used
42
42
:return:
@@ -46,12 +46,14 @@ def mode_str_to_int(modestr: str) -> int:
46
46
for example."""
47
47
mode = 0
48
48
for iteration , char in enumerate (reversed (modestr [- 6 :])):
49
+ char = cast (Union [str , int ], char )
49
50
mode += int (char ) << iteration * 3
50
51
# END for each char
51
52
return mode
52
53
53
54
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' ]]:
55
57
"""
56
58
:return: type suitable to handle the given object type name.
57
59
Use the type to create new instances.
@@ -72,7 +74,7 @@ def get_object_type_by_name(object_type_name: str) -> Union['Commit', 'TagObject
72
74
from . import tree
73
75
return tree .Tree
74
76
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 () )
76
78
77
79
78
80
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
116
118
self ._offset = timedelta (seconds = - secs_west_of_utc )
117
119
self ._name = name or 'fixed'
118
120
119
- def __reduce__ (self ) -> Tuple ['tzoffset' , Tuple [float , str ]]:
121
+ def __reduce__ (self ) -> Tuple [Type [ 'tzoffset' ] , Tuple [float , str ]]:
120
122
return tzoffset , (- self ._offset .total_seconds (), self ._name )
121
123
122
124
def utcoffset (self , dt ) -> timedelta :
@@ -163,18 +165,18 @@ def parse_date(string_date: str) -> Tuple[int, int]:
163
165
# git time
164
166
try :
165
167
if string_date .count (' ' ) == 1 and string_date .rfind (':' ) == - 1 :
166
- timestamp , offset = string_date .split ()
168
+ timestamp , offset_str = string_date .split ()
167
169
if timestamp .startswith ('@' ):
168
170
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 ))
171
173
else :
172
- offset = "+0000" # local time by default
174
+ offset_str = "+0000" # local time by default
173
175
if string_date [- 5 ] in '-+' :
174
- offset = verify_utctz (string_date [- 5 :])
176
+ offset_str = verify_utctz (string_date [- 5 :])
175
177
string_date = string_date [:- 6 ] # skip space as well
176
178
# END split timezone info
177
- offset = utctz_to_altz (offset )
179
+ offset = utctz_to_altz (offset_str )
178
180
179
181
# now figure out the date and time portion - split time
180
182
date_formats = []
@@ -235,7 +237,7 @@ def parse_actor_and_date(line: str) -> Tuple[Actor, int, int]:
235
237
author Tom Preston-Werner <tom@mojombo.com> 1191999972 -0700
236
238
237
239
:return: [Actor, int_seconds_since_epoch, int_timezone_offset]"""
238
- actor , epoch , offset = '' , 0 , 0
240
+ actor , epoch , offset = '' , '0' , '0'
239
241
m = _re_actor_epoch .search (line )
240
242
if m :
241
243
actor , epoch , offset = m .groups ()
0 commit comments