From 21f73c5340d4c2aaa41f366524b8b902cf54bff8 Mon Sep 17 00:00:00 2001 From: Klaus Rettinghaus Date: Wed, 31 Jul 2024 15:22:21 +0200 Subject: [PATCH 1/2] style: adding type hints --- edtf/convert.py | 13 ++++++------- edtf/jdutil.py | 16 ++++++++-------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/edtf/convert.py b/edtf/convert.py index a294462..581b76a 100644 --- a/edtf/convert.py +++ b/edtf/convert.py @@ -39,11 +39,10 @@ def dt_to_struct_time(dt): return struct_time( [dt.year, dt.month, dt.day] + TIME_EMPTY_TIME + TIME_EMPTY_EXTRAS ) - else: - raise NotImplementedError(f"Cannot convert {type(dt)} to `struct_time`") + raise NotImplementedError(f"Cannot convert {type(dt)} to `struct_time`") -def struct_time_to_date(st): +def struct_time_to_date(st: struct_time) -> date: """ Return a `datetime.date` representing the provided `struct_time. @@ -52,7 +51,7 @@ def struct_time_to_date(st): return date(*st[:3]) -def struct_time_to_datetime(st): +def struct_time_to_datetime(st: struct_time) -> datetime: """ Return a `datetime.datetime` representing the provided `struct_time. @@ -61,7 +60,7 @@ def struct_time_to_datetime(st): return datetime(*st[:6]) -def trim_struct_time(st, strip_time=False): +def trim_struct_time(st: struct_time, strip_time=False) -> struct_time: """ Return a `struct_time` based on the one provided but with the extra fields `tm_wday`, `tm_yday`, and `tm_isdst` reset to default values. @@ -75,7 +74,7 @@ def trim_struct_time(st, strip_time=False): return struct_time(list(st[:6]) + TIME_EMPTY_EXTRAS) -def struct_time_to_jd(st): +def struct_time_to_jd(st: struct_time) -> float: """ Return a float number representing the Julian Date for the given `struct_time`. @@ -91,7 +90,7 @@ def struct_time_to_jd(st): return jdutil.date_to_jd(year, month, day) -def jd_to_struct_time(jd): +def jd_to_struct_time(jd: float) -> struct_time: """ Return a `struct_time` converted from a Julian Date float number. diff --git a/edtf/jdutil.py b/edtf/jdutil.py index 16cd312..7c0a3bd 100644 --- a/edtf/jdutil.py +++ b/edtf/jdutil.py @@ -18,7 +18,7 @@ # time deltas if one date is from before 10-15-1582. -def mjd_to_jd(mjd): +def mjd_to_jd(mjd: float) -> float: """ Convert Modified Julian Day to Julian Day. @@ -37,7 +37,7 @@ def mjd_to_jd(mjd): return mjd + 2400000.5 -def jd_to_mjd(jd): +def jd_to_mjd(jd: float) -> float: """ Convert Julian Day to Modified Julian Day @@ -55,7 +55,7 @@ def jd_to_mjd(jd): return jd - 2400000.5 -def date_to_jd(year, month, day): +def date_to_jd(year: int, month: int, day: float) -> float: """ Convert a date to Julian Day. @@ -117,7 +117,7 @@ def date_to_jd(year, month, day): return jd -def jd_to_date(jd): +def jd_to_date(jd: float) -> tuple: """ Convert Julian Day to date. @@ -175,7 +175,7 @@ def jd_to_date(jd): return year, month, day -def hmsm_to_days(hour=0, min=0, sec=0, micro=0): +def hmsm_to_days(hour: int = 0, min: int = 0, sec: int = 0, micro: int = 0) -> float: """ Convert hours, minutes, seconds, and microseconds to fractional days. @@ -262,7 +262,7 @@ def days_to_hmsm(days): return int(hour), int(min), int(sec), int(micro) -def datetime_to_jd(date): +def datetime_to_jd(date: dt.datetime) -> float: """ Convert a `datetime.datetime` object to Julian Day. @@ -291,7 +291,7 @@ def datetime_to_jd(date): return date_to_jd(date.year, date.month, days) -def jd_to_datetime(jd): +def jd_to_datetime(jd: float) -> dt.datetime: """ Convert a Julian Day to an `jdutil.datetime` object. @@ -321,7 +321,7 @@ def jd_to_datetime(jd): return datetime(year, month, day, hour, min, sec, micro) -def timedelta_to_days(td): +def timedelta_to_days(td: dt.timedelta) -> float: """ Convert a `datetime.timedelta` object to a total number of days. From 2e69104706f3c03d5f59faf56fa3df98daa0dc33 Mon Sep 17 00:00:00 2001 From: Klaus Rettinghaus Date: Wed, 31 Jul 2024 15:29:52 +0200 Subject: [PATCH 2/2] style: adding one more hint --- edtf/convert.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/edtf/convert.py b/edtf/convert.py index 581b76a..ee03f36 100644 --- a/edtf/convert.py +++ b/edtf/convert.py @@ -60,7 +60,7 @@ def struct_time_to_datetime(st: struct_time) -> datetime: return datetime(*st[:6]) -def trim_struct_time(st: struct_time, strip_time=False) -> struct_time: +def trim_struct_time(st: struct_time, strip_time: bool = False) -> struct_time: """ Return a `struct_time` based on the one provided but with the extra fields `tm_wday`, `tm_yday`, and `tm_isdst` reset to default values.