Skip to content

Commit ed73ab6

Browse files
authored
fix: enable ruff linting rules (#51)
1 parent 3c0ae82 commit ed73ab6

File tree

16 files changed

+41
-64
lines changed

16 files changed

+41
-64
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ exclude_lines = ["no cov", "if __name__ == .__main__.:", "if TYPE_CHECKING:"]
7272
line-length = 88
7373

7474
[tool.ruff.lint]
75-
preview = false
75+
preview = true
7676

7777
[tool.ruff.lint.per-file-ignores]
7878
"tests/**" = [
@@ -81,6 +81,7 @@ preview = false
8181
"ARG005",
8282
"S101",
8383
"PLR2004",
84+
"PLR6301",
8485
"SIM117",
8586
"TRY301",
8687
]

src/aws_durable_execution_sdk_python/concurrency.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from enum import Enum
1313
from typing import TYPE_CHECKING, Generic, Self, TypeVar
1414

15+
from aws_durable_execution_sdk_python.config import ChildConfig
1516
from aws_durable_execution_sdk_python.exceptions import (
1617
InvalidStateError,
1718
SuspendExecution,
@@ -23,7 +24,7 @@
2324
if TYPE_CHECKING:
2425
from collections.abc import Callable
2526

26-
from aws_durable_execution_sdk_python.config import ChildConfig, CompletionConfig
27+
from aws_durable_execution_sdk_python.config import CompletionConfig
2728
from aws_durable_execution_sdk_python.lambda_service import OperationSubType
2829
from aws_durable_execution_sdk_python.serdes import SerDes
2930
from aws_durable_execution_sdk_python.state import ExecutionState
@@ -92,7 +93,7 @@ def from_dict(cls, data: dict) -> BatchItem[R]:
9293

9394

9495
@dataclass(frozen=True)
95-
class BatchResult(Generic[R], BatchResultProtocol[R]):
96+
class BatchResult(Generic[R], BatchResultProtocol[R]): # noqa: PYI059
9697
all: list[BatchItem[R]]
9798
completion_reason: CompletionReason
9899

@@ -353,10 +354,7 @@ def should_complete(self) -> bool:
353354
# Impossible to succeed condition
354355
# TODO: should this keep running? TS doesn't currently handle this either.
355356
remaining_tasks = self.total_tasks - self.success_count - self.failure_count
356-
if self.success_count + remaining_tasks < self.min_successful:
357-
return True
358-
359-
return False
357+
return self.success_count + remaining_tasks < self.min_successful
360358

361359
def is_all_completed(self) -> bool:
362360
"""True if all tasks completed successfully."""
@@ -493,11 +491,7 @@ def __init__(
493491
self._suspend_exception: SuspendExecution | None = None
494492

495493
# ExecutionCounters will keep track of completion criteria and on-going counters
496-
min_successful = (
497-
self.completion_config.min_successful
498-
if self.completion_config.min_successful
499-
else len(self.executables)
500-
)
494+
min_successful = self.completion_config.min_successful or len(self.executables)
501495
tolerated_failure_count = self.completion_config.tolerated_failure_count
502496
tolerated_failure_percentage = (
503497
self.completion_config.tolerated_failure_percentage
@@ -532,9 +526,7 @@ def execute(
532526
"▶️ Executing concurrent operation, items: %d", len(self.executables)
533527
)
534528

535-
max_workers = (
536-
self.max_concurrency if self.max_concurrency else len(self.executables)
537-
)
529+
max_workers = self.max_concurrency or len(self.executables)
538530

539531
self.executables_with_state = [
540532
ExecutableWithState(executable=exe) for exe in self.executables
@@ -588,7 +580,7 @@ def should_execution_suspend(self) -> SuspendResult:
588580
) = None
589581

590582
for exe_state in self.executables_with_state:
591-
if exe_state.status in (BranchStatus.PENDING, BranchStatus.RUNNING):
583+
if exe_state.status in {BranchStatus.PENDING, BranchStatus.RUNNING}:
592584
# Exit here! Still have tasks that can make progress, don't suspend.
593585
return SuspendResult.do_not_suspend()
594586
if exe_state.status is BranchStatus.SUSPENDED_WITH_TIMEOUT:
@@ -692,7 +684,6 @@ def _execute_item_in_child_context(
692684
executable: Executable[CallableType],
693685
) -> ResultType:
694686
"""Execute a single item in a child context."""
695-
from aws_durable_execution_sdk_python.config import ChildConfig
696687

697688
def execute_in_child_context(child_context: DurableContext) -> ResultType:
698689
return self.execute_item(child_context, executable)

src/aws_durable_execution_sdk_python/context.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def function_with_arguments(child_context: DurableContext):
9494
return wrapper
9595

9696

97-
class Callback(Generic[T], CallbackProtocol[T]):
97+
class Callback(Generic[T], CallbackProtocol[T]): # noqa: PYI059
9898
"""A future that will block on result() until callback_id returns."""
9999

100100
def __init__(
@@ -162,13 +162,9 @@ def __init__(
162162
execution_arn=state.durable_execution_arn, parent_id=parent_id
163163
)
164164
self._log_info = log_info
165-
self.logger: Logger = (
166-
logger
167-
if logger
168-
else Logger.from_log_info(
169-
logger=logging.getLogger(),
170-
info=log_info,
171-
)
165+
self.logger: Logger = logger or Logger.from_log_info(
166+
logger=logging.getLogger(),
167+
info=log_info,
172168
)
173169

174170
# region factories
@@ -207,7 +203,7 @@ def _resolve_step_name(name: str | None, func: Callable) -> str | None:
207203
str | None: The provided name, and if that doesn't exist the callable function's name if it has one.
208204
"""
209205
# callable's name will override name if name is falsy ('' or None)
210-
return name if name else getattr(func, "_original_name", None)
206+
return name or getattr(func, "_original_name", None)
211207

212208
def set_logger(self, new_logger: LoggerInterface):
213209
"""Set the logger for the current context."""

src/aws_durable_execution_sdk_python/operation/child.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ def child_handler(
6464
)
6565
if checkpointed_result.is_failed():
6666
checkpointed_result.raise_callable_error()
67-
sub_type = (
68-
config.sub_type if config.sub_type else OperationSubType.RUN_IN_CHILD_CONTEXT
69-
)
67+
sub_type = config.sub_type or OperationSubType.RUN_IN_CHILD_CONTEXT
7068

7169
if not checkpointed_result.is_existent():
7270
start_operation = OperationUpdate.create_context_start(

src/aws_durable_execution_sdk_python/operation/map.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
R = TypeVar("R")
3030

3131

32-
class MapExecutor(Generic[T, R], ConcurrentExecutor[Callable, R]):
32+
class MapExecutor(Generic[T, R], ConcurrentExecutor[Callable, R]): # noqa: PYI059
3333
def __init__(
3434
self,
3535
executables: list[Executable[Callable]],
@@ -94,6 +94,6 @@ def map_handler(
9494
) -> BatchResult[R]:
9595
"""Execute a callable for each item in parallel."""
9696
executor: MapExecutor[T, R] = MapExecutor.from_items(
97-
items=items, func=func, config=config if config else MapConfig()
97+
items=items, func=func, config=config or MapConfig()
9898
)
9999
return executor.execute(execution_state, run_in_child_context)

src/aws_durable_execution_sdk_python/operation/parallel.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def from_callables(
6464
serdes=config.serdes,
6565
)
6666

67-
def execute_item(self, child_context, executable: Executable[Callable]) -> R:
67+
def execute_item(self, child_context, executable: Executable[Callable]) -> R: # noqa: PLR6301
6868
logger.debug("🔀 Processing parallel branch: %s", executable.index)
6969
result: R = executable.func(child_context)
7070
logger.debug("✅ Processed parallel branch: %s", executable.index)
@@ -80,7 +80,5 @@ def parallel_handler(
8080
],
8181
) -> BatchResult[R]:
8282
"""Execute multiple operations in parallel."""
83-
executor = ParallelExecutor.from_callables(
84-
callables, config if config else ParallelConfig()
85-
)
83+
executor = ParallelExecutor.from_callables(callables, config or ParallelConfig())
8684
return executor.execute(execution_state, run_in_child_context)

src/aws_durable_execution_sdk_python/operation/step.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,7 @@ def retry_handler(
183183
"""Checkpoint and suspend for replay if retry required, otherwise raise error."""
184184
error_object = ErrorObject.from_exception(error)
185185

186-
retry_strategy = (
187-
config.retry_strategy if config.retry_strategy else RetryPresets.default()
188-
)
186+
retry_strategy = config.retry_strategy or RetryPresets.default()
189187

190188
retry_attempt: int = (
191189
checkpointed_result.operation.step_details.attempt

src/aws_durable_execution_sdk_python/serdes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ def deserialize(self, data: str, serdes_context: SerDesContext) -> T:
3030

3131

3232
class JsonSerDes(SerDes[T]):
33-
def serialize(self, value: T, _: SerDesContext) -> str:
33+
def serialize(self, value: T, _: SerDesContext) -> str: # noqa: PLR6301
3434
return json.dumps(value)
3535

36-
def deserialize(self, data: str, _: SerDesContext) -> T:
36+
def deserialize(self, data: str, _: SerDesContext) -> T: # noqa: PLR6301
3737
return json.loads(data)
3838

3939

src/aws_durable_execution_sdk_python/state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def is_started_or_ready(self) -> bool:
105105
op = self.operation
106106
if not op:
107107
return False
108-
return op.status in (OperationStatus.STARTED, OperationStatus.READY)
108+
return op.status in {OperationStatus.STARTED, OperationStatus.READY}
109109

110110
def is_pending(self) -> bool:
111111
"""Return True if the checkpointed operation is PENDING."""

tests/context_test.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
from aws_durable_execution_sdk_python.config import (
99
CallbackConfig,
1010
ChildConfig,
11+
InvokeConfig,
1112
MapConfig,
1213
ParallelConfig,
1314
StepConfig,
1415
WaitForConditionConfig,
16+
WaitForConditionDecision,
1517
)
1618
from aws_durable_execution_sdk_python.context import Callback, DurableContext
1719
from aws_durable_execution_sdk_python.exceptions import (
@@ -472,8 +474,6 @@ def test_invoke_basic(mock_handler):
472474
@patch("aws_durable_execution_sdk_python.context.invoke_handler")
473475
def test_invoke_with_name_and_config(mock_handler):
474476
"""Test invoke with name and config."""
475-
from aws_durable_execution_sdk_python.config import InvokeConfig
476-
477477
mock_handler.return_value = "configured_result"
478478
mock_state = Mock(spec=ExecutionState)
479479
mock_state.durable_execution_arn = (
@@ -578,8 +578,6 @@ def test_invoke_with_custom_serdes(mock_handler):
578578
"arn:aws:durable:us-east-1:123456789012:execution/test"
579579
)
580580

581-
from aws_durable_execution_sdk_python.config import InvokeConfig
582-
583581
config = InvokeConfig[dict, dict](
584582
serdes_payload=CustomDictSerDes(),
585583
serdes_result=CustomDictSerDes(),
@@ -1019,8 +1017,6 @@ def test_function(context, item, index, items):
10191017
@patch("aws_durable_execution_sdk_python.context.map_handler")
10201018
def test_map_with_name_and_config(mock_handler):
10211019
"""Test map with name and config."""
1022-
from aws_durable_execution_sdk_python.config import MapConfig
1023-
10241020
mock_handler.return_value = "configured_map_result"
10251021
mock_state = Mock(spec=ExecutionState)
10261022
mock_state.durable_execution_arn = (
@@ -1155,8 +1151,6 @@ def task2(context):
11551151
@patch("aws_durable_execution_sdk_python.context.parallel_handler")
11561152
def test_parallel_with_name_and_config(mock_handler):
11571153
"""Test parallel with name and config."""
1158-
from aws_durable_execution_sdk_python.config import ParallelConfig
1159-
11601154
mock_handler.return_value = "configured_parallel_result"
11611155
mock_state = Mock(spec=ExecutionState)
11621156
mock_state.durable_execution_arn = (
@@ -1492,8 +1486,6 @@ def test_check(state, check_context):
14921486
return state
14931487

14941488
def test_wait_strategy(state, attempt):
1495-
from aws_durable_execution_sdk_python.config import WaitForConditionDecision
1496-
14971489
return WaitForConditionDecision.STOP
14981490

14991491
# Create mock state and context

0 commit comments

Comments
 (0)