from typing import Callable, Type def prepare_list_arg(name: str, required: bool = True, make_unique: bool = True, result_type: Type = list): """Decorator to make list argument items unique and not none. If the list is empty and required then return empty list without calling the function. Args: name: List kwarg name. required: If the list is required (to be not empty). make_unique: Remove list arg duplicates. result_type: Result type (list or dict). """ def wrapper(f: Callable): async def wrapped(*args, **kwargs): if name in kwargs and isinstance(kwargs[name], list): if make_unique: kwargs[name] = [i for i in set(kwargs[name]) if i] if required and not kwargs[name]: return result_type() return await f(*args, **kwargs) return wrapped return wrapper