Note

This page is a reference documentation. It only explains the class signature, and not how to use it. Please refer to the user guide for the big picture.

brainprep.decorators.CoerceparamsHook

class brainprep.decorators.CoerceparamsHook[source]

Bases: Hook

Convert annotated arguments.

This hook inspects the type annotations of the wrapped function and performs two automatic conversions:

  1. Arguments annotated as File or Directory are converted into pathlib.Path instances.

  2. Arguments annotated as list types (e.g., list[int], list[str]) are parsed from comma-separated strings into Python lists.

Examples

>>> from brainprep.decorators import step, CoerceparamsHook
>>> from brainprep.typing import Directory
>>> from brainprep.utils import Bunch
>>> @step(
...     hooks=[CoerceparamsHook()]
... )
... def myfunc(a: Directory, b: str, c: list[Directory]):
...     '''Convert annotated arguments.'''
...     return Bunch(a=a, b=b, c=c)
>>> result = myfunc("/tmp", "/tmp", "/tmp,/tmp")
>>> print(result)
Bunch(
    a: PosixPath('/tmp')
    b: '/tmp'
    c: [PosixPath('/tmp'), PosixPath('/tmp')]
)
before_call(func, inputs)[source]

Transform and inspect inputs before the function call.

Parameters:
funcCallable

The function to be decorated.

inputsdict[str, Any]

Positional and keyword arguments passed to func.

Returns:
inputsdict[str, Any]

Positional and keyword arguments passed to func where arguments annotated as File or Directory are converted to pathlib.Path objects, and list-typed arguments are coerced from comma-separated strings into lists.

Raises:
ValueError

If the decorated function contains arguments without type annotations.