Skip to content

Commit 024b696

Browse files
committed
Fix parse_date typing 4
1 parent 2fe13ca commit 024b696

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

‎git/objects/util.py

+7-12
Original file line numberDiff line numberDiff line change
@@ -181,16 +181,15 @@ def parse_date(string_date: Union[str, datetime]) -> Tuple[int, int]:
181181
:raise ValueError: If the format could not be understood
182182
:note: Date can also be YYYY.MM.DD, MM/DD/YYYY and DD.MM.YYYY.
183183
"""
184-
if isinstance(string_date, datetime) and string_date.tzinfo:
185-
utcoffset = string_date.utcoffset()
186-
offset = -int(utcoffset.total_seconds()) if utcoffset else 0
187-
return int(string_date.astimezone(utc).timestamp()), offset
184+
if isinstance(string_date, datetime):
185+
if string_date.tzinfo:
186+
utcoffset = cast(timedelta, string_date.utcoffset()) # typeguard, if tzinfoand is not None
187+
offset = -int(utcoffset.total_seconds())
188+
return int(string_date.astimezone(utc).timestamp()), offset
189+
else:
190+
raise ValueError(f"Unsupported date format or type: {string_date}" % string_date)
188191

189192
else:
190-
assert isinstance(string_date, str), f"string_date={string_date}, type={type(string_date)}" # for mypy
191-
192-
# git time
193-
try:
194193
if string_date.count(' ') == 1 and string_date.rfind(':') == -1:
195194
timestamp, offset_str = string_date.split()
196195
if timestamp.startswith('@'):
@@ -244,13 +243,9 @@ def parse_date(string_date: Union[str, datetime]) -> Tuple[int, int]:
244243
continue
245244
# END exception handling
246245
# END for each fmt
247-
248246
# still here ? fail
249247
raise ValueError("no format matched")
250248
# END handle format
251-
except Exception as e:
252-
raise ValueError("Unsupported date format: %s" % string_date) from e
253-
# END handle exceptions
254249

255250

256251
# precompiled regex

0 commit comments

Comments
 (0)