Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions src/workflows/__init__.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,9 @@
from .checkpointer import (
Checkpoint,
WorkflowCheckpointer,
)
from .context import Context
from .context_serializers import (
JsonPickleSerializer,
JsonSerializer,
)
from .decorators import step
from .errors import (
WorkflowRuntimeError,
WorkflowTimeoutError,
WorkflowValidationError,
)
from .events import (
Event,
HumanResponseEvent,
InputRequiredEvent,
StartEvent,
StopEvent,
)
from .workflow import Workflow

__all__ = [
"Context",
"Event",
"StartEvent",
"StopEvent",
"Workflow",
"WorkflowRuntimeError",
"WorkflowTimeoutError",
"WorkflowValidationError",
"step",
"Context",
"InputRequiredEvent",
"HumanResponseEvent",
"JsonPickleSerializer",
"JsonSerializer",
"WorkflowCheckpointer",
"Checkpoint",
]
3 changes: 2 additions & 1 deletion src/workflows/checkpointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
Field,
)

from .context_serializers import BaseSerializer, JsonSerializer
from workflows.context import BaseSerializer, JsonSerializer

from .errors import WorkflowStepDoesNotExistError
from .events import Event

Expand Down
9 changes: 9 additions & 0 deletions src/workflows/context/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from .context import Context
from .serializers import BaseSerializer, JsonSerializer, PickleSerializer

__all__ = [
"Context",
"PickleSerializer",
"JsonSerializer",
"BaseSerializer",
]
23 changes: 12 additions & 11 deletions src/workflows/context.py → src/workflows/context/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@
TypeVar,
)

from .checkpointer import CheckpointCallback
from .context_serializers import BaseSerializer, JsonSerializer
from .decorators import StepConfig
from .errors import (
from workflows.decorators import StepConfig
from workflows.errors import (
ContextSerdeError,
WorkflowCancelledByUser,
WorkflowDone,
WorkflowRuntimeError,
)
from .events import Event, InputRequiredEvent
from .resource import ResourceManager
from .service import ServiceManager
from .types import RunResultT
from workflows.events import Event, InputRequiredEvent
from workflows.resource import ResourceManager
from workflows.service import ServiceManager
from workflows.types import RunResultT

from .serializers import BaseSerializer, JsonSerializer

if TYPE_CHECKING: # pragma: no cover
from .workflow import Workflow
from workflows import Workflow
from workflows.checkpointer import CheckpointCallback

T = TypeVar("T", bound=Event)
EventBuffer = dict[str, list[Event]]
Expand Down Expand Up @@ -544,7 +545,7 @@ def add_step_worker(
config: StepConfig,
stepwise: bool,
verbose: bool,
checkpoint_callback: CheckpointCallback | None,
checkpoint_callback: "CheckpointCallback | None",
run_id: str,
service_manager: ServiceManager,
resource_manager: ResourceManager,
Expand Down Expand Up @@ -573,7 +574,7 @@ async def _step_worker(
config: StepConfig,
stepwise: bool,
verbose: bool,
checkpoint_callback: CheckpointCallback | None,
checkpoint_callback: "CheckpointCallback | None",
run_id: str,
service_manager: ServiceManager,
resource_manager: ResourceManager,
Expand Down
57 changes: 57 additions & 0 deletions src/workflows/context/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from __future__ import annotations

from importlib import import_module
from typing import (
Any,
)


def get_qualified_name(value: Any) -> str:
"""
Get the qualified name of a value.

Args:
value (Any): The value to get the qualified name for.

Returns:
str: The qualified name in the format 'module.class'.

Raises:
AttributeError: If value does not have __module__ or __class__ attributes

"""
try:
return value.__module__ + "." + value.__class__.__name__
except AttributeError as e:
raise AttributeError(f"Object {value} does not have required attributes: {e}")


def import_module_from_qualified_name(qualified_name: str) -> Any:
"""
Import a module from a qualified name.

Args:
qualified_name (str): The fully qualified name of the module to import.

Returns:
Any: The imported module object.

Raises:
ValueError: If qualified_name is empty or malformed
ImportError: If module cannot be imported
AttributeError: If attribute cannot be found in module

"""
if not qualified_name or "." not in qualified_name:
raise ValueError("Qualified name must be in format 'module.attribute'")

module_path = qualified_name.rsplit(".", 1)
try:
module = import_module(module_path[0])
return getattr(module, module_path[1])
except ImportError as e:
raise ImportError(f"Failed to import module {module_path[0]}: {e}")
except AttributeError as e:
raise AttributeError(
f"Attribute {module_path[1]} not found in module {module_path[0]}: {e}"
)
54 changes: 1 addition & 53 deletions src/workflows/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import inspect
from importlib import import_module
from typing import (
Annotated,
Any,
Expand All @@ -14,7 +13,7 @@

try:
from typing import Union
except ImportError:
except ImportError: # pragma: no cover
from typing_extensions import Union

# handle python version compatibility
Expand Down Expand Up @@ -278,54 +277,3 @@ def is_free_function(qualname: str) -> bool:
return False
else:
return toks[-2] == "<locals>"


def get_qualified_name(value: Any) -> str:
"""
Get the qualified name of a value.

Args:
value (Any): The value to get the qualified name for.

Returns:
str: The qualified name in the format 'module.class'.

Raises:
AttributeError: If value does not have __module__ or __class__ attributes

"""
try:
return value.__module__ + "." + value.__class__.__name__
except AttributeError as e:
raise AttributeError(f"Object {value} does not have required attributes: {e}")


def import_module_from_qualified_name(qualified_name: str) -> Any:
"""
Import a module from a qualified name.

Args:
qualified_name (str): The fully qualified name of the module to import.

Returns:
Any: The imported module object.

Raises:
ValueError: If qualified_name is empty or malformed
ImportError: If module cannot be imported
AttributeError: If attribute cannot be found in module

"""
if not qualified_name or "." not in qualified_name:
raise ValueError("Qualified name must be in format 'module.attribute'")

module_path = qualified_name.rsplit(".", 1)
try:
module = import_module(module_path[0])
return getattr(module, module_path[1])
except ImportError as e:
raise ImportError(f"Failed to import module {module_path[0]}: {e}")
except AttributeError as e:
raise AttributeError(
f"Attribute {module_path[1]} not found in module {module_path[0]}: {e}"
)
3 changes: 1 addition & 2 deletions src/workflows/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
from pydantic import ValidationError

from .checkpointer import Checkpoint, CheckpointCallback
from .context import Context
from .context_serializers import BaseSerializer, JsonSerializer
from .context import BaseSerializer, Context, JsonSerializer
from .decorators import StepConfig, step
from .errors import (
WorkflowConfigurationError,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from pydantic import PrivateAttr

from workflows.context_serializers import JsonSerializer
from workflows.context import JsonSerializer
from workflows.events import Event


Expand Down
3 changes: 1 addition & 2 deletions tests/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

import pytest

from workflows.context import Context
from workflows.context_serializers import PickleSerializer
from workflows.context import Context, PickleSerializer
from workflows.decorators import step
from workflows.errors import (
WorkflowCancelledByUser,
Expand Down