MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / _validate_prepostcmd_hook

Method _validate_prepostcmd_hook

cmd2/cmd2.py:5862–5880  ·  view source on GitHub ↗

Check parameter and return types for pre and post command hooks.

(
        cls, func: Callable[[CommandDataType], CommandDataType], data_type: type[CommandDataType]
    )

Source from the content-addressed store, hash-verified

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."""

Callers 2

register_precmd_hookMethod · 0.95
register_postcmd_hookMethod · 0.95

Calls 2

get_typesFunction · 0.85

Tested by

no test coverage detected