Skip to content

Commit 1d56fbb

Browse files
committed
Refactor Duration to store only seconds internally
- Store only seconds field instead of days/hours/minutes/seconds - Accept int | float in all from_* constructors (from_seconds, from_minutes, from_hours, from_days) - Convert to seconds using direct multiplication instead of timedelta - Use ValidationError instead of ValueError for negative values - Simplify validation to check only seconds < 0
1 parent d81fa64 commit 1d56fbb

2 files changed

Lines changed: 18 additions & 26 deletions

File tree

src/aws_durable_execution_sdk_python/config.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
import random
66
from dataclasses import dataclass, field
7-
from datetime import timedelta
87
from enum import Enum, StrEnum
98
from typing import TYPE_CHECKING, Generic, TypeVar
109

10+
from aws_durable_execution_sdk_python.exceptions import ValidationError
11+
1112
P = TypeVar("P") # Payload type
1213
R = TypeVar("R") # Result type
1314
T = TypeVar("T")
@@ -26,49 +27,40 @@
2627
Numeric = int | float # deliberately leaving off complex
2728

2829

29-
@dataclass
30+
@dataclass(frozen=True)
3031
class Duration:
31-
"""Represents a duration with multiple time units."""
32+
"""Represents a duration stored as total seconds."""
3233

33-
days: int = 0
34-
hours: int = 0
35-
minutes: int = 0
3634
seconds: int = 0
3735

3836
def __post_init__(self):
39-
if any(v < 0 for v in [self.days, self.hours, self.minutes, self.seconds]):
40-
msg = "All duration values must be positive"
41-
raise ValueError(msg)
37+
if self.seconds < 0:
38+
msg = "Duration seconds must be positive"
39+
raise ValidationError(msg)
4240

4341
def to_seconds(self) -> int:
4442
"""Convert the duration to total seconds."""
45-
td = timedelta(
46-
days=self.days,
47-
hours=self.hours,
48-
minutes=self.minutes,
49-
seconds=self.seconds,
50-
)
51-
return int(td.total_seconds())
43+
return self.seconds
5244

5345
@classmethod
54-
def from_seconds(cls, value: int) -> Duration:
46+
def from_seconds(cls, value: float) -> Duration:
5547
"""Create a Duration from total seconds."""
56-
return cls(seconds=value)
48+
return cls(seconds=int(value))
5749

5850
@classmethod
59-
def from_minutes(cls, value: int) -> Duration:
51+
def from_minutes(cls, value: float) -> Duration:
6052
"""Create a Duration from minutes."""
61-
return cls(minutes=value)
53+
return cls(seconds=int(value * 60))
6254

6355
@classmethod
64-
def from_hours(cls, value: int) -> Duration:
56+
def from_hours(cls, value: float) -> Duration:
6557
"""Create a Duration from hours."""
66-
return cls(hours=value)
58+
return cls(seconds=int(value * 3600))
6759

6860
@classmethod
69-
def from_days(cls, value: int) -> Duration:
61+
def from_days(cls, value: float) -> Duration:
7062
"""Create a Duration from days."""
71-
return cls(days=value)
63+
return cls(seconds=int(value * 86400))
7264

7365

7466
@dataclass(frozen=True)

tests/operation/callback_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
StepConfig,
1212
WaitForCallbackConfig,
1313
)
14-
from aws_durable_execution_sdk_python.exceptions import CallbackError
14+
from aws_durable_execution_sdk_python.exceptions import CallbackError, ValidationError
1515
from aws_durable_execution_sdk_python.identifier import OperationIdentifier
1616
from aws_durable_execution_sdk_python.lambda_service import (
1717
CallbackDetails,
@@ -333,7 +333,7 @@ def test_create_callback_handler_with_none_operation_in_result():
333333
def test_create_callback_handler_with_negative_timeouts():
334334
"""Test create_callback_handler with negative timeout values in config."""
335335
# Duration now validates that all values must be positive
336-
with pytest.raises(ValueError, match="All duration values must be positive"):
336+
with pytest.raises(ValidationError, match="Duration seconds must be positive"):
337337
CallbackConfig(
338338
timeout=Duration(seconds=-100), heartbeat_timeout=Duration(seconds=-50)
339339
)

0 commit comments

Comments
 (0)