Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Parallelizable and Collect to structural subtypes #1115

Merged
merged 3 commits into from
Sep 1, 2024
Merged
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
42 changes: 31 additions & 11 deletions hamilton/htypes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import inspect
import sys
import typing
from abc import ABC
from typing import Any, Generator, Optional, Tuple, Type, TypeVar, Union
from typing import Any, Iterable, Optional, Protocol, Tuple, Type, TypeVar, Union

import typing_inspect

Expand Down Expand Up @@ -137,7 +136,11 @@ def types_match(param_type: Type[Type], required_node_type: Any) -> bool:


_sys_version_info = sys.version_info
_version_tuple = (_sys_version_info.major, _sys_version_info.minor, _sys_version_info.micro)
_version_tuple = (
_sys_version_info.major,
_sys_version_info.minor,
_sys_version_info.micro,
)

"""
The following is purely for backwards compatibility
Expand Down Expand Up @@ -269,26 +272,43 @@ def get_type_information(some_type: Any) -> Tuple[Type[Type], list]:


# Type variables for annotations below
T = TypeVar("T")
U = TypeVar("U")
V = TypeVar("V")
SequentialElement = TypeVar("SequentialElement", covariant=True)
ParallelizableElement = TypeVar("ParallelizableElement", covariant=True)
CollectElement = TypeVar("CollectElement", covariant=True)


# TODO -- support sequential operation
# class Sequential(Generator[T, None, None], ABC):
# class Sequential(Iterable[SequentialElement], Protocol[SequentialElement]):
# pass


class Parallelizable(Generator[U, None, None], ABC):
class Parallelizable(Iterable[ParallelizableElement], Protocol[ParallelizableElement]):
"""Marks the output of a function node as parallelizable.

Parallelizable outputs are expected to be iterable, where each element dynamically
generates a node. When using dynamic execution, each of these dynamic nodes can be
executed in parallel.

Because this uses dynamic execution, the builder method `enable_dynamic_execution`
must be called with `allow_experimental_mode=True`.
"""

pass


def is_parallelizable_type(type_: Type) -> bool:
return issubclass(type_, Parallelizable)
return _get_origin(type_) == Parallelizable


class Collect(Generator[V, None, None], ABC):
pass
class Collect(Iterable[CollectElement], Protocol[CollectElement]):
"""Marks a function node parameter as collectable.

Collectable inputs are expected to be iterable, where each element is populated with
the results of dynamic nodes derived from parallelizable outputs.

Because this uses dynamic execution, the builder method `enable_dynamic_execution`
must be called with `allow_experimental_mode=True`.
"""


def check_input_type(node_type: Type, input_value: Any) -> bool:
Expand Down