1818from 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
2323if TYPE_CHECKING :
2424from .commit import Commit
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."""
4747mode = 0
4848for iteration ,char in enumerate (reversed (modestr [- 6 :])):
49+ char = cast (Union [str ,int ],char )
4950mode += int (char )<< iteration * 3
5051# END for each char
5152return 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
7274from .import tree
7375return tree .Tree
7476else :
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
7880def utctz_to_altz (utctz :str )-> int :
@@ -116,7 +118,7 @@ def __init__(self, secs_west_of_utc: float, name: Union[None, str] = None) -> No
116118self ._offset = timedelta (seconds = - secs_west_of_utc )
117119self ._name = name or 'fixed'
118120
119- def __reduce__ (self )-> Tuple ['tzoffset' ,Tuple [float ,str ]]:
121+ def __reduce__ (self )-> Tuple [Type [ 'tzoffset' ] ,Tuple [float ,str ]]:
120122return tzoffset , (- self ._offset .total_seconds (),self ._name )
121123
122124def utcoffset (self ,dt )-> timedelta :
@@ -163,18 +165,18 @@ def parse_date(string_date: str) -> Tuple[int, int]:
163165# git time
164166try :
165167if string_date .count (' ' )== 1 and string_date .rfind (':' )== - 1 :
166- timestamp ,offset = string_date .split ()
168+ timestamp ,offset_str = string_date .split ()
167169if timestamp .startswith ('@' ):
168170timestamp = 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 ))
171173else :
172- offset = "+0000" # local time by default
174+ offset_str = "+0000" # local time by default
173175if string_date [- 5 ]in '-+' :
174- offset = verify_utctz (string_date [- 5 :])
176+ offset_str = verify_utctz (string_date [- 5 :])
175177string_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
180182date_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'
239241m = _re_actor_epoch .search (line )
240242if m :
241243actor ,epoch ,offset = m .groups ()