Check parameter and return types for pre and post command hooks.
(
cls, func: Callable[[CommandDataType], CommandDataType], data_type: type[CommandDataType]
)
| 5860 | |
| 5861 | @classmethod |
| 5862 | def _validate_prepostcmd_hook( |
| 5863 | cls, func: Callable[[CommandDataType], CommandDataType], data_type: type[CommandDataType] |
| 5864 | ) -> None: |
| 5865 | """Check parameter and return types for pre and post command hooks.""" |
| 5866 | # validate that the callable has the right number of parameters |
| 5867 | cls._validate_callable_param_count(cast(Callable[..., Any], func), 1) |
| 5868 | |
| 5869 | type_hints, ret_ann = get_types(func) |
| 5870 | if not type_hints: |
| 5871 | raise TypeError(f"{func.__name__} parameter is missing a type hint, expected: {data_type}") |
| 5872 | _param_name, par_ann = next(iter(type_hints.items())) |
| 5873 | # validate the parameter has the right annotation |
| 5874 | if par_ann != data_type: |
| 5875 | raise TypeError(f"argument 1 of {func.__name__} has incompatible type {par_ann}, expected {data_type}") |
| 5876 | # validate the return value has the right annotation |
| 5877 | if ret_ann is None: |
| 5878 | raise TypeError(f"{func.__name__} does not have a declared return type, expected {data_type}") |
| 5879 | if ret_ann != data_type: |
| 5880 | raise TypeError(f"{func.__name__} has incompatible return type {ret_ann}, expected {data_type}") |
| 5881 | |
| 5882 | def register_precmd_hook(self, func: Callable[[plugin.PrecommandData], plugin.PrecommandData]) -> None: |
| 5883 | """Register a hook to be called before the command function.""" |
no test coverage detected