GrahamDumpleton / wrapt

A Python module for decorators, wrappers and monkey patching.
BSD 2-Clause "Simplified" License
2.03k stars 231 forks source link

Function return type is not being preserved when using @wrapt.decorator #233

Open ICE-FlatLine opened 1 year ago

ICE-FlatLine commented 1 year ago

Hi, I am using the following function as a decorator for other functions with the help of wrapt.decorator:

@wrapt.decorator
async def log_function_execution(func, instance, args, kwargs):
    """
    Wrapping CYSDKLogger log writing at the beginning & ending of function
    """
    CYSDKLogger().logger.info(f'Start {func.__name__}')
    result = await func(*args, **kwargs)
    CYSDKLogger().logger.info(f'Finish {func.__name__}')
    return result

Basically, it logs start and finish of the inner function execution. Using it as a simple decorator:

@log_function_execution
async def acquire_by_conf(self, machine_conf: MACHINE_CONF, keep_alive: bool = False,
                          local_execution: Union[None, bool] = None) -> MobileDevice:

However, when calling for the acquire_by_conf() method, IDE (Pycharm) throws this warning: image

Seems like instead of MobileDevice return type, type checker expects Coroutine[Any, Any, MobileDevice] (Not sure if this is indeed the issue). Is there a way to preserve return type of the decorated function for the type checker?

P.S. By removing the decorator from the method, warning from IDE disappears. I am using python 3.7.15