|
| 1 | +########################################################################## |
| 2 | +# NSAp - Copyright (C) CEA, 2025 |
| 3 | +# Distributed under the terms of the CeCILL-B license, as published by |
| 4 | +# the CEA-CNRS-INRIA. Refer to the LICENSE file or to |
| 5 | +# http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html |
| 6 | +# for details. |
| 7 | +########################################################################## |
| 8 | + |
| 9 | + |
| 10 | +import inspect |
| 11 | +from functools import wraps |
| 12 | + |
| 13 | +from .validation import check_type |
| 14 | + |
| 15 | + |
| 16 | +def typecheck(hints_params=True, hints_return=False): |
| 17 | + """ Enforce type hints on the parameters and return types of the decorated |
| 18 | + function. |
| 19 | +
|
| 20 | + Parameters |
| 21 | + ---------- |
| 22 | + hints_params: bool, default=False |
| 23 | + Require all parameter type hints to be specified. |
| 24 | + hints_return: bool, default=False |
| 25 | + Require the return type hint to be specified. |
| 26 | + """ |
| 27 | + def decorator(func): |
| 28 | + checker = TypEx( |
| 29 | + func, |
| 30 | + hints_params=hints_params, |
| 31 | + hints_return=hints_return |
| 32 | + ) |
| 33 | + @wraps(func) |
| 34 | + def wrapper(*args, **kwargs): |
| 35 | + checker.check_params(args, kwargs) |
| 36 | + result = func(*args, **kwargs) |
| 37 | + checker.check_output(result) |
| 38 | + return result |
| 39 | + return wrapper |
| 40 | + return decorator |
| 41 | + |
| 42 | + |
| 43 | +class TypEx: |
| 44 | + """ Enforce type hints on a function. |
| 45 | +
|
| 46 | + Parameters |
| 47 | + ---------- |
| 48 | + func: Callable |
| 49 | + The function whose type hints should be enforced. |
| 50 | + hints_params: bool, default=False |
| 51 | + Require all parameter type hints to be specified. |
| 52 | + hints_return: bool, default=False |
| 53 | + Require the return type hint to be specified. |
| 54 | +
|
| 55 | + Raises |
| 56 | + ------ |
| 57 | + TypeError |
| 58 | + If function parameters are not annotated. |
| 59 | + TraitError |
| 60 | + If the input value have incorrect type. |
| 61 | + NotImplementedError |
| 62 | + If a type is not handled by the code. |
| 63 | + """ |
| 64 | + def __init__(self, func, hints_params=False, hints_return=False): |
| 65 | + self.func = func |
| 66 | + self.hints_params = hints_params |
| 67 | + self.hints_return = hints_return |
| 68 | + self.annotations = self.func.__annotations__ |
| 69 | + param_names = list(inspect.signature(func).parameters.keys()) |
| 70 | + self.ignore_self = (len(param_names) > 0 and param_names[0] == "self") |
| 71 | + self.n_params = (len(self.annotations) |
| 72 | + if "return" not in self.annotations |
| 73 | + else len(self.annotations) - 1) |
| 74 | + if self.hints_return and "return" not in self.annotations: |
| 75 | + raise TypeError( |
| 76 | + f"The return outputs of the '{func.__name__}' function is not " |
| 77 | + "annotated." |
| 78 | + ) |
| 79 | + |
| 80 | + def check_params(self, passed_args, passed_kwargs): |
| 81 | + """ Check input function parameters. |
| 82 | +
|
| 83 | + Parameters |
| 84 | + ---------- |
| 85 | + passed_args: list |
| 86 | + List of args passed to the function |
| 87 | + passed_kwargs: dict |
| 88 | + Dict of kwargs passed to the function |
| 89 | +
|
| 90 | + Raises |
| 91 | + ------ |
| 92 | + TypeError |
| 93 | + If an input parameter is not valid. |
| 94 | + """ |
| 95 | + if self.n_params < len(passed_args): |
| 96 | + raise TypeError( |
| 97 | + "Unexpected number of parameters for the " |
| 98 | + f"{self.func.__name__} function." |
| 99 | + ) |
| 100 | + named_args = list(self.annotations.keys())[:len(passed_args)] |
| 101 | + if self.ignore_self: |
| 102 | + passed_args = passed_args[1:] |
| 103 | + for name, val in zip(named_args, passed_args): |
| 104 | + check_type(val, self.func, name) |
| 105 | + for name, val in passed_kwargs.items(): |
| 106 | + check_type(val, self.func, name) |
| 107 | + |
| 108 | + def check_output(self, value): |
| 109 | + """ Check return value of the function call. |
| 110 | +
|
| 111 | + Parameters |
| 112 | + ---------- |
| 113 | + value: Any |
| 114 | + The return value of the function. |
| 115 | + """ |
| 116 | + check_type(value, self.func, "return") |
0 commit comments