Skip to content

Commit 90bad6d

Browse files
authored
Merge pull request #540 from opsmill/pog-substract-param-typing
Fix typing for substract params
2 parents 89d4e9f + 751b7da commit 90bad6d

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

infrahub_sdk/timestamp.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@
33
import re
44
import warnings
55
from datetime import datetime, timezone
6-
from typing import Literal
6+
from typing import Literal, TypedDict
77

8+
from typing_extensions import NotRequired
89
from whenever import Date, Instant, LocalDateTime, OffsetDateTime, Time, ZonedDateTime
910

1011
from .exceptions import TimestampFormatError
1112

1213
UTC = timezone.utc # Required for older versions of Python
1314

15+
16+
class SubstractParams(TypedDict):
17+
seconds: NotRequired[float]
18+
minutes: NotRequired[float]
19+
hours: NotRequired[float]
20+
21+
1422
REGEX_MAPPING = {
1523
"seconds": r"(\d+)(s|sec|second|seconds)",
1624
"minutes": r"(\d+)(m|min|minute|minutes)",
@@ -73,14 +81,19 @@ def _parse_string(cls, value: str) -> ZonedDateTime:
7381
except ValueError:
7482
pass
7583

76-
params: dict[str, float] = {}
84+
params: SubstractParams = {}
7785
for key, regex in REGEX_MAPPING.items():
7886
match = re.search(regex, value)
7987
if match:
80-
params[key] = float(match.group(1))
88+
if key == "seconds":
89+
params["seconds"] = float(match.group(1))
90+
elif key == "minutes":
91+
params["minutes"] = float(match.group(1))
92+
elif key == "hours":
93+
params["hours"] = float(match.group(1))
8194

8295
if params:
83-
return ZonedDateTime.now("UTC").subtract(**params) # type: ignore[call-overload]
96+
return ZonedDateTime.now("UTC").subtract(**params)
8497

8598
raise TimestampFormatError(f"Invalid time format for {value}")
8699

0 commit comments

Comments
 (0)