Skip to content

_signature_constraints

_extract_function(module_relative_path, function_name, package_name, verbose=False)

Extract function from a module with the same name.

PARAMETER DESCRIPTION
package_name

Example fractal_tasks_core.

TYPE: str

module_relative_path

Example tasks/create_ome_zarr.py.

TYPE: str

function_name

Example create_ome_zarr.

TYPE: str

verbose

TYPE: bool DEFAULT: False

Source code in src/fractal_task_tools/_signature_constraints.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def _extract_function(
    module_relative_path: str,
    function_name: str,
    package_name: str,
    verbose: bool = False,
) -> callable:
    """
    Extract function from a module with the same name.

    Args:
        package_name: Example `fractal_tasks_core`.
        module_relative_path: Example `tasks/create_ome_zarr.py`.
        function_name: Example `create_ome_zarr`.
        verbose:
    """
    if not module_relative_path.endswith(".py"):
        raise ValueError(f"{module_relative_path=} must end with '.py'")
    module_relative_path_no_py = str(
        Path(module_relative_path).with_suffix("")
    )
    module_relative_path_dots = module_relative_path_no_py.replace("/", ".")
    if verbose:
        logging.info(
            f"Now calling `import_module` for "
            f"{package_name}.{module_relative_path_dots}"
        )
    imported_module = import_module(
        f"{package_name}.{module_relative_path_dots}"
    )
    if verbose:
        logging.info(
            f"Now getting attribute {function_name} from "
            f"imported module {imported_module}."
        )
    task_function = getattr(imported_module, function_name)
    return task_function

_validate_function_signature(function)

Validate the function signature.

Implement a set of checks for type hints that do not play well with the creation of JSON Schema, see https://github.com/fractal-analytics-platform/fractal-tasks-core/issues/399.

PARAMETER DESCRIPTION
function

TBD

TYPE: callable

Source code in src/fractal_task_tools/_signature_constraints.py
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def _validate_function_signature(function: callable):
    """
    Validate the function signature.

    Implement a set of checks for type hints that do not play well with the
    creation of JSON Schema, see
    https://github.com/fractal-analytics-platform/fractal-tasks-core/issues/399.

    Args:
        function: TBD
    """
    sig = signature(function)
    for param in sig.parameters.values():

        # CASE 1: Check that name is not forbidden
        if param.name in FORBIDDEN_PARAM_NAMES:
            raise ValueError(
                f"Function {function} has argument with name {param.name}"
            )

        # CASE 2: Raise an error for unions
        if str(param.annotation).startswith(("typing.Union[", "Union[")):
            raise ValueError("typing.Union is not supported")

        # CASE 3: Raise an error for "|"
        if "|" in str(param.annotation):
            raise ValueError('Use of "|" in type hints is not supported')

        # CASE 4: Raise an error for optional parameter with given (non-None)
        # default, e.g. Optional[str] = "asd"
        is_annotation_optional = str(param.annotation).startswith(
            ("typing.Optional[", "Optional[")
        )
        default_given = (param.default is not None) and (
            param.default != inspect._empty
        )
        if default_given and is_annotation_optional:
            raise ValueError("Optional parameter has non-None default value")

    logging.info("[_validate_function_signature] END")
    return sig