|
| 1 | +"""Spider client task module.""" |
| 2 | + |
| 3 | +import inspect |
| 4 | +from collections.abc import Callable |
| 5 | +from types import FunctionType, GenericAlias |
| 6 | +from typing import get_args, get_origin |
| 7 | + |
| 8 | +from spider_py import core |
| 9 | +from spider_py.client.data import Data |
| 10 | +from spider_py.core import TaskInput, TaskOutput, TaskOutputData, TaskOutputValue |
| 11 | +from spider_py.type import to_tdl_type_str |
| 12 | + |
| 13 | + |
| 14 | +class TaskContext: |
| 15 | + """Spider task context.""" |
| 16 | + |
| 17 | + # TODO: Implement task context for use in task executor |
| 18 | + |
| 19 | + |
| 20 | +# NOTE: This type alias is for clarification purposes only. It does not enforce static type checks. |
| 21 | +# Instead, we rely on the runtime check to ensure the first argument is `TaskContext`. To statically |
| 22 | +# enforce the first argument to be `TaskContext`, `Protocol` is required, which is not compatible |
| 23 | +# with `Callable` without explicit type casting. |
| 24 | +TaskFunction = Callable[..., object] |
| 25 | + |
| 26 | + |
| 27 | +def _is_tuple(t: type | GenericAlias) -> bool: |
| 28 | + """ |
| 29 | + :param t: |
| 30 | + :return: Whether t is a tuple. |
| 31 | + """ |
| 32 | + return get_origin(t) is tuple |
| 33 | + |
| 34 | + |
| 35 | +def _validate_and_convert_params(signature: inspect.Signature) -> list[TaskInput]: |
| 36 | + """ |
| 37 | + Validates the task parameters and converts them into a list of `core.TaskInput`. |
| 38 | + :param signature: |
| 39 | + :return: The converted task parameters. |
| 40 | + :raises TypeError: If the parameters are invalid. |
| 41 | + """ |
| 42 | + params = list(signature.parameters.values()) |
| 43 | + inputs = [] |
| 44 | + if not params or params[0].annotation is not TaskContext: |
| 45 | + msg = "First argument is not a TaskContext." |
| 46 | + raise TypeError(msg) |
| 47 | + for param in params[1:]: |
| 48 | + if param.kind in {inspect.Parameter.VAR_POSITIONAL, inspect.Parameter.VAR_KEYWORD}: |
| 49 | + msg = "Variadic parameters are not supported." |
| 50 | + raise TypeError(msg) |
| 51 | + if param.annotation is inspect.Parameter.empty: |
| 52 | + msg = "Parameters must have type annotation." |
| 53 | + raise TypeError(msg) |
| 54 | + tdl_type_str = to_tdl_type_str(param.annotation) |
| 55 | + inputs.append(TaskInput(tdl_type_str, None)) |
| 56 | + return inputs |
| 57 | + |
| 58 | + |
| 59 | +def _validate_and_convert_return(signature: inspect.Signature) -> list[TaskOutput]: |
| 60 | + """ |
| 61 | + Validates the task returns and converts them into a list of `core.TaskOutput`. |
| 62 | + :param signature: |
| 63 | + :return: The converted task returns. |
| 64 | + :raises TypeError: If the return type is invalid. |
| 65 | + """ |
| 66 | + returns = signature.return_annotation |
| 67 | + outputs = [] |
| 68 | + if returns is inspect.Parameter.empty: |
| 69 | + msg = "Return type must have type annotation." |
| 70 | + raise TypeError(msg) |
| 71 | + |
| 72 | + if not _is_tuple(returns): |
| 73 | + tdl_type_str = to_tdl_type_str(returns) |
| 74 | + if returns is Data: |
| 75 | + outputs.append(TaskOutput(tdl_type_str, TaskOutputData())) |
| 76 | + else: |
| 77 | + outputs.append(TaskOutput(tdl_type_str, TaskOutputValue())) |
| 78 | + return outputs |
| 79 | + |
| 80 | + args = get_args(returns) |
| 81 | + if Ellipsis in args: |
| 82 | + msg = "Variable-length tuple return types are not supported." |
| 83 | + raise TypeError(msg) |
| 84 | + for arg in args: |
| 85 | + tdl_type_str = to_tdl_type_str(arg) |
| 86 | + if arg is Data: |
| 87 | + outputs.append(TaskOutput(tdl_type_str, TaskOutputData())) |
| 88 | + else: |
| 89 | + outputs.append(TaskOutput(tdl_type_str, TaskOutputValue())) |
| 90 | + return outputs |
| 91 | + |
| 92 | + |
| 93 | +def create_task(func: TaskFunction) -> core.Task: |
| 94 | + """ |
| 95 | + Creates a core Task object from the task function. |
| 96 | + :param func: |
| 97 | + :return: The created core Task object. |
| 98 | + :raise TypeError: If the function signature contains unsupported types. |
| 99 | + """ |
| 100 | + if not isinstance(func, FunctionType): |
| 101 | + msg = "`func` is not a function." |
| 102 | + raise TypeError(msg) |
| 103 | + signature = inspect.signature(func) |
| 104 | + return core.Task( |
| 105 | + function_name=func.__qualname__, |
| 106 | + task_inputs=_validate_and_convert_params(signature), |
| 107 | + task_outputs=_validate_and_convert_return(signature), |
| 108 | + ) |
0 commit comments