Skip to content

Commit 9f63554

Browse files
committed
chore: modernize Python code using 3.10+ annotations
1 parent bce5b7f commit 9f63554

18 files changed

+175
-157
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ description = "Add your description here"
1717
readme = "README.md"
1818
requires-python = ">=3.9"
1919
dependencies = [
20+
"eval-type-backport>=0.2.2 ; python_full_version < '3.10'",
2021
"llama-index-instrumentation>=0.1.0",
21-
"pydantic>=2.11.5"
22+
"pydantic>=2.11.5",
2223
]
2324

2425
[tool.hatch.build.targets.wheel]

src/workflows/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
"WorkflowRuntimeError",
3232
"WorkflowTimeoutError",
3333
"WorkflowValidationError",
34-
"draw_all_possible_flows",
35-
"draw_most_recent_execution",
3634
"step",
3735
"Context",
3836
"InputRequiredEvent",

src/workflows/checkpointer.py

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1+
from __future__ import annotations
2+
13
import asyncio
24
import functools
35
import uuid
46
from typing import (
57
TYPE_CHECKING,
68
Any,
79
Awaitable,
8-
Dict,
9-
List,
10-
Optional,
1110
Protocol,
12-
Set,
1311
Type,
1412
)
1513

@@ -33,20 +31,20 @@ class CheckpointCallback(Protocol):
3331
def __call__(
3432
self,
3533
run_id: str,
36-
last_completed_step: Optional[str],
37-
input_ev: Optional[Event],
38-
output_ev: Optional[Event],
34+
last_completed_step: str | None,
35+
input_ev: Event | None,
36+
output_ev: Event | None,
3937
ctx: "Context",
4038
) -> Awaitable[None]: ...
4139

4240

4341
class Checkpoint(BaseModel):
4442
model_config = ConfigDict(arbitrary_types_allowed=True)
4543
id_: str = Field(default_factory=lambda: str(uuid.uuid4()))
46-
last_completed_step: Optional[str]
47-
input_event: Optional[Event]
48-
output_event: Optional[Event]
49-
ctx_state: Dict[str, Any]
44+
last_completed_step: str | None
45+
input_event: Event | None
46+
output_event: Event | None
47+
ctx_state: dict[str, Any]
5048

5149

5250
class WorkflowCheckpointer:
@@ -63,8 +61,8 @@ class WorkflowCheckpointer:
6361
def __init__(
6462
self,
6563
workflow: "Workflow",
66-
checkpoint_serializer: Optional[BaseSerializer] = None,
67-
disabled_steps: List[str] = [],
64+
checkpoint_serializer: BaseSerializer | None = None,
65+
disabled_steps: list[str] = [],
6866
):
6967
"""
7068
Create a WorkflowCheckpointer object.
@@ -73,15 +71,15 @@ def __init__(
7371
workflow (Workflow): The wrapped workflow.
7472
checkpoint_serializer (Optional[BaseSerializer], optional): The serializer to use
7573
for serializing associated `Context` of a Workflow run. Defaults to None.
76-
disabled_steps (List[str], optional): Steps for which to disable checkpointing. Defaults to [].
74+
disabled_steps (list[str], optional): Steps for which to disable checkpointing. Defaults to [].
7775
7876
"""
79-
self._checkpoints: Dict[str, List[Checkpoint]] = {}
77+
self._checkpoints: dict[str, list[Checkpoint]] = {}
8078
self._checkpoint_serializer = checkpoint_serializer or JsonSerializer()
8179
self._lock: asyncio.Lock = asyncio.Lock()
8280

8381
self.workflow = workflow
84-
self.enabled_checkpoints: Set[str] = {
82+
self.enabled_checkpoints: set[str] = {
8583
k for k in workflow._get_steps() if k != "_done"
8684
}
8785
for step_name in disabled_steps:
@@ -122,17 +120,17 @@ def run_from(self, checkpoint: Checkpoint, **kwargs: Any) -> "WorkflowHandler":
122120
)
123121

124122
@property
125-
def checkpoints(self) -> Dict[str, List[Checkpoint]]:
123+
def checkpoints(self) -> dict[str, list[Checkpoint]]:
126124
return self._checkpoints
127125

128126
def new_checkpoint_callback_for_run(self) -> CheckpointCallback:
129127
"""Closure to generate a new `CheckpointCallback` with a unique run-id."""
130128

131129
async def _create_checkpoint(
132130
run_id: str,
133-
last_completed_step: Optional[str],
134-
input_ev: Optional[Event],
135-
output_ev: Optional[Event],
131+
last_completed_step: str | None,
132+
input_ev: Event | None,
133+
output_ev: Event | None,
136134
ctx: "Context",
137135
) -> None:
138136
"""Build a checkpoint around the last completed step."""
@@ -156,9 +154,9 @@ async def _create_checkpoint(
156154
def _checkpoint_filter_condition(
157155
self,
158156
ckpt: Checkpoint,
159-
last_completed_step: Optional[str],
160-
input_event_type: Optional[Type[Event]],
161-
output_event_type: Optional[Type[Event]],
157+
last_completed_step: str | None,
158+
input_event_type: Type[Event] | None,
159+
output_event_type: Type[Event] | None,
162160
) -> bool:
163161
if last_completed_step and ckpt.last_completed_step is not last_completed_step:
164162
return False
@@ -170,11 +168,11 @@ def _checkpoint_filter_condition(
170168

171169
def filter_checkpoints(
172170
self,
173-
run_id: Optional[str] = None,
174-
last_completed_step: Optional[str] = None,
175-
input_event_type: Optional[Type[Event]] = None,
176-
output_event_type: Optional[Type[Event]] = None,
177-
) -> List[Checkpoint]:
171+
run_id: str | None = None,
172+
last_completed_step: str | None = None,
173+
input_event_type: Type[Event] | None = None,
174+
output_event_type: Type[Event] | None = None,
175+
) -> list[Checkpoint]:
178176
"""Returns a list of Checkpoint's based on user provided filters."""
179177
if (
180178
not run_id

0 commit comments

Comments
 (0)