Skip to content

tools

dandy.core.typing.tools

get_typed_kwargs_from_callable

Source code in dandy/core/typing/tools.py
def get_typed_kwargs_from_callable(
        callable_: Callable,
        return_defaulted: bool = True,
) -> TypedKwargs:
    signature = inspect.signature(callable_)

    typed_kwargs_dict = {}

    for name, param in signature.parameters.items():
        # if name == 'self':
        #     continue

        if param.annotation is inspect._empty:
             raise DandyCriticalException(f'Parameter {name} of {callable_.__qualname__} has no typed annotation')

        if param.default is inspect._empty:
            typed_kwargs_dict[name] = (param.annotation, ...)

        elif return_defaulted and param.default is not inspect._empty:
            typed_kwargs_dict[name] = (param.annotation, param.default)

    return TypedKwargs(typed_kwargs_dict)