Skip to content

Commit

Permalink
chore: add typing for directly returning functions which are lifted i…
Browse files Browse the repository at this point in the history
…nto components
  • Loading branch information
z3z1ma committed Aug 19, 2024
1 parent 86c6c1d commit e42086e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 36 deletions.
32 changes: 24 additions & 8 deletions src/cdf/core/component/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import typing as t

from .base import Component, Entrypoint, ServiceLevelAgreement
from .operation import Operation
from .pipeline import DataPipeline
from .publisher import DataPublisher
from .service import Service
from .operation import Operation, OperationProto
from .pipeline import DataPipeline, DataPipelineProto
from .publisher import DataPublisher, DataPublisherProto
from .service import Service, ServiceProto

__all__ = [
"DataPipeline",
Expand All @@ -20,10 +20,26 @@
"ServiceLevelAgreement",
]

ServiceDef = t.Union[Service, t.Dict[str, t.Any]]
DataPipelineDef = t.Union[DataPipeline, t.Dict[str, t.Any]]
DataPublisherDef = t.Union[DataPublisher, t.Dict[str, t.Any]]
OperationDef = t.Union[Operation, t.Dict[str, t.Any]]
ServiceDef = t.Union[
Service,
t.Callable[..., ServiceProto],
t.Dict[str, t.Any],
]
DataPipelineDef = t.Union[
DataPipeline,
t.Callable[..., DataPipelineProto],
t.Dict[str, t.Any],
]
DataPublisherDef = t.Union[
DataPublisher,
t.Callable[..., DataPublisherProto],
t.Dict[str, t.Any],
]
OperationDef = t.Union[
Operation,
t.Callable[..., OperationProto],
t.Dict[str, t.Any],
]

TComponent = t.TypeVar("TComponent", bound=t.Union[Component, Entrypoint])
TComponentDef = t.TypeVar(
Expand Down
4 changes: 3 additions & 1 deletion src/cdf/core/component/operation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from .base import Entrypoint

OperationProto = int

class Operation(Entrypoint[int], frozen=True):

class Operation(Entrypoint[OperationProto], frozen=True):
"""A generic callable that returns an exit code."""
25 changes: 12 additions & 13 deletions src/cdf/core/component/pipeline.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import inspect
import typing as t

import pydantic

from .base import Entrypoint, _get_bind_func, _unwrap_entrypoint
from .base import Entrypoint

if t.TYPE_CHECKING:
from dlt.common.destination import Destination as DltDestination
from dlt.common.pipeline import LoadInfo
from dlt.pipeline.pipeline import Pipeline as DltPipeline


DataPipelineProto = t.Tuple[
"DltPipeline",
t.Union[
t.Callable[..., "LoadInfo"],
t.Callable[..., t.Iterator["LoadInfo"]],
], # run
t.List[t.Callable[..., None]], # tests
]


class DataPipeline(
Entrypoint[
t.Tuple[
"DltPipeline",
t.Union[
t.Callable[..., "LoadInfo"],
t.Callable[..., t.Iterator["LoadInfo"]],
],
t.List[t.Callable[..., None]],
]
],
Entrypoint[DataPipelineProto],
frozen=True,
):
"""A data pipeline which loads data from a source to a destination."""
Expand Down
19 changes: 7 additions & 12 deletions src/cdf/core/component/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@

from .base import Entrypoint


def _ping() -> bool:
"""A default preflight check which always returns True."""
return bool("pong")
DataPublisherProto = t.Tuple[
t.Callable[..., None], # run
t.Callable[..., bool], # preflight
t.Optional[t.Callable[..., None]], # success hook
t.Optional[t.Callable[..., None]], # failure hook
]


class DataPublisher(
Entrypoint[
t.Tuple[
t.Callable[..., None], # run
t.Callable[..., bool], # preflight
t.Optional[t.Callable[..., None]], # success hook
t.Optional[t.Callable[..., None]], # failure hook
]
],
Entrypoint[DataPublisherProto],
frozen=True,
):
"""A data publisher which pushes data to an operational system."""
Expand Down
4 changes: 3 additions & 1 deletion src/cdf/core/component/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from .base import Component

ServiceProto = t.Any

class Service(Component[t.Any], frozen=True):

class Service(Component[ServiceProto], frozen=True):
"""A service that the workspace provides. IE an API, database, requests client, etc."""
3 changes: 2 additions & 1 deletion src/cdf/core/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,8 @@ def run():
name="exchangerate_pipeline",
owner="Alex",
description="A test pipeline",
)
),
test_pipeline, # we can use the proto directly with assumptions
],
)

Expand Down

0 comments on commit e42086e

Please sign in to comment.